Thread: Seeking insight into a link Dave provided to someone

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Seeking insight into a link Dave provided to someone

    I need some kind of brilliant insight into the code at the following URL

    http://www.daniweb.com/code/snippet266.html

    The question stems from mygeti() in the following code.

    Code:
    #include <stdio.h>
    
    int mygeti(int *result)
    {
            char buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
            return fgets(buff, sizeof buff, stdin) && sscanf(buff, "%d", result) == 1;
    }
    
    int main(void)
    {
            int value;
            do {
                    fputs("Enter an integer: ", stdout);
                    fflush(stdout);
            } while ( !mygeti(&value) );
            printf("value = %d\n", value);
            return 0;
    }
    Isn't the line
    Code:
    char buff [ 13 ];
    A bit non-portable? What happens if the code was compiled under a 16 bit machine? Or more to the point, any type of machine who's int type is not 32 bit. Wouldn't the code break?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No. sizeof(char) is always 1, and fgets's argument is sizeof buff, which is always 13. fgets() isn't like gets(), it takes a size parameter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A bit non-portable? What happens if the code was compiled under a 16 bit machine? Or more to the point, any type of machine who's int type is not 32 bit. Wouldn't the code break?
    Oh, I see. You mean, on some machines it might not be possible to enter every possible int value.

    I've never seen a compiler with an int size of over 32 bits. But it's possible.

    You're right, the array should probably be a little larger (perhaps BUFSIZ).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdalten
    Isn't the line
    Code:
            char buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
    A bit non-portable? What happens if the code was compiled under a 16 bit machine? Or more to the point, any type of machine who's int type is not 32 bit. Wouldn't the code break?
    Yes, it's not portable -- that's part of the reason for the comment. I know a little compile-time thingy that can hard-code the correct size for an integer, but I didn't want to overcomplicate the snippet. And there is always the possibility of the user intering something like -1234567890abcdefghijkl. I chose not to get into that in the snippet.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. what's wrong...
    By dreamkk in forum C Programming
    Replies: 9
    Last Post: 10-09-2003, 06:12 AM
  3. destructor with a link list
    By Bones in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2003, 12:01 PM
  4. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 05:57 PM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM