# 04 - C Output - Display Text and Values

**URL:** https://buzzly.harislab.tech/t/04-c-output-display-text-and-values/192
**Category:** C Programming Tutorials
**Tags:** c, basics
**Created:** [April 21, 2026, 1:42pm UTC](https://buzzly.harislab.tech/t/04-c-output-display-text-and-values/192 "2026-04-21T13:42:17Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![Haris](https://buzzly.harislab.tech/user_avatar/buzzly.harislab.tech/haris/32/7_2.png) [@Haris](https://buzzly.harislab.tech/u/Haris)
#### Post date: [April 21, 2026, 1:42pm UTC](https://buzzly.harislab.tech/t/04-c-output-display-text-and-values/192/1 "2026-04-21T13:42:18Z")

</div>

**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

```c
#include <stdio.h>

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

```

**Output:**

```auto
Hello, World!

```

* * *

## The printf() Function

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

### Basic Syntax

```c
printf("text to display");

```

### Examples

#### Example 1: Simple Text

```c
#include <stdio.h>

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

```

**Output:**

```auto
I am learning C!

```

#### Example 2: Multiple printf Statements

```c
#include <stdio.h>

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

```

**Output:**

```auto
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.

```c
#include <stdio.h>

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

```

**Output:**

```auto
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

```c
#include <stdio.h>

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

```

**Output:**

```auto
I am 25 years old

```

### Example: Printing a Float

```c
#include <stdio.h>

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

```

**Output:**

```auto
Price: $19.990000

```

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

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

```

**Output:**

```auto
Price: $19.99

```

* * *

## Practical Examples

### Example 1: Personal Information

```c
#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:**

```auto
Name: John
Age: 25
Height: 5.9

```

### Example 2: Simple Math Results

```c
#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:**

```auto
10 + 5 = 15
15 / 4 = 3.75

```

* * *

## Multiple Values

Print multiple values with one printf:

```c
#include <stdio.h>

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

```

**Output:**

```auto
x = 10, y = 20

```

* * *

## Common Mistakes

### Mistake 1: Forgetting the Newline

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

```

**Fix:**

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

```

### Mistake 2: Wrong Format Specifier

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

```

**Fix:**

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

```

### Mistake 3: Missing Variable

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

```

**Fix:**

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

```

* * *

## Quick Reference: Format Specifiers

```c
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`:

```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:

```bash
gcc output.c -o output
./output

```

* * *

### 📚 Navigation

| Previous | Next |
| --- | --- |
| [← C Syntax](https://buzzly.harislab.tech/t/03-c-syntax-understanding-program-structure/191/1) | [C Comments →](https://buzzly.harislab.tech/t/05-c-comments-writing-helpful-code-notes/193/1) |

* * *

## ❓ Have Questions?

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

* * *

---

_[View the full topic](https://buzzly.harislab.tech/t/04-c-output-display-text-and-values/192)._
