How to Use putchar() Function in C Programming Language

putchar() Function in C Programming Language

Introduction :

In C programming language, there are various functions are used to display output on the screen. The putchar() function is one of them. It is used to print a single character as output on the console screen (standard output). It is defined in the standard Input Output library (stdio.h). It displays individual characters on the console screen in a simple way.

The putchar() is an important function in the C programming language for managing character output. This function is useful in various applications such as a text-based application, embedded systems programming etc. In this article, I shall show you how to use putchar() function using the C programming language. Here, I shall also show you the syntax and usage of putchar() function.

What is putchar() function :

The putchar() function is used to write a single character to the standard output (console). It is part of the standard C input output library or header file (stdio.h). The putchar() function writes one character at a time on the console screen. Using putchar() function, you can handle special characters like newline (\n), tab (\t) and more.

Syntax of the putchar() function :

In C programming language the following code is the syntax of the putchar() function.

The argument or Parameter of the putchar() function is an int which represents a character. It returns the character as an unsigned char cast to an int or End Of File (EOF) if there is an error.

Different types of programs using putchar() function :

In this article, I have shown you the different uses of the putchar() function in C programming language.

Single character output using putchar() function :

Using putchar() function, you can print a single character on the console screen. Here, you have to pass the character as an argument which you want to print. In this program, putchar() function prints the character “P” on the console. Using “\n” in printf() function, you can add a newline character that moves the cursor to the next line.

Source code of the program :

The following code is used for a single character printing using putchar() function in C programming language.

Output of the program :

You can see the output of printing single character using putchar() function like below image.

output of printing single character using putchar() function

Printing the alphabets using putchar() function :

You can use the putchar() function in a loop to print sequences of characters such as the alphabets. The below program is the example of print all uppercase letters of the alphabet using putchar() function.

Source code of the program :

The following code is used to print all uppercase letters of the alphabet using putchar() function in C programming language.

Output of the program :

You can see the output of printing all uppercase letters of the alphabet using putchar() function like below image.

output of printing all uppercase letters of the alphabet using putchar() function

Printing digits using putchar() function :

You can use the putchar() function in a loop to print the digits from 0 to 9. The below program is the example of print digits from 0 to 9 using putchar() function.

Source code of the program :

The following code is used to print the digits from 0 to 9 using putchar() function in C programming language.

Output of the program :

You can see the output of printing digits from 0 to 9 using putchar() function like below image.

output of printing digits from 0 to 9 using putchar() function

Printing strings using putchar() function :

You can use the putchar() function in a loop to print a string character by character. The below program is the example of print each character of a string using the putchar() function. The putchar() function prints all the character of the string until it encounters the null character (\0).

Source code of the program :

The following code is used to print a string using putchar() function in C programming language.

Output of the program :

You can see the output of printing a string using putchar() function like below image.

output of printing a string using putchar() function

How run the programs :

If you want to run all the above programs, you have to install VS Code on your PC. You can use any other C compiler. Then, open VS Code on your PC. Then, create a new file like as “pac.c”. After that, copy the above code of program one by one and paste in the “pac.c” file and run all the programs on your PC.

Conclusion :

Finally, you have learned how to use putchar() function in the C programming language. Now you can use the putchar() function in your own programs. Thank you for visiting my site.

Scroll to Top