C interesting Q & A

By Jegathesan | February 26, 2015 [Innovation Begins Here]

Run the following code

/*Innovation Begins Here */

#include<stdio.h>

main(){

char name[10];

printf(“Enter ur name\n”);

gets(name);

puts(name);}

After executing code surely you will get one warning message.

warning: the gets function is dangerous and should not be used.

Do you know why?

The gets() system call is part of the C programming language’s standard I/O library. It takes a char pointer as its only input, and will try to fill the buffer the pointer presumably points to with a line of text. It is widely considered to be a bad idea, since it will gladly overflow any buffer it is passed (BoundsChecking is not performed, in other words, and this can lead to MemoryCorruption). Most C programmers regard use of gets() as a sign of general cluelessness on the part of whoever wrote the code.fgets() is widely advocated as a drop-in replacement to gets(), as fgets() also takes as a parameter the size of the buffer. But this is not entirely satisfactory, either, since if the input exceeds the size of the buffer fgets() will simply null-terminate the string right where the buffer ends and exit, leaving unread characters on standard input.

Follow Innovation Begins Here on Twitter, innovationBeginsHere,Facebook , Google+ & Linkdin.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s