Skip to content

Output in C: Displaying Information

In C programming, output refers to showing information to the user. The most common way to display output is using the printf function. This function prints text and variable values to the console (the terminal or command prompt). Let's explore how to use printf for basic output.

Printing to the Console: The printf Function

The printf function allows you to print text and data to the console. Here's the basic syntax:

c
printf("format string", arguments);
  • format string: A string that determines how the output will be formatted.
  • arguments: Values to be displayed, matching the format specifiers in the format string.

Example

c
#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.9;
    char grade = 'A';

    printf("Age: %d\n", age);          // Prints an integer
    printf("Height: %.1f meters\n", height); // Prints a floating-point number
    printf("Grade: %c\n", grade);     // Prints a character

    return 0;
}

Format Specifiers

  • %d : Displays an integer.
  • %f : Displays a floating-point number.
  • %c : Displays a single character.
  • %s : Displays a string of characters.

Example

c
#include <stdio.h>

int main() {
    int number = 10;
    printf("Number: %d\n", number); // Prints the number

    char letter = 'B';
    printf("Letter: %c\n", letter); // Prints the letter

    return 0;
}

Printing Strings and Special Characters

You can also print strings and use special characters in your output. Special characters are represented by escape sequences:

  • \n : Newline (moves to the next line)
  • \t : Tab (adds a horizontal space)
  • \\ : Backslash (prints a backslash)
  • \" : Double quote (prints a double quote)

Example

c
#include <stdio.h>

int main() {
    printf("Hello, World!\n");          // Prints a new line
    printf("Name:\tAlice\n");            // Prints a tab
    printf("Quote: \"To be or not to be\"\n"); // Prints a double quote

    return 0;
}

Practice Exercise

Create a simple program that uses printf to display different types of data, such as integers, floating-point numbers, and characters. Experiment with format specifiers and escape sequences to see how they affect the output.


Output functions are essential for interacting with users and showing data in your programs. They provide the means to display results and feedback effectively. Next, let’s dive into the Comments chapter to learn how to add meaningful notes and explanations to your code.