Monday 1 April 2013

gets() vs fgets()


    char inputString[100] = {0};
To get string input from the keyboard which one of the following is better?

    1) gets(inputString)
    2) fgets(inputString, sizeof(inputString), fp)
Answer & Explanation:
The second one is better because gets(inputString) doesn't know the size of the string passed and so, if a very big input (here, more than 100 chars) the charactes will be written past the input string. When fgets is used with stdin performs the same operation as gets but is safe.


Blog Author: Vijay Kumar

No comments:

Post a Comment