Question
How to declare a string variable in C?
Background
In my quest to learn the basics of c, I am trying to port one of my oldest python programs, Bob
, to C. In the program, the script asks the user for information on him or herself, and then spits out responses. Almost all of these variables use raw_input
for their information - the variables are strings. But, I have found no way to declare C variables.
Code
So far, I have tried to declare the variable as of type char
and int.
Here is the code, switch the type at your leisure.
int main(int argc, const char * argv[])
{
int name;
printf("What is your name?");
scanf("%s",&name);
printf("Your name is %s", name );
return 0;
}
Error Message
When I run this code, Xcode
returns some weird stuff. This part of the globidty-gloop is highlighted.
0x7fff96d2b4f0: pcmpeqb(%rdi), %xmm0
Lasty, this Yahoo Answer said that I had to use something called a character array
. It was posted 5 years ago, so I assumed that there was a better way.
EDIT
I am following the tutorial at C Programming.
char name[60];
scanf("%s", name);
Edit: restricted input length to 59 characters (plus terminating 0):
char name[60];
scanf("%59s", name);