Thread: Noob character numbers question

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    9

    Noob character numbers question

    I've been learning c coding from the book "c coding for dummies" I don't understand why this program is set for 2 characters in the char function. Normally the char function specifies how many characters in the function. I just don't get why this is specified as 2, I tried changing it to 1 and 0 and received the same output from the program. thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
                                 
    int main()
    {
     char num[2];
     int number;
     
     printf("I am your computer genie!\n"); 
     
     printf("Enter a number from 0 to 9:");
     gets(num);
     number=atoi(num);
     
     
     if(number<5)  
     {
      printf("That number is less than 5!\n");
     }
     
     printf("The genie knows all, sees all!\n");
     return(0);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    By convention, strings in C are an array of characters, with a trailing zero character. So the string "9" is represented using two characters, an '9' and a '\0' (a character with value zero, which is distinct from the character '0' that looks like a zero when printed).

    gets() when reading data from stdin, if you enter 9 then the enter key, will write two characters (a '9' then a '\0') to the supplied buffer. So, if you can trust the user to enter a single digit followed by the enter key, you need a buffer of two characters.

    atoi(), when given that string, will keep reading digits until it sees the '\0' (unless an overflow occurs). That is why atoi() will convert a string "27" to the value 27 rather than the value 2.

    As to changing the buffer length to 0 (or 1): you just got lucky. Formally (i.e. according to the C standard) you invoked undefined behaviour from gets() by doing that .... if the user entered a digit then the enter key, gets() will happily attempt to write two characters to characters to a 0 (or 1) character buffer. In practice, that works sometimes (eg depending on how your compiler lays out the variables in memory) but not other times. It is also the sort of code that might work perfectly well with one compiler, but crash when the code is compiled with another compiler.


    Note: generally it is also a really bad idea to use gets(), as gets() will happily attempt to write 10 characters to a 2-character buffer, depending on what the user supplies. Relying on the user to do the "right thing" is bad practice. Generally, it is better to use fgets(), as you can tell it what the length of buffer is. Note that, depending on user input, that fgets() does put a newline (a '\n' character) into the buffer in some circumstances. So read the documentation for fgets() safely.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    9
    Thanks grumpy, I appreciate the help, I'll keep that in mind about using the gets() function. I'm just following the order of the functions and codes dumped to me in the book. I'm sure that the function fgets() will be braught up sooner or later.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That depends on the book. Quite a few never delve into fgets(), unfortunately. They deem it to be a topic that is "too advanced", I guess. Hence we get beginners learning bad technique.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    9
    Hmm, you may be right about that, I'm not seeing anything mentioned about the fgets() function. Is there any books you would recommend after I finish this one? I also heard learn code the hard way provides good information on programming languages.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    9
    Hello again, So from what you told me the strings in the char storage are always seen as two.For example because the program wanted a single character the storage was set to two because of the trailing 0 afterwords. Though in reality they are only 1 digit, is that correct?

    Anyways, my new question is how would this be applied with an odd number and for what reason is 5 set for char storage? I listed the code below. I apologize for bothering you with all these questions. If you know of a website that would provide me with all the info that I would need to know about this, I would appreciate it. thanks.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    int main()
    { 
     int tax1,tax2;
     char height[4],temp[4],favnum[5];
     
     printf("Enter the height in inches:");
     gets(height);
     printf("What temperature is it outside?");
     gets(temp);
     printf("Enter your favorite number:");
     gets(favnum);
     
     tax1=atoi(height)*atoi(favnum);
     tax2=atoi(temp)*atoi(favnum);
     
     if(tax1>tax2)
     {
      printf("You owe $%d in taxes.\n",tax1*10);
     }
     if(tax2>=tax1)
     {
      printf("You owe $%d in taxes.\n",tax2*10);
     }
     return(0);
    }
    Last edited by bitpixel; 05-12-2012 at 12:57 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bitpixel View Post
    Hello again, So from what you told me the strings in the char storage are always seen as two.For example because the program wanted a single character the storage was set to two because of the trailing 0 afterwords. Though in reality they are only 1 digit, is that correct?
    No, that is not correct. Strings for storage are not seen as two. By convention, a "string" is stored in an array of characters, plus a zero (also called nul) terminator. In general terms, if the user is entering N characters (which the program is reading using gets()) then the buffer needs to be at least N+1 characters - to hold the N characters plus the terminator.

    Presumably, in your example, favnum is an input from the user of at least four characters. So, if the user inputs "9876" followed by the enter key the storage required is five characters (the '9', '8', '7', '6', and '\0').

    Note that, if the user misbehaves, and enters "100000" for favum, then the array favnum will be overflowed. That is why using gets() is a bad idea .... there is absolutely no way for the program to recover if the user goes outside what the programmer expects.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    9
    Right, that is what I meant, maby you just misinterpreted what I was saying. All I really meant was say you were to enter two characters "56" then would need the amount of storage for 3 because of the terminator 0. Right?

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    9
    Anyways thanks for the help Grumpy, I completely understand what you mean now.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bitpixel View Post
    Right, that is what I meant, maby you just misinterpreted what I was saying.
    I'm not going to wear that one.

    You're the one who said "the strings in the char storage are always seen as two". There is no interpretation of those words which express what (you now claim) you were saying. Particularly as, in that same post, you expressed inability to understand why some of the input might have required a buffer of length five.

    The possibilities are either that you misunderstood until I corrected you or, if you actually did understand, you expressed the concept incorrectly. Either way, the error was in either your comprehension or in your expression.

    Quote Originally Posted by bitpixel View Post
    All I really meant was say you were to enter two characters "56" then would need the amount of storage for 3 because of the terminator 0. Right?
    That might or might not have have been what you meant. It is not what you said.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    9
    Most likely expressing the concept incorrectly, but that is what I meant.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    9
    Cut me a break I'm a noob -_-..

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Pffft. Being a newbie does not excuse the fact that you tried to bluff and assign responsibility to me for an error that you made.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    9
    Okay, you're right about that grumpy, obviously you're quite an experienced programmer. I didn't mean it as an insult or anything if thats what you saw it as. But anyways, I appologize for that, I've just had a long day..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question
    By dhuan in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2010, 05:00 AM
  2. noob question
    By gillypie in forum C Programming
    Replies: 21
    Last Post: 09-04-2007, 07:49 AM
  3. Noob question?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-21-2006, 09:41 PM
  4. read text file, convert character numbers to int?
    By hyaku_ in forum C Programming
    Replies: 6
    Last Post: 11-06-2006, 05:04 PM
  5. Noob Question
    By ytaipsw in forum C Programming
    Replies: 27
    Last Post: 04-27-2006, 07:49 PM