C Library Function - putc()
Last Updated :
06 Jun, 2023
In C, the putc() function is used to write a character passed as an argument to a given stream. It is a standard library function defined in the <stdio.h> header file. The function first converts the character to unsigned char, then writes it to the given stream at the position indicated by the file pointer, and finally increments the file pointer by one.
Syntax of putc()
int putc(int ch, FILE *stream);
Parameters
- ch - This is the character to be written.
- stream - This is a pointer to a FILE object that identifies the stream where the character is to be written.
Return Value
- If the operation is successful, the function returns the character written.
- If an error occurs or the end of the file is reached, it returns EOF.
Examples of C putc()
Example 1:
In this example, we will write a single character to a file using putc().
C
// C program to demonstrate the putc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fp = NULL;
// Open the file in write mode
fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
"w");
if (fp == NULL) {
// Print an error message if
// the file couldn't be opened
printf("The file can't be opened!\n");
// Exit the program with an error code
exit(1);
}
// Character to be written to the file
char ch = 'A';
// Write the character to the file
putc(ch, fp);
// Print a message
// indicating that
// the file has
// been modified
printf("File has been modified !\n");
// Close the file
fclose(fp);
// Set the file pointer to NULL for safety
fp = NULL;
return 0;
}
OutputFile has been modified !
If we open the generated file.txt, it will print the following content:
C
// C program to display the content of the above text file
#include <stdio.h>
int main()
{
FILE* fptr;
int temp;
// Open the file in read mode
fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
"r");
while (1) {
// Read a character from the file
temp = fgetc(fptr);
// Check if end of file has been
// reached
if (feof(fptr)) {
// If so, break out of the loop
break;
}
// Print the character
printf("%c", temp);
}
// Close the file
fclose(fptr);
return (0);
}
Output
A
Example 2:
In this example, we will use a for loop to write all the characters between A to Z to a file using putc(). See the following code:
C
// C program to demonstrate the putc() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fp = NULL;
// Open the file in write mode
fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
"w");
if (fp == NULL) {
// Print an error message if
// the file couldn't be opened
printf("The file can't be opened!\n");
// Exit the program with an error code
exit(1);
}
for (int ch = 65; ch <= 90; ch++) {
// Write characters A to Z to the file
putc(ch, fp);
}
// Print a message
// indicating that
// the file has
// been modified
printf("File has been modified !\n");
// Close the file
fclose(fp);
// Set the file pointer to NULL for safety
fp = NULL;
return 0;
}
OutputFile has been modified !
If we open the generated file.txt, it will print the following content:
C
// C program to display the content of the above text file
#include <stdio.h>
int main()
{
FILE* fptr;
int temp;
// Open the file in read mode
fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
"r");
while (1) {
// Read a character from the file
temp = fgetc(fptr);
// Check if end of file has been reached
if (feof(fptr)) {
// If so, break out of the loop
break;
}
// Print the character
printf("%c", temp);
}
// Close the file
fclose(fptr);
return (0);
}
Output
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Similar Reads
putchar() function in C The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value:
1 min read
putwchar() function in C/C++ The putwchar() function in C/C++ writes a wide character to stdout (standard output). It is defined in CPP library . Syntax : wint_t putwchar(wchar_t ch) Parameter : The function accepts one mandatory parameter ch which specifies the wide character to be written. Return value : The function returns
2 min read
putwchar() function in C/C++ The putwchar() function in C/C++ writes a wide character to stdout (standard output). It is defined in CPP library . Syntax : wint_t putwchar(wchar_t ch) Parameter : The function accepts one mandatory parameter ch which specifies the wide character to be written. Return value : The function returns
2 min read
strcmpi() function in C The strcmpi() function is a built-in function in C and is defined in the "string.h" header file. The strcmpi() function is same as that of the strcmp() function but the only difference is that strcmpi() function is not case sensitive and on the other hand strcmp() function is the case sensitive. Sy
2 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read