C Output (Print Text, New Lines)
In C programming, output functions are used to display information to the user. The most commonly used function for output is printf()
from the stdio.h
library.
Key Topics
1. Using printf()
The printf()
function is used to output text and variables to the console.
Example: Printing Text
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Output:
Hello, World!
Code Explanation: The printf()
function prints the string "Hello, World!" followed by a new line character \n
.
2. Format Specifiers
Format specifiers allow you to print variables of different data types. Below is a table of common format specifiers:
Specifier | Data Type | Example |
---|---|---|
%d | Signed integer | int age = 25; printf("%d", age); |
%f | Floating-point number | float price = 9.99; printf("%f", price); |
%c | Single character | char grade = 'A'; printf("%c", grade); |
%s | String | char name[] = "John"; printf("%s", name); |
%lf | Double precision floating-point number | double pi = 3.14159; printf("%lf", pi); |
%u | Unsigned integer | unsigned int num = 50; printf("%u", num); |
%o | Octal number | int num = 10; printf("%o", num); |
%x | Hexadecimal number (lowercase) | int num = 255; printf("%x", num); |
%X | Hexadecimal number (uppercase) | int num = 255; printf("%X", num); |
%% | Prints a percent sign | printf("%%"); |
Example: Printing Variables with Format Specifiers
#include <stdio.h>
int main() {
int age = 30;
float height = 5.8;
char grade = 'A';
char name[] = "Alice";
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Height: %.1f feet\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Name: Alice Age: 30 Height: 5.8 feet Grade: A
Code Explanation: This program demonstrates the use of different format specifiers to print variables of various data types. The \n
escape sequence is used to move to a new line after each output.
3. Escape Sequences
Escape sequences are used to represent special characters in strings. Common escape sequences include:
Escape Sequence | Description |
---|---|
\n | New line |
\t | Horizontal tab |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
Example: Using Escape Sequences
printf("First Line\nSecond Line\n");
printf("Column1\tColumn2\tColumn3\n");
printf("She said, \"Hello!\"\n");
Output:
First Line Second Line Column1 Column2 Column3 She said, "Hello!"
Code Explanation: The escape sequences \n
, \t
, and \"
are used to format the output with new lines, tabs, and to include double quotes in the string.
Best Practices
- Use appropriate format specifiers matching the data type.
- Include escape sequences to format the output neatly.
- Test your output to ensure it displays as intended.
Don'ts
- Don't mismatch format specifiers and variables; this can lead to unexpected results.
- Don't forget to include the
stdio.h
header file. - Don't omit escape sequences when they are needed for formatting.
Key Takeaways
- The
printf()
function is essential for displaying output in C programs. - Format specifiers allow you to print variables of different data types effectively.
- Escape sequences help you format your output for better readability.