Thread: fgets and size

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    14

    fgets and size

    Lets say i want to create a programm that takes a string and prints it
    At the beggining i have to declare my string and give it a size.For example, if i give it 10, can i write this?
    fgets(string,20,stdin)
    or my second parameter has to be less than the size that i declared already

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    Yes, the size you tell fgets() must fit the space you've declared.
    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
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by giorgosmarga View Post
    Lets say i want to create a programm that takes a string and prints it
    At the beggining i have to declare my string and give it a size.For example, if i give it 10, can i write this?
    fgets(string,20,stdin)
    or my second parameter has to be less than the size that i declared already
    You use fgets like this

    Code:
    char string[1024];
    fgets(string, 1024, stdin);
    User is very unlikely to enter 1024 characters. Probably you only expect 20 or so. But it's usually hard to give a firm upper limit on the length of someone's name, for example. So best make it ridiculously large. Memory is cheap these days.

    Whilst we can make the second parameter less than 1024 and the code will stil work, there is little point in doing this. You want it to be exactly the same size. If user enters more than 1023 characters (the szie includes the nul) then input will be truncated, which is a theroretical difficulty with fgets but not much of a practical one, because legitimate inputs are so unlikely to exceed the buffer.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    888
    If your compiler/c standard library is POSIX:2008 compliant you can use getline() function, which will allocate the buffer for the line for you:
    Code:
    char *line;
    size_t size;
    
    // Need this for allocation
    line = NULL;
    size = 0;
    
    if ( getline( &line, &size, fin ) == -1 )
    { ... handle error here... }
    ...
    free( line );

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,191
    then input will be truncated, which is a theroretical difficulty with fgets
    Which is also why fgets leaves the newline in the string. If it's not there (and the stream isn't at eof) then the line was truncated.
    We live as it were by chance, and by chance we are governed. - Seneca

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets works only with size > 1
    By DoK in forum C Programming
    Replies: 10
    Last Post: 09-10-2017, 10:53 PM
  2. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  3. fgets without specifying size
    By Queue in forum C Programming
    Replies: 5
    Last Post: 09-17-2006, 06:02 PM
  4. fgets() array size memory concern
    By Sereby in forum C Programming
    Replies: 6
    Last Post: 07-28-2004, 10:50 PM
  5. fgets array size
    By stautze in forum C Programming
    Replies: 9
    Last Post: 04-29-2002, 01:25 PM

Tags for this Thread