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.

xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60

8 Answers8

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

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

char name[60];
scanf("%59s", name);
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • 3
    Surely you mean `scanf("%59s", name)` ? – cnicutar Mar 14 '13 at 21:40
  • @cnicutar yes, but that is a subject of buffer overflow question :) – Valeri Atamaniouk Mar 14 '13 at 21:41
  • 1
    Well since you're dealing with a self-proclaimed beginner there's no point in teaching questionable habits from the start. – cnicutar Mar 14 '13 at 21:41
  • @ValeriAtamaniouk so... what happens if my name is over 60 characters? – xxmbabanexx Mar 14 '13 at 21:42
  • 1
    @xxmbabanexx Undefined behavior. Most likely input will write all over the stack. – cnicutar Mar 14 '13 at 21:43
  • Also, the `scanf` is in yellow in xcode. It says `Format specifies type 'char *' but the argument has type 'char(*)[60]'` – xxmbabanexx Mar 14 '13 at 21:44
  • @xxmbabanexx As this variable is automatic, you will get SIGSEGV when trying to return from function. As `printf` uses buffered output, you will likely not see the message. And consider yourself lucky if you program just crashes quick and dirty: it is easy to catch and fix. Otherwise you will get a headache with your program behaving in some strange manner – Valeri Atamaniouk Mar 14 '13 at 21:45
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;
}
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);
Tushar Gaurav
  • 486
  • 4
  • 18
  • 1
    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 – Mitro Mar 15 '13 at 18:02
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

Mitro
  • 1,230
  • 8
  • 32
  • 61
  • What does `fgets` and the related arguments mean? – xxmbabanexx Mar 14 '13 at 22:16
  • '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 – Mitro Mar 14 '13 at 22:21
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);
}
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.

  • You typed the type declaration backward when you say, "just put this line...". Then, in the block below, you have it right. – Jeff McMahan Nov 06 '20 at 23:12
-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.

Ceylan
  • 1
-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;
}
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42