13

Question

How to declare a string variable in C?

Background

In my quest to learn the basics of , I am trying to port one of my oldest 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.

10
  • 1
    Why is this tagged python? Commented Mar 14, 2013 at 21:39
  • 1
    A string is a 0-terminated character array. Still. Commented Mar 14, 2013 at 21:39
  • 2
    Why are you using an integer for name? Commented Mar 14, 2013 at 21:40
  • 3
    Also, this is a C tutorial level question. I suggest following one. (The pedantic but useless answer is that C doesn't have a string type, only character arrays, and string manipulation functions that expect them to be null-terminated.) Commented Mar 14, 2013 at 21:40
  • 1
    There is no better way since the Yahoo answer was posted. The answer there is still the best one. Commented Mar 14, 2013 at 21:42

8 Answers 8

10
char name[60];
scanf("%s", name);

Edit: restricted input length to 59 characters (plus terminating 0):

char name[60];
scanf("%59s", name);
Sign up to request clarification or add additional context in comments.

7 Comments

Surely you mean scanf("%59s", name) ?
@cnicutar yes, but that is a subject of buffer overflow question :)
Well since you're dealing with a self-proclaimed beginner there's no point in teaching questionable habits from the start.
@ValeriAtamaniouk so... what happens if my name is over 60 characters?
@xxmbabanexx Undefined behavior. Most likely input will write all over the stack.
|
3

The int your putting is not a string, a string looks like "char myString[20]". Not like "int name", that's an integer and not a string or char. This is the code you want:

         int main(int argc, const char * argv[])
{

char name[9999];
printf("What is your name?\n");
scanf("%s", name);
system("cls");
printf("Your name is %s", name);

return 0;
}

Comments

2

In C you can not direct declare a string variable like Java and other language. you'll have to use character array or pointer for declaring strings.

char a[50];
printf("Enter your string");
gets(a);

OR

char *a;
printf("Enter your string here");
gets(a);

OR

char a[60];
scanf("%59s",a);

1 Comment

Is better don't use gets, use fgets as I did in my answer. Why? I had several problems with a more complex problem using gets
1

TESTED ON XCODE

You can do so:

int main(int argc, const char * argv[])
{

    int i;
    char name[60]; //array, every cell contains a character

    //But here initialize your array

    printf("What is your name?\n");
    fgets(name, sizeof(name), stdin);
    printf("Your name is %s", name );

    return 0;
}

Initialize the array, is good to avoid bug

for(i=0;i<60;i++){
      name[i]='\0'; //null
}

Instead int is used for int number (1, 2, 3, ecc.); For floating point number instead you have to use float

2 Comments

What does fgets and the related arguments mean?
'fgets' put character by character in every cell for the lenght of the array name and put after the last cell used '\0' that means null. stdin clean the stream of the input
0

C does not have a string variable type. Strings can be stored as character arrays (char variable type). The most basic example I would add up to the rest is:

int main()
{
   char name[] = "Hello World!";
   printf("%s",name);
   return(0);
}

Comments

0

It's easy!

Just put this line below, atop of your main() function.

typedef string char*;

This allows you to create a string variable as you do with integers or characters in C. After that, your program should look like this:

#include <stdio.h>

typedef char* string;

int main(void) {
    string a = "Hello";
    printf("%s\n", a);  // %s format specifier for String
    return 0;
}

For a live demonstration, visit this REPL.it.

1 Comment

You typed the type declaration backward when you say, "just put this line...". Then, in the block below, you have it right.
-1

Normally we use "&" in scanf but you shouldn't use it before variable "name" here. Because "name" is a char array. When the name of a char array is used without "[]", it means the address of the array.

Comments

-2

replace int name; to--. char name[60];

#include <stdio.h>
int main()
{
  char name[648];
  printf("What is your name?");

  scanf("%s", name);
  printf("Your name is %s", name );

  return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.