Thread: Maximum array size (char) to input a double

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Maximum array size (char) to input a double

    Hi guys,

    what size of array should I use to get a double into it using fgets();

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    BUFSIZ would be my suggestion.

    But then again, I would probably use BUFSIZ for reading ALL input, then validate whatever was typed in, just to minimise the problems of the user being a moron and typing excessively long lines of rubbish just to see if they can break your code.

    For a double, you need about 15 decimal digits to fill the precision available.

    myprog < myprog
    is an extreme test of the input handling capabilities of your program
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    BUFSIZ would be my suggestion.

    But then again, I would probably use BUFSIZ for reading ALL input, then validate whatever was typed in, just to minimise the problems of the user being a moron and typing excessively long lines of rubbish just to see if they can break your code.

    For a double, you need about 15 decimal digits to fill the precision available.

    myprog < myprog
    is an extreme test of the input handling capabilities of your program
    Just bear in mind that floating point numbers can be expressed sub-optimally, e.g. using extra zero's at either end of the number, and easily reach into the 50's or 100's of digits and still be valid input for a double.

    And if you do use a buffer that is shorter than bufsiz, at least make sure that the line you read through fgets() is complete - if it's not a complete line, ask the user to enter again [after clearing the buffer as per the FAQ]. Otherwise, you may find that you read something like this:
    Code:
    char buffer[20];
    double d;
    int x;
    fgets(buffer, sizeof(buffer, stdin);
    d = strtod(buffer, ...);
    fgets(buffer, sizeof(buffer, stdin);
    i = strtol(buffer, ...);
    printf("i=%d, d = %f\n", i, d)
    Feed that with
    0.00000000000000000012345
    4711
    and you see the ouput of
    Code:
    0.00000
    12345
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    a double is 8 bytes. so -
    Code:
    unsigned char Buffer[8];
    would do.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > a double is 8 bytes. so
    Which as nothing at all to do with it's representation as a string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That would only accommodate 7 digits (excluding the dot), so up to around 99 999 999 or -9 999 999 or 0.999 99 or -0.9999.
    Hmm. Not very long.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Sorry guys what is BUFSIZ?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    BUFSIZ is a standard constant for "the buffer inside a FILE struct".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Got it,

    This is my stdio.h macro for BUFSIZ
    Code:
    #if     defined(_M_MPPC)
    #define BUFSIZ  4096
    #else  /* defined (_M_MPPC) */
    #define BUFSIZ  512
    #endif /* defined (_M_MPPC) */
    I have few questions about it..

    1. Why should I use BUFSIZ ?
    2. Is BUFSIZ is stdin buffer size too ?
    3. Why BUFSIZ can be 4096 or 512 ?
    4. If I'm writing a buffer[20000] to a file using fputs() will it take 20000/FILESIZ times to write a single fputs ?

    Many thanks

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, a fputs() of a large block will take size/BUFSIZ number of actual write operations - if you don't want that behaviour, you need to use fwrite() or a lower level write operation - it is normally not a problem.

    In the header file you are showing, it seems that PowerPC has a 4KB memory buffer, other architectures have 512 byte buffer. Why? Who knows - perhaps something in the PowerPC architecture makes it important that the buffer is 4KB...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1. Why should I use BUFSIZ ?
    It exists to be used.

    It may also signify to the reader that the buffer is only temporary and that any data read into the buffer is also temporary, and needs to be validated before being moved to the correct place in memory.

    It also saves programmer decision time in trying to decide just how big to make the buffer. Excessive squeezing of a few bytes as in abachler's example leads to all sorts of other side effects like forgetting to count the newline AND the \0.

    A couple of dozen "magic number" sizes all through the code may leave you wondering what's going on and what's safe to change. A consistent size may give you more confidence.

    > Is BUFSIZ is stdin buffer size too ?
    Yes, there's no problem using BUFSIZ for stdin.
    Remember, stdin may be redirected from a file on some systems.

    > Why BUFSIZ can be 4096 or 512 ?
    It depends on the architecture (in your example). The system implementers have decided that on a PowerPC a size of 4096 is a better answer than 512.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM