Originally Posted by
CougarElite
Code:
#include <stdio.h>
int awesome_name;
main()
{
printf("Please enter your name: ");
scanf("%d", &awesome_name);
printf("Your name is %d", awesome_name);
return 0;
}
When the program with that source runs, it says Please enter your name:. I input it then press enter but then it comes up and says Your name is 0. What is up with that? Does C even allow you to use text like that? Is there a certain code I have to use?
At the point where you say the compiler takes a few bytes (in almost cases 4) and he will use them as number-space. They will allways be interpreted like a number.
So if you want to store a name (a string) you would need to change this to something like "char awesome_name[x]" where x can be any number and stands for the amount of characters in the string.
Then you need to tell scanf to read in a string instead of a number by changing to
Code:
scanf("%1234s", &awesome_name);
where 1234 is the length x you chose for the string.
And finally you need to tell printf to put out a string also:
Code:
printf("Your name is %s", awesome_name);
so far, Chris