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
%dSigned integerint age = 25; printf("%d", age);
%fFloating-point numberfloat price = 9.99; printf("%f", price);
%cSingle characterchar grade = 'A'; printf("%c", grade);
%sStringchar name[] = "John"; printf("%s", name);
%lfDouble precision floating-point numberdouble pi = 3.14159; printf("%lf", pi);
%uUnsigned integerunsigned int num = 50; printf("%u", num);
%oOctal numberint num = 10; printf("%o", num);
%xHexadecimal number (lowercase)int num = 255; printf("%x", num);
%XHexadecimal number (uppercase)int num = 255; printf("%X", num);
%%Prints a percent signprintf("%%");

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
\nNew line
\tHorizontal 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.