C Output
Learn how to display output in C programming
🖥️ Displaying Output in C
The printf() function is used to display output in C. It's part of the stdio.h library and allows you to print text and variables.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Output:
Hello, World!
Output Functions
printf()
Print formatted text and variables
printf("Hello World");
puts()
Print string with automatic newline
puts("Hello World");
putchar()
Print single character
putchar('A');
Format Specifiers
Display different data types
printf("%d", 42);
🔹 Basic printf() Usage
The printf() function is the primary way to produce C output, printing both plain text and richly formatted data for clear, professional results. By combining literal strings with format specifiers such as %d, %f, %c, and %s, printf() allows precise control over integers, floating-point numbers, characters, and strings. Use escape sequences like \n for new lines and field width/precision (for example, %.2f) to control layout. Mastering printf() improves debugging, user interaction, and overall C programming output quality.
#include <stdio.h>
int main() {
// Print simple text
printf("Welcome to C programming!");
// Print with newline
printf("Line 1\n");
printf("Line 2\n");
// Print multiple lines
printf("First line\nSecond line\nThird line\n");
return 0;
}
Output:
Welcome to C programming!Line 1
Line 2
First line
Second line
Third line
🔹 Format Specifiers
Format specifiers tell printf() how to display different data types in your output. The %d specifier formats integers, %f formats floating-point numbers, %c formats single characters, and %s formats strings. You can control decimal places with specifiers like %.2f for two decimal places. Format specifiers work with scanf() for input too, allowing consistent data handling throughout your program. Mastering format specifiers enables you to create professional-looking output and correctly parse user input in all your C applications.
#include <stdio.h>
int main() {
int age = 25;
float height = 5.8;
char grade = 'A';
// Integer output
printf("Age: %d\n", age);
// Float output
printf("Height: %.1f feet\n", height);
// Character output
printf("Grade: %c\n", grade);
// String output
printf("Name: %s\n", "John");
// Multiple values
printf("Student: %s, Age: %d, Grade: %c\n", "Alice", 20, 'B');
return 0;
}
Output:
Age: 25
Height: 5.8 feet
Grade: A
Name: John
Student: Alice, Age: 20, Grade: B
🔹 Common Format Specifiers
These frequently used format specifiers handle most common data types in C programming. %d prints integers, %i also prints integers, %f prints floating-point numbers with default precision, and %e prints scientific notation. %c displays single characters, %s shows strings, %x displays hexadecimal values, and %% prints a literal percent sign. You can modify specifiers like %5d for width and %.2f for decimal precision. Understanding this complete set of specifiers ensures you can format any output your programs need.
#include <stdio.h>
int main() {
// %d or %i - integers
printf("Integer: %d\n", 42);
// %f - floating point numbers
printf("Float: %f\n", 3.14159);
printf("Float (2 decimals): %.2f\n", 3.14159);
// %c - single character
printf("Character: %c\n", 'X');
// %s - string
printf("String: %s\n", "Hello");
// %x - hexadecimal
printf("Hexadecimal: %x\n", 255);
// %% - print % symbol
printf("Percentage: 85%%\n");
return 0;
}
Output:
Integer: 42
Float: 3.141590
Float (2 decimals): 3.14
Character: X
String: Hello
Hexadecimal: ff
Percentage: 85%
🔹 Other Output Functions
C provides alternative output functions beyond printf() for specific display requirements. The puts() function displays strings followed by automatic newlines, simplifying text output. The putchar() function outputs single characters efficiently. The fputs() function writes to specific file streams. Each function serves different purposes depending on your needs. While printf() handles formatted output, these alternatives provide simpler, faster solutions for basic text display. Knowing when to use each function makes your programs more efficient and your code cleaner.
#include <stdio.h>
int main() {
// puts() - prints string with automatic newline
puts("This is line 1");
puts("This is line 2");
// putchar() - prints single character
printf("Characters: ");
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
putchar('\n');
// Combining different functions
printf("Using printf: ");
printf("Hello ");
printf("World\n");
puts("Using puts:");
puts("Hello World");
return 0;
}
Output:
This is line 1
This is line 2
Characters: Hello
Using printf: Hello World
Using puts:
Hello World