Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

The Function Ungetc ()() in C

Explore The Function Ungetc ()() in C with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers the function ungetc ()() in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

Declare the Ungetc ()() Function in C

Below is the declaration for the ungetc () function in C:

 int ungetc ( int char , FILE * stream ) 

Parameters

Char - This is a character to be pushed back.

Stream - This is the pointer to a FILE object that identifies the Input Stream.

Returns the value

If successful, it returns the character that was pushed back. Otherwise, the function returns EOF and Stream remains unchanged.

For example

The following program C illustrates the usage of the ungetc () function in C:

 #include int main () { FILE * fp ; int c ; char buffer [ 256 ]; fp = fopen ( "baitapc.txt" , "r" ); if ( fp == NULL ) { perror ( "Xuat hien loi trong khi mo baitapc.txt" ); return (- 1 ); } while (! feof ( fp )) { c = getc ( fp ); /* thay the ky tu ! boi ky tu + */ if ( c == '!' ) { ungetc ( '+' , fp ); } else { ungetc ( c , fp ); } fgets ( buffer , 255 , fp ); fputs ( buffer , stdout ); } return ( 0 ); } 

Suppose we have the bait. Txt file that contains the following data. This file will be used as input.

Compile and run the above C program to see the results.

According to Tutorialspoint

Previous lesson: Ham puts () in C

Next lesson: Function of perror () in C

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.