04 - C Output - Display Text and Values

Learn how to display output on the screen using printf()


What is this topic?

This guide explains C Output - Display Text and Values in simple terms, what it does, and how to use it in real C programs.

Why We Need It

  • It helps you write correct and reliable C code.
  • It makes your programs easier to read and maintain.
  • It is used in real projects and interviews.
  • It reduces common beginner mistakes.
  • It builds a strong foundation for advanced topics.

Use Cases

  • Building practical C programs step by step.
  • Solving real coding tasks with clean logic.
  • Preparing for exams, interviews, and projects.
  • Understanding and improving existing C code.

What is Output?

Output is information your program sends to the screen. The most common way to create output in C is using the printf() function.

Simple Output Example

#include <stdio.h>

int main() {
    printf("Hello, World!");
    return 0;
}

Output:

Hello, World!

The printf() Function

printf() stands for “print formatted” and displays text on the screen.

Basic Syntax

printf("text to display");

Examples

Example 1: Simple Text

#include <stdio.h>

int main() {
    printf("I am learning C!");
    return 0;
}

Output:

I am learning C!

Example 2: Multiple printf Statements

#include <stdio.h>

int main() {
    printf("First line");
    printf("Second line");
    return 0;
}

Output:

First lineSecond line

Notice how they printed on the same line? We need to add a newline!


Newlines: \n

The \n character creates a new line.

#include <stdio.h>

int main() {
    printf("First line\n");
    printf("Second line\n");
    return 0;
}

Output:

First line
Second line

More Escape Sequences

Sequence Effect
\n New line
\t Tab (space)
\\ Backslash
\" Double quote
\' Single quote

Printing Numbers and Variables

Use format specifiers to print numbers:

Format Specifiers

Specifier Data Type Example
%d Integer printf("%d", 42);
%f Float (decimal) printf("%f", 3.14);
%c Character printf("%c", 'A');
%s String printf("%s", "text");

Example: Printing an Integer

#include <stdio.h>

int main() {
    int age = 25;
    printf("I am %d years old\n", age);
    return 0;
}

Output:

I am 25 years old

Example: Printing a Float

#include <stdio.h>

int main() {
    float price = 19.99;
    printf("Price: $%f\n", price);
    return 0;
}

Output:

Price: $19.990000

Notice the extra zeros! To fix this, use %.2f:

printf("Price: $%.2f\n", price);  // Shows 2 decimal places

Output:

Price: $19.99

Practical Examples

Example 1: Personal Information

#include <stdio.h>

int main() {
    char name[] = "John";
    int age = 25;
    float height = 5.9;
    
    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    return 0;
}

Output:

Name: John
Age: 25
Height: 5.9

Example 2: Simple Math Results

#include <stdio.h>

int main() {
    int result = 10 + 5;
    printf("10 + 5 = %d\n", result);
    
    float division = 15.0 / 4.0;
    printf("15 / 4 = %.2f\n", division);
    return 0;
}

Output:

10 + 5 = 15
15 / 4 = 3.75

Multiple Values

Print multiple values with one printf:

#include <stdio.h>

int main() {
    int x = 10;
    int y = 20;
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

Output:

x = 10, y = 20

Common Mistakes

Mistake 1: Forgetting the Newline

printf("Line 1");
printf("Line 2");  // Prints on same line

Fix:

printf("Line 1\n");
printf("Line 2\n");

Mistake 2: Wrong Format Specifier

int age = 25;
printf("Age: %f\n", age);  // ✗ %f is for floats, not ints

Fix:

printf("Age: %d\n", age);  // ✓ %d is for integers

Mistake 3: Missing Variable

printf("My number: %d\n");  // ✗ No value provided

Fix:

printf("My number: %d\n", 42);  // ✓ Value provided

Quick Reference: Format Specifiers

printf("%d", integer);        // Integer
printf("%.2f", decimal);      // Decimal (2 places)
printf("%c", character);      // Single character
printf("%s", string);         // String of text

Practice Exercise

Create a file called output.c:

#include <stdio.h>

int main() {
    printf("=== Welcome ===\n");
    printf("My name is John\n");
    printf("I love programming!\n");
    
    int score = 95;
    float grade = 4.0;
    printf("\nScore: %d\n", score);
    printf("Grade: %.1f\n", grade);
    
    return 0;
}

Compile and run:

gcc output.c -o output
./output

:books: Navigation


:red_question_mark: Have Questions?

Comment below and we’ll help you! Struggling with printf? Share your questions below!