C Output

C Tutorial

1. Using printf() for C Output

The most common way to display output in C is with the printf() function from the stdio.h library.

Basic Syntax:

printf("format string", values);
  • "format string" → The text you want to display.

  • values → Variables or constants to display.

  • Placeholders in the format string are replaced by variable values.


2. Format Specifiers

Format specifiers tell printf() what type of data to print:

Specifier Type Example
%d Integer int x = 10
%f Float float pi = 3.14
%c Character char ch = ‘A’
%s String char str[] = “Hi”
%lf Double double d = 3.1415
%u Unsigned int unsigned int u = 20

3. Examples of Output

Example 1: Display text only


 

Output:

Hello, World!

Example 2: Display variables


 

Output:

Age: 25
Height: 5.9
Grade: A

Example 3: Multiple variables


 

Output:

a = 10, b = 20

4. Escape Sequences

Special characters that give formatting instructions:

Escape Sequence Meaning
\n New line
\t Horizontal tab
\\ Backslash
\" Double quote
\' Single quote

Example:


Output:

Hello World

5. Tips for Using printf()

  1. Always match format specifiers with variable types.

  2. Use %.nf to control decimal places for floats/doubles.

  3. Combine text and variables in a single statement for cleaner code.

You may also like...