tags:

views:

95

answers:

3

Hi,

I am beginner for programming.I referred books of C programming,but i am confused.

1.) What's the difference betweent printf and gets? I believe gets is simpler and doesn't have any formats?

+3  A: 

They fundamentally perform different tasks.

printf: prints out text to a console.
gets: reads in input from the keyboard.

radman
+3  A: 

printf: allowing you to format a string from components (ie. taking results from variables), and when output to stdout, it does not append new line character. You have to do this by inserting '\n' in the format string.

puts: only output a string to stdout, but does append new line afterward.

scanf: scan the input fields, one character at a time, and convert them according to the given format.

gets: simply read a string from stdin, with no format consideration, the return character is replaced by string terminator '\0'.

http://en.wikipedia.org/wiki/Printf

http://en.wikipedia.org/wiki/Gets

Edward Leno
+6  A: 

printf

The printf function writes a formatted string to the standard output. A formatted string is the result of replacing placeholders with their values. This sounds a little complicated but it will become very clear with an example:

   printf("Hello, my name is %s and I am %d years old.", "Andreas", 22);

Here %s and %d are the placeholders, that are substituted with the first and second argument. You should read on the man page (linked above) the list of placeholders and their options, but the ones you'll run into most often are %d (a number) and %s (a string).

Making sure that the placeholder arguments match their type is extremely important. For example, the following code will result in undefined behavior (meaning that anything can happen: the program may crash, it may work, it may corrupt data, etc):

printf("Hello, I'm %s years old.", 22);

Unfortunately in C there is no way to avoid these relatively common mistakes.

gets

The gets function is used for a completely different purpose: it reads a string from the standard input.

For example:

char name[512];
printf("What's your name? ");
gets(name);

This simple program will ask the user for a name and save what he or she types into name.

However, gets() should NEVER be used. It will open your application and the system it runs on to security vulnerabilities.

Quoting from the man page:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

Explained in a more simple way the problem is that if the variable you give gets (name in this case) is not big enough to hold what the user types a buffer overflow will occur, which is, gets will write past the end of the variable. This is undefined behavior and on some systems it will allow execution of arbitrary code by the attacker.

Since the variable must have a finite, static size and you can't set a limit of the amount of characters the user can type as the input, gets() is never secure and should never be used. It exists only for historical reasons.

As the manual suggested, you should use fgets instead. It has the same purpose as gets but has a size argument that specifies the size of the variable:

char *fgets(char *s, int size, FILE *stream);

So, the program above would become:

char name[512];
printf("What's your name? ");
fgets(name, sizeof(name) /* 512 */, stdin /* The standard input */);
Andreas Bonini
nice answer. A safer alternative to gets is fgets:char *fgets(char *s, int size, FILE *stream);eg. : fgets(name, 512, stdin);fgets reads size-1 chars, because a 00-byte is used to mark the end of the string.
terabaud
Thank you sirNow I have a good idea from above description.
Nilesh