🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
putchar() function in C is a standard library function that writes a single character to the standard output (stdout). It is part of the C standard library (stdio.h) and is commonly used for simple output operations.Understanding putchar()
The putchar() function writes the character specified by the argument char to the standard output (stdout). It is useful for writing characters one at a time, handling output character by character, and for implementing features like character-based menus or simple text editors.
putchar() Function Syntax
The syntax for the putchar() function is as follows:
int putchar(int char);
Parameters:
char: The character to be written to the standard output. It is passed as anint, but it is internally treated as anunsigned char.
Returns:
- The function returns the character written as an
unsigned charcast to anintorEOFif an error occurs.
Examples
Writing a Single Character
To demonstrate how to use putchar() to write a single character to the standard output, we will write a simple program.
Example
#include <stdio.h>
int main() {
int ch = 'A';
// Write a single character to standard output
putchar(ch);
// Print a newline for clarity
putchar('\n');
return 0;
}
Output:
A
Using putchar() in a Loop
This example shows how to use putchar() in a loop to write multiple characters.
Example
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int i = 0;
// Write each character of the string to standard output
while (str[i] != '\0') {
putchar(str[i]);
i++;
}
// Print a newline for clarity
putchar('\n');
return 0;
}
Output:
Hello, World!
Real-World Use Case
Implementing a Simple Progress Indicator
In real-world applications, the putchar() function can be used to implement simple progress indicators that show progress one character at a time.
Example
#include <stdio.h>
#include <unistd.h> // For sleep function
int main() {
int i;
printf("Progress: ");
for (i = 0; i < 10; i++) {
// Simulate progress by printing a dot and flushing the output buffer
putchar('.');
fflush(stdout);
// Sleep for 1 second to simulate work being done
sleep(1);
}
// Print a newline for clarity
putchar('\n');
printf("Completed!\n");
return 0;
}
Output:
Progress: ..........
Completed!
Conclusion
The putchar() function is a simple and efficient way to write a single character to the standard output. It is often used in programs where character-by-character output is required, such as in text processing or interactive console applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment