char variable[#] - confusion

This is a discussion on char variable[#] - confusion within the C Programming forums, part of the General Programming Boards category; Okay I'm just learning so bear with me. Suppose i have some code like this: [code] . . char name[4]; ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    char variable[#] - confusion

    Okay I'm just learning so bear with me.

    Suppose i have some code like this:
    [code]
    .
    .
    char name[4];
    printf("Please enter your name:");
    scanf("%s",&name);
    printf("Your name is %s",name);
    .
    .
    [\code]
    Now I've set aside 4 characters for the name variable. However when I type in a name like Christopher I get the full name back.

    Well haven't I specifically set the size for 4 characters? So why do I get 11 characters back in the output? Shouldn't my name be truncated after the 4th character?

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Okay I'm just learning so bear with me.

    Suppose i have some code like this:
    Code:
    .
    .
    char name[4];
    printf("Please enter your name:");
    scanf("%s",&name);
    printf("Your name is %s",name);
    .
    .
    Now I've set aside 4 characters for the name variable. However when I type in a name like Christopher I get the full name back.

    Well haven't I specifically set the size for 4 characters? So why do I get 11 characters back in the output? Shouldn't my name be truncated after the 4th character?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You just got lucky. You actually only allocated enough space for a 3-character name (3 characters plus the trailing \0) so writing anything bigger is undefined behavior. The extra space you wrote into just happened not to be used by anything else at the time. It's safer to use fgets() instead of scanf() to enforce not reading a longer string than you have allocated.

    http://en.wikipedia.org/wiki/Fgets

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Shouldn't my name be truncated after the 4th character?
    C isn't that user-friendly, you get exactly what you ask for, even if it wasn't what you wanted. In your case, run this to see where those extra characters are stored:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char left[256] = {0};
      char name[4];
      char right[256] = {0};
    
      printf ( "Enter your name: " );
      fflush ( stdout );
      scanf ( "%s", name );
    
      printf ( "left:  |%s|\n", left );
      printf ( "name:  |%s|\n", name );
      printf ( "right: |%s|\n", right );
    
      return 0;
    }
    Now try to think of how many ways that could break your program.
    My best code is written with the delete key.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Chris2222 View Post
    Well haven't I specifically set the size for 4 characters? So why do I get 11 characters back in the output? Shouldn't my name be truncated after the 4th character?
    Writing past the end of an array results in that lovely concept of "Undefined Behaviour". since it is undefined, anything could happen, including that everything works just fine. Just don't count on it.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Maybe an illustration would help.
    Code:
    Think of you memory as looking like this where each 0 is one byte:
    ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
    
    Each of those bytes has an address.
    When you start the program the OS will set up an address to store your array:
    char name[4];
    
              |4 bytes| other potential data
              |       |
    ...0 0 0 0|0 0 0 0|0 0 0 0 0 0 0 0 0 0 0 0 ...
               \
                the starting address of array 'name'
    
    You can show this address like this:
    printf("&#37;p", (void*)name);
    
    .....
    When you type in Christopher (and hit enter)
    
    scanf("%s", &name);
    
    ...will store the characters entered and end it with a '\0'
    (called a 'null' which is actually just zero)
    
              |4 bytes| other potential data
              |       |
    ...0 0 0 0|C h r i|s t o p e r 0 0 0 0 0 0 ...
                                   ^
                                   zero added by scanf()
    As was said you will sometimes get away with writing more elements than are allocated.
    That does not mean that you should not worry about it because it works THIS time.
    If another strings data is stored right next to 'name' writing too many characters will ruin that data as the Preludes example will show. . It is up to us as programmers to BE SURE we're staying 'in bounds'. . Just think, it could a simple string or it could be your savings balance!
    Howard();
    Last edited by HowardL; 09-27-2007 at 01:38 PM.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Okay thanks.

    So by specifying name[4] it'll be stable up to 3 characters otherwise, as you have said, they sit in undefined territory and I'll get unpredicatable results with anything beyond the 3 character mark.

    Before, if you wrote more than specified, I thought the computer would automatically allocate more characters to it. Sort of like when a float is multiplied by an int and the answer is forced to become a floating variable - the computer automatically floats the answer but of course in this case that is not the case.
    Thanks for clearing that up.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought the computer would automatically allocate more characters to it.
    "Automatically" isn't something you should expect as a C programmer. The world is your oyster, but you have to dive for it, yank it up, shuck it, cook it, and swallow it by yourself. And it's still kinda slimy.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > I thought the computer would automatically allocate more characters to it.

    In C++ there's a string class which can grow automatically as needed, which in particular lets you input arbitrary-length strings without having to specify an upper size limit in advance. In C, the best you can do easily is to use something like fgets() to make sure that your input doesn't overwrite your fixed-size buffer - but if you exceed it it will be truncated (which at least is better than a buffer overrun).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21