What size should be used for fgets?

moosefoot

I have the following segment of code in one of my programs:

char input[LINE_SIZE + 1]; /* +1 for '\0'. */

while(fgets(input, LINE_SIZE, stdin) != NULL)
{
    /* Do stuff. */
}

In a question that has since been deleted, it was pointed out to me that my code could potentially be buggy. I declare strings with the "+ 1" notation to make the code more informative and readable (ensuring that I do not forget to account for the NULL terminator, because this used to be an issue). However, I have been told that the fgets should use the exact same size for its second parameter. I have seen other posts here that do the same practices as me.

I am unsure. Is it bad practice to not include the "+ 1" in the fgets parameter as well?

John Bode
7.21.7.2 The fgets function

Synopsis

1        #include <stdio.h>
         char *fgets(char * restrict s, int n, FILE * restrict stream);

Description

2     The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

C 2011 Online Draft

Emphasis added.

If you specify LINE_SIZE, then fgets will read at most LINE_SIZE - 1 characters into input, and will write a 0 terminator following the last input character. Note that fgets will store the newline character if there's room.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What return type should be used for setTimeout in TypeScript?

From Java

What is std::decay and when it should be used?

From Java

What is std::move(), and when should it be used?

From Java

What size should TabBar images be?

From Java

What are native methods in Java and where should they be used?

From Dev

What size should be returned from MeasureOverride

From Dev

What should the file size be for website videos?

From Dev

Should a custom Java HashMap size be used?

From Dev

What should be the kernel size for deviation of 0.5

From Dev

Android what is setLevel() and when should it be used?

From Dev

What is LoggingEventData ? How and where should it be used?

From Dev

What collation should be used for french language?

From Dev

What should be used instead of "setListNavigationCallbacks"?

From Dev

What is a parallel for loop, and how/when should it be used?

From Dev

What charsets should be used for url encoding

From Dev

What SqlDbType should be used when creating a table?

From Dev

what should be used New() or var in go?

From Dev

What LayoutParams should be used in an AlertDialog?

From Dev

what should be the size of Drawer Header Image?

From Dev

What kind of network protocol should be used in this scenario?

From Dev

If an XSD does not provide explicitly what encoding should be used, then what encoding should be used by default?

From Dev

Static vars - what are they and when should they be used?

From Dev

What minimum'allows' should be used in iptables rules?

From Dev

What should the file size be for website videos?

From Dev

is cron job what should be used in this case?

From Dev

What collation should be used for french language?

From Dev

What is the difference between "used space", "size" and "size on disk"?

From Dev

What SqlDbType should be used when creating a table?

From Dev

What are form factories and when should they be used?

Related Related

  1. 1

    What return type should be used for setTimeout in TypeScript?

  2. 2

    What is std::decay and when it should be used?

  3. 3

    What is std::move(), and when should it be used?

  4. 4

    What size should TabBar images be?

  5. 5

    What are native methods in Java and where should they be used?

  6. 6

    What size should be returned from MeasureOverride

  7. 7

    What should the file size be for website videos?

  8. 8

    Should a custom Java HashMap size be used?

  9. 9

    What should be the kernel size for deviation of 0.5

  10. 10

    Android what is setLevel() and when should it be used?

  11. 11

    What is LoggingEventData ? How and where should it be used?

  12. 12

    What collation should be used for french language?

  13. 13

    What should be used instead of "setListNavigationCallbacks"?

  14. 14

    What is a parallel for loop, and how/when should it be used?

  15. 15

    What charsets should be used for url encoding

  16. 16

    What SqlDbType should be used when creating a table?

  17. 17

    what should be used New() or var in go?

  18. 18

    What LayoutParams should be used in an AlertDialog?

  19. 19

    what should be the size of Drawer Header Image?

  20. 20

    What kind of network protocol should be used in this scenario?

  21. 21

    If an XSD does not provide explicitly what encoding should be used, then what encoding should be used by default?

  22. 22

    Static vars - what are they and when should they be used?

  23. 23

    What minimum'allows' should be used in iptables rules?

  24. 24

    What should the file size be for website videos?

  25. 25

    is cron job what should be used in this case?

  26. 26

    What collation should be used for french language?

  27. 27

    What is the difference between "used space", "size" and "size on disk"?

  28. 28

    What SqlDbType should be used when creating a table?

  29. 29

    What are form factories and when should they be used?

HotTag

Archive