Here you can see my source code:
#include <stdio.h>
int main()
{
char yourname;
char yoursex;
int yourage = 0;
printf("Hey, what's your name?\n");
printf("My name is: ");
scanf("%s", &yourname);
printf("Oh, hello %s! \n\n", &yourname);
printf("Are you a boy or a girl?: ");
scanf("%s", &yoursex);
printf("Nice to know you are a %s! \n\n", &yoursex);
printf("How old are you %s? I am ", &yourname);
scanf("%d", &yourage);
printf("I see you are %d, you have many years then!", &yourage);
return 0;
}
I was trying things that I didn't knew, and strangely it is not working for me. What's the problem? Also, why it needs to be %s and not %c? If I use %c instead it does not work!
Where it says: How old are you %s? instead of putting my name, it says ''oy''
and instead of showing my age in the last line, it shows a big number.
This is a single character:
But
%s
indicates that the variable is a string (i.e., an array of characters terminated by aNUL
). That's why you need%c
. If you really did mean to use a string, then define the variable likeAlso, you are correct to use the address of the variable with
scanf()
, butprintf()
needs the value. So instead ofuse
The "big number" is the memory address.
The expression
char yourname;
only holds space for a single character, so quite likely you end up corrupting the memory space when scanning for yourname. You should allocate a bigger buffer and make sure that you don't overrun its length by setting a maximum number of characters to be read with the scanf function; as described in some of the other answers.The fact that the following
printf
print correctly the name doesn't mean that the memory doesn't get corrupted; as C/C++ don't really check the boundary of any strings or arrays used at runtime.As suggested by others, starting by reading a good book about C and/or C++ wouldn't a bad idea.
Make sure you read the comments in code!
%s
is for reading a string -- multiple characters delimited by whitespace.%c
is for reading a single char.You declare your
yourname
andyoursex
vars as characters, and then try to read strings into them. The string read will overwrite random other things in the stack frame and misbehave or crash.You want to declare
yourname
andyoursex
as character arrays, so they can hold strings:then, when reading into them, you want to include a length limit so they don't overflow:
These are the very basics of C Programming, and I strongly advise you to get a decent book - The C Programming Language by Dennis Ritchie would be a good start.
There are numerous errors in your code.
A
char
can contain only one character, like 'A', or 'a' or something like that. When you're scanning a name, it is going to be a group of characters, like 'E', 'd', 'd', 'y'. To store multiple characters, you need to use a character array. Also, the format specifier used to scan/print characters is%c
,%s
is for when you need to scan a group of characters, also called a string into an array.When you use
printf
, you do not supply a pointer to the variable you are trying to print (&x
is a pointer to variablex
). The pointer is a 32/64-bit integer, which is likely why you see a random integer when trying to print.printf("%c\n", charVar)
is sufficient.scanf
does not need an&
while using%s
as the format specifier, assuming you have passed a character array as the argument. The reason is,scanf
needs to know where to store the data you are reading from the input - and that is given by a pointer to the memory location. When you need to scan an integer, you need to pass an&x
- which means, pointer to memory location of x. But when you pass a character array, it is already in the form of a memory address, and doesn't need to be preceded by an ampersand.I once again recommend you look up some decent tutorials online, or get a book (the one I mentioned above is a classic). Type the examples as given in the material. Experiment. Have fun. :)