EmbLogic's Blog

DIFF BTWN gets(), fgets(), getc(), fgetc() , getchar(), ungetc().

1. All these functions gets(),fgets(),getc(),fgetc(),getchar(),ungetc() …are function is declared in the header file stdio.h.

2.gets() :-it takes single argument.The argument must be a data item representing a string.
gets() doesn’t allow to specify the length of the buffer to store the string in.
This would allow user to keep entering data past the end of your buffer…
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.

3.fgets():-This function read bytes from stream into the array pointed to by s, until n-1 bytes are read,
or a is read and transferred to s, or an end-of-file condition is encountered.

RETURN VALUE :Both gets() and fgets() return a pointer to the string passed.
and on failure of function gets() and fgets() returns NULL.

4.getc() :-returns a character from the specified file.

5.fgetc():-it’s equivalent to the same getc().Only the implementation of the two functions differs.

6.fgetc() and getc() are equivalent, except that getc may be implemented as a macro in some libraries.

7.getchar():- this is similiar to getc().it return a char form stdin.

8.All these above function getc(),fgetc(),getchar() return EOF on error.

9.ungetc() :-we know how getc() reads the next character from a file stream,this ungetc() is the opposite of getc()
it pushes a character back into the file stream so that it will show up again on the very next read from
the stream, as if you’d never gotten it from getc() in the first place.

Now ,suppose you have a stream of data that you’re reading a character at a time, and you won’t know to
stop reading until you get a certain character, but you want to be able to read that character again
later. You can read the character, see that it’s what you’re supposed to stop on, and then ungetc() it
so it’ll show up on the next read.
The standard only guarantees that you’ll be able to push back one character.
RETURN VALUE:On success, ungetc() returns the character you passed to it. On failure, it returns EOF.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>