[Previous]
[Contents]
[Next]

ungetc()

push a character back onto an input stream

Synopsis:

#include <stdio.h>

int ungetc( int c, FILE *fp );

Description:

The ungetc() function pushes the character specified by c back onto the input stream pointed to by fp. This character will be returned by the next read on the stream. The pushed-back character is discarded if a call is made to the fflush() function or to a file-positioning function (fseek(), fsetpos() or rewind()) before the next read operation is performed.

Only one character (the most recent one) of pushback is remembered.

The ungetc() function clears the end-of-file indicator, unless the value of c is EOF.

Returns:

The character pushed back.

Examples:

#include <stdio.h>
#include <ctype.h>

void main()
  {
    FILE *fp;
    int c;
    long value;

    fp = fopen( "file", "r" );
    value = 0;
    c = fgetc( fp );
    while( isdigit(c) ) {
      value = value*10 + c - '0';
      c = fgetc( fp );
    }
    ungetc( c, fp ); /* put last character back */
    printf( "Value=%ld\n", value );
    fclose( fp );
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

fflush(), fgetc(), fgetchar(), fgets(), fopen(), fseek(), fsetpos(), getc(), getchar(), gets(), rewind(), ungetch()


[Previous]
[Contents]
[Next]