โœ๏ธ C Basics โ€“ Syntax, Input, Tokens
Estimated reading: 3 minutes 275 views

C Output (printf) โ€“ Displaying Output in C Programs

Introduction โ€“ What Is printf() in C?
In C programming, the printf() function is used to display output on the console. It is part of the standard I/O library and one of the most frequently used functions in C. With printf(), you can display strings, variables, formatted numbers, and even characters in a structured way.

In this guide, youโ€™ll learn:

  • The syntax and usage of printf() in C
  • How to print variables with format specifiers
  • Examples for printing integers, floats, characters, and strings
  • Formatting techniques for cleaner output

Syntax of printf()

printf("format string", variable1, variable2, ...);
  • "format string" contains text and format specifiers.
  • Variables following the string are substituted into the format specifiers.

Example:

int age = 25;
printf("My age is %d years.", age);

Output:

My age is 25 years.

Format Specifiers in printf()

SpecifierDescriptionExample Output
%d or %iInteger42
%fFloating-point number3.141593
%.2fFloat with 2 decimals3.14
%cCharacterA
%sStringHello
%uUnsigned int42
%x / %XHexadecimal (lower/upper)2a or 2A
%oOctal number52
%%Percent symbol%

Examples of Using printf()

1. Printing an Integer

int num = 100;
printf("Number: %d\n", num);

Output:

Number: 100

2. Printing a Float

float pi = 3.14159;
printf("Value of pi: %.2f\n", pi);

Output:

Value of pi: 3.14

3. Printing a Character

char grade = 'A';
printf("Grade: %c\n", grade);

Output:

Grade: A

4. Printing a String

char name[] = "John";
printf("Name: %s\n", name);

Output:

Name: John

Formatting Output with printf()

  • Use \n for a newline:
printf("Hello\nWorld");

Output:

Hello
World
  • Use \t for tab spacing:
printf("Name:\tJohn\nAge:\t25");

Output:

Name:   John
Age:    25
  • Align numbers with field width:
printf("%5d\n", 12);  // Right-aligned in 5-character width

Output:

   12

Summary โ€“ Recap & Next Steps

The printf() function is a powerful and flexible tool for formatting and displaying output in C programs. It supports a wide range of data types and customization options to control output presentation.

Key Takeaways:

  • printf() is used for displaying output in C.
  • Format specifiers help print variables of different types.
  • Escape sequences like \n and \t enhance output formatting.
  • You can print integers, floats, strings, characters, and even percent signs.

Real-World Relevance:

Whether you’re printing logs, debugging values, or building console-based applications, mastering printf() is a must-have skill for every C programmer.


Frequently Asked Questions (FAQ)

What does printf() do in C?

printf() is a standard library function that displays text and variables to the terminal or console. It allows formatted output using format specifiers like %d, %s, and %f.


What header file is required for printf()?

You must include the standard input-output header:

#include <stdio.h>

Without it, the compiler may throw an โ€œundefined referenceโ€ error.


How do I print a percent symbol using printf()?

Use %% inside the format string:

printf("Progress: 100%%");

Output:

Progress: 100%

What does %0.2f mean in printf()?

It formats a float to 2 decimal places. For example:

printf("%.2f", 3.14159);

Output:

3.14

Can I use multiple variables in one printf()?

Yes. Just ensure the format specifiers match the order and type of the variables:

int a = 5, b = 10;
printf("A = %d, B = %d", a, b);

What is the difference between %d and %i?

Both are used for printing integers and behave the same in printf(). However, %i has different behavior in scanf() (for input).


Share Now :
Share

๐Ÿ“ค C Output (printf)

Or Copy Link

CONTENTS
Scroll to Top