YouTip LogoYouTip

C Function Putchar

## C Library Function - `putchar()` The `putchar()` function is a standard library function in C that writes a single character to the standard output stream (`stdout`). It is defined in the `` header file and is one of the most fundamental functions used for character-based output operations. --- ## Syntax and Declaration To use the `putchar()` function, you must include the `` header file in your program. ### Function Prototype ```c int putchar(int character); ``` ### Parameters * **`character`**: This is the character to be written to `stdout`. Although the parameter is of type `int`, it is internally converted to an `unsigned char` before being written. You can pass a character literal (e.g., `'A'`), a `char` variable, or its corresponding ASCII integer value (e.g., `65`). ### Return Value * **On Success**: The function returns the character written, cast as an `int`. * **On Failure**: The function returns `EOF` (End-of-File) and sets the error indicator for the stream. --- ## How `putchar()` Works The `putchar()` function is functionally equivalent to calling `putc(character, stdout)`. It is highly optimized and often implemented as a macro in many C standard library implementations, making it faster and more efficient than `printf("%c", character)` for printing single characters. --- ## Code Examples ### Example 1: Printing the Alphabet (A to Z) The following example demonstrates how to use `putchar()` in a loop to print all uppercase letters from 'A' to 'Z'. ```c #include int main() { char ch; // Loop through the characters from 'A' to 'Z' for (ch = 'A'; ch <= 'Z'; ch++) { putchar(ch); } // Print a newline character at the end putchar('\n'); return 0; } ``` #### Output ```text ABCDEFGHIJKLMNOPQRSTUVWXYZ ``` ### Example 2: Printing Characters Using ASCII Values Since `putchar()` accepts an integer, you can pass ASCII values directly to it. ```c #include int main() { // ASCII value for 'H' is 72, 'e' is 101, 'l' is 108, 'o' is 111 putchar(72); // H putchar(101); // e putchar(108); // l putchar(108); // l putchar(111); // o putchar('\n'); // Newline return 0; } ``` #### Output ```text Hello ``` --- ## Key Considerations and Best Practices ### 1. `putchar()` vs. `printf()` * **Performance**: `putchar()` is significantly faster than `printf()` because it does not need to parse a format string (like `%c` or `%d`). If you only need to output a single character, always prefer `putchar()`. * **Simplicity**: Use `putchar()` for simple character outputs and `printf()` when you need complex formatting. ### 2. Handling Return Values and Errors In robust production code, it is good practice to check the return value of output functions to ensure the write operation succeeded, especially when writing to files or piped streams. ```c #include int main() { int result = putchar('A'); if (result == EOF) { perror("Failed to write character"); return 1; } return 0; } ``` ### 3. Output Buffering Like most standard output functions, `stdout` is typically line-buffered. This means that characters written with `putchar()` may not appear on the screen immediately until a newline character (`\n`) is written, or until `fflush(stdout)` is explicitly called.
← Eclipse TutorialC Function Putc β†’