
Output in C
?? Output in C
In C, output (like printing to the screen) is typically done using the printf()
function from the stdio.h
header.
? Basic Syntax
int main() { printf("Hello, World!\n"); return 0;}
? Output:
Hello, World!
? Printing Variables
int age = 21;float height = 5.9;char grade = 'A';printf("Age: %d\n", age);printf("Height: %.1f\n", height);printf("Grade: %c\n", grade);
? Format Specifiers
Format | Data Type | Example Output |
---|---|---|
%d | Integer | Age: 21 |
%f | Float/Double | Height: 5.900000 |
%.1f | Float (1 decimal) | Height: 5.9 |
%c | Character | Grade: A |
%s | String | Hello |
%p | Pointer/Address | 0x7ffe... |
? Multiple Variables
int x = 5, y = 10;printf("x = %d, y = %d\n", x, y);
? Escape Sequences
Sequence | Meaning |
---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
\" | Double quote |
? Example with All
#include <stdio.h>int main() { printf("Name:\t\"John\"\n"); printf("Age:\t%d\n", 20); printf("Score:\t%.2f%%\n", 92.5); // %% prints a literal % return 0;}