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. 🙂
solved problems with scanf and conversion specifiers