Thread: How to define a variable for storing string value?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    How to define a variable for storing string value?

    I learnt that C doesn't have string data type. In order to store a string value we have to define an array variable of char data type.

    I just need a clarification. If I want to store a string up to 20 characters, do I have to define the variable as

    char stringValue[20];

    or

    char stringValue[21];?

    I ask this because I read somewhere in the book, it says that a string must also include the null character, '\0'.

    I tried defining a variable to store the grade value as the following:

    char grade[2];

    Then I use the scanf("%s",grade) to read the value. When I keyed in the grade as B+ and later print it back as follows:

    printf("The grade you have entered is [%s].", grade)

    It appeared as:

    The grade you have entered is [B+].

    It seems that it accepted the entry as two characters. Does it mean the null character is not counted in the array size?

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    I'm a bit unclear as to exactly what your source code was but my guess is this is what you mean. If it's not, let me know:

    #include <stdio.h>
    Code:
    int main(){
    
      char grade[2];
    
      printf("Input a grade: ");
      scanf("%s", grade);
      printf("\nThe grade you entered was: %s\n", grade);
    
      return 0;
    }
    You are correct in that it will store the 'B' and the '+' character in a row, but your string will not be null terminated.

    It is a good practice to declare character arrays of size (DESIRED_SIZE + 1) and to ensure that last extra index is filled with a null character. Some very useful string functions in <string.h> assume your strings are null-terminated. The null character (aka null terminator) is written '\0'.

    Getting into this habit, if you're planning on doing long term C programming, is a good idea. Also, there are some issues with using scanf() to read in strings.

    scanf for strings?

    Also, to answer your last question: you'll often hear that C is stupid in that it doesn't make many assumptions for you, even if they seem intuitive to you. You want to use a null terminator, you need tell C about the extra index. Many functions already written, or that you may desire to write, cycle through a string until they reach the end. C only knows about 'the end' due to these null terminators. In C if you declare an array (technically a pointer to the first element. Using [] and indexing through the array) you can make assignments like

    Code:
    char string[2];
    string[2]  = '\0';  /* because we start indices at 0, we've overstepped our array.
    C will not stop you from doing this. The issue is that you may write over something critical, and you'll likely get a segmentation fault. It may seem perfectly intuitive that you can just add the '\0' in memory right after your array, but it doesn't work out. C also doesn't just take care of it for you, so you have to keep an eye on these things.

    So to summarize, yes you want to declare the extra space, and get into the habit of null-terminating your strings.
    Last edited by Ocifer; 06-05-2011 at 06:58 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ocifer View Post
    You are correct in that it will store the 'B' and the '+' character in a row, but your string will not be null terminated.
    Actually it will be nul terminated, assuming your program doesn't crash. scanf with %s is going to ensure that a string is read. %s cares nothing about how long the destination is. You are invoking undefined behavior by entering more than your destination allows (running off the end of your array), but, assuming it doesn't crash, you will end up with a \0 at the end -- wherever the end happens to be.

    As said here:
    Quote Originally Posted by Ocifer View Post
    C will not stop you from doing this. The issue is that you may write over something critical, and you'll likely get a segmentation fault. It may seem perfectly intuitive that you can just add the '\0' in memory right after your array, but it doesn't work out. C also doesn't just take care of it for you, so you have to keep an eye on these things.

    So to summarize, yes you want to declare the extra space, and get into the habit of null-terminating your strings.
    Except, depending on what string function you try to use, it may or may not add a \0 for you. For example, strcpy will add a \0, where as strncpy may not.

    Quzah.
    Last edited by quzah; 06-05-2011 at 07:00 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using environment variable: gcc -D vs. #define
    By MK27 in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 09:20 PM
  2. How to define variable of specific size.
    By User Name: in forum C Programming
    Replies: 4
    Last Post: 06-05-2010, 01:33 AM
  3. storing a fil to a variable.
    By lilrayray in forum C Programming
    Replies: 46
    Last Post: 08-05-2006, 12:00 AM
  4. how to define the variable of a edit box?
    By Jasonymk in forum Windows Programming
    Replies: 2
    Last Post: 02-10-2003, 09:23 PM
  5. Storing a name, what variable?
    By RoD in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 02:36 AM