Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : ungetc

intungetc(int c,FILE * stream) ;

Return Type : int

Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
Read about return values of ungetc function .

1st Parameter Type : int

Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)

1st Parameter

The character to be pushed back into the input stream. It is passed as an int.
Must not be EOF (operation fails);
Converted to unsigned char before push-back;
Value outside 0-255 range still works (only last byte used);

2nd Parameter Type : FILE *

Pointer to FILE structure for file operations

2nd Parameter

Pointer to the input stream where the character will be pushed back.
Must be open for reading;
Must be valid (not NULL);
Must have room in push-back buffer;
Must not be at invalid stream state;

Read more about parameters of ungetc in parameters section
The ungetcfunction in C language pushes a character back onto a stream, where it is available for subsequent read operations.
ungetc is used for character stream manipulation when you need to "peek" at input without consuming it, making it essential for parsing and lexical analysis tasks. What makes it special is its ability to put a character back into the input stream, effectively "un-reading" it, which is particularly useful when you've read a character but realize it belongs to the next token or operation. The function works closely with input functions like fgetc, getc, and fscanf which will read the pushed-back character on their next call, and it's affected by file positioning functions like fseek and rewind which clear its push-back buffer.

To Summarize the Workflow:
  1. 1.
    Takes character as int and FILE pointer as parameters
  2. 2.
    Pushes back one character into input stream
  3. 3.
    Decrements stream position indicator
  4. 4.
    Returns pushed-back character on success
  5. 5.
    Returns EOF if push-back fails
  6. 6.
    Character becomes available for next read operation


Yes

No

Yes

No

ungetc(int c, FILE *stream)

Is c == EOF?

Push-back possible?

Push character back Decrement position

Return character

Return EOF




Read more about return type and value ofungetc function in return section.
The ungetcfunction takes 2 parameters:
  • int `c`: The character to be pushed back into the input stream. It is passed as an int.
    Must not be EOF (operation fails);
    Converted to unsigned char before push-back;
    Value outside 0-255 range still works (only last byte used);
  • FILE * `stream`: Pointer to the input stream where the character will be pushed back.
    Must be open for reading;
    Must be valid (not NULL);
    Must have room in push-back buffer;
    Must not be at invalid stream state;
Important limitations:
● Only one character push-back guaranteed by standard
● Can't push back after EOF is reached
● Push-back buffer cleared by file positioning functions
● Attempting multiple push-backs may fail on some systems
The ungetc function return value :
  • Success: returns character that was pushed back (as int)
    Failure: returns EOF (-1) when:
    Input is EOF;
    Stream is invalid;
    Push-back buffer is full;
    Stream is in error state
  • Important Considerations:
    Return value matches input parameter on success;
    Can be safely compared with EOF;
    Doesn't indicate how many push-backs remain possible;
    Success doesn't guarantee another push-back will work;

Yes

No

No

Yes

ungetc(int c, FILE *stream)

Is c == EOF?

Push-back possible?

Return EOF: • Invalid stream • Buffer full • Stream state error

Return c: • Character pushed back • Available for next read


Output

This example reads a character from a file, pushes it back, and then reads it again.