Back to C Programming
2026-07-125 min read

C - printf Function in C

Learn C - printf Function in C step by step with clear examples and exercises.

Why This Matters

Understanding and mastering the printf function is crucial for any C programmer. It allows developers to output formatted data to the console, which is essential for creating interactive applications, debugging code, and solving complex problems. Familiarity with printf can help you excel in coding interviews and real-world programming scenarios.

Prerequisites

Before diving into printf, ensure that you have a good grasp of C basics, including variables, data types, control structures like loops and conditional statements, and the standard input function, scanf. Familiarity with basic file I/O using functions such as fprintf and fscanf will also be helpful.

Core Concept

Understanding printf

printf is a library function in C that allows you to print formatted output to the console. It belongs to the stdio.h (Standard Input/Output) library and must be included at the beginning of your program with the following line:

#include <stdio.h>

The basic syntax for using printf is as follows:

printf("Format String", arguments);

The format string contains placeholders (indicated by %) that correspond to the types of arguments you want to print. Each placeholder is followed by a conversion specifier, which tells printf how to interpret and display the corresponding argument.

Common Format Specifiers

Here are some common format specifiers used with printf:

  • %d or %i for integers (signed)
  • %u for unsigned integers
  • %f for floating-point numbers
  • %c for characters
  • %s for strings
  • %o for octal numbers
  • %x or %X for hexadecimal numbers (lowercase and uppercase, respectively)
  • %% to print a literal % character

Example

#include <stdio.h>

int main() {
int num = 42;
float pi = 3.14159;
char letter = 'A';
char string[] = "Hello, World!";

printf("The integer is: %d\n", num);
printf("The floating-point number is: %.2f\n", pi);
printf("The character is: %c\n", letter);
printf("The string is: %s\n", string);
printf("The octal number is: %o\n", num);
printf("The hexadecimal number (lowercase) is: %x\n", num);
printf("The hexadecimal number (uppercase) is: %X\n", num);
printf("The literal %% character is: %%\n");

return 0;
}

Precision and Width

In addition to format specifiers, you can also control the precision and width of your output using optional modifiers. The precision modifier (.) specifies the number of digits after the decimal point for floating-point numbers, while the width modifier (0) defines the minimum field width for all types.

printf("The floating-point number is: %.3f\n", pi); // Outputs "The floating-point number is: 3.142"
printf("The integer is: %5d\n", num); // Outputs "The integer is: 42" (minimum field width of 5)

Worked Example

Let's create a simple program that calculates the area of a circle using printf. We will ask the user for the radius and then output the calculated area.

#include <stdio.h>
#include <math.h>

int main() {
double radius, area;

printf("Enter the radius of the circle: ");
scanf("%lf", &radius);

area = M_PI * pow(radius, 2);
printf("The area of the circle with radius %.2lf is %.4lf\n", radius, area);

return 0;
}

Common Mistakes

  1. Forgetting to include `` or other necessary headers.
  2. Incorrectly specifying format specifiers for the provided arguments (e.g., using %d for a floating-point number).
  3. Not accounting for the precision when using floating-point numbers (e.g., specifying %.0f instead of %.2f).
  4. Using an outdated version of printf that doesn't support newer format specifiers like %lf.
  5. Failing to include a newline character (\n) when expecting output on separate lines.
  6. Not handling input errors, such as when the user enters non-numeric data with scanf.
  7. Forgetting to initialize variables before using them.
  8. Using an incorrect conversion specifier for a specific data type or format (e.g., using %d instead of %i).
  9. Confusing the precision modifier (.) with the field width modifier (0).
  10. Not understanding the difference between left-justified and right-justified output.

Left-Justified vs Right-Justified Output

By default, printf left-justifies its output within the specified field width. However, you can change this behavior by using a plus sign (+) or a minus sign (-) before the precision modifier. A positive sign will right-align the output, while a negative sign will left-align it.

printf("Left-justified: %.2f\n", pi); // Outputs "Left-justified: 3.14" (left-aligned by default)
printf("Right-justified: %.2f\n", pi); // Outputs "Right-justified: 3.14" (right-aligned with a plus sign)

Practice Questions

  1. Write a program that takes two integers as input and outputs their sum using printf.
  2. Create a program that calculates the average of three floating-point numbers entered by the user.
  3. Write a program that prints the following pattern:
*
**
**
**

(Hint: Use nested loops and printf to print each line.)

  1. Modify the previous example to print the pattern in reverse order (from bottom to top).
  2. Write a program that converts a given decimal number into its binary representation using printf.
  3. Create a program that calculates the factorial of a given integer using printf and recursion.
  4. Write a program that prints the Fibonacci sequence up to a given number using printf.
  5. Modify the worked example to calculate and print the circumference of a circle, given its radius as input.
  6. Create a program that reads a string from the user and counts the number of occurrences of each vowel (a, e, i, o, u) in the string using printf.
  7. Write a program that prints the ASCII table, displaying characters and their corresponding decimal values using printf.

FAQ

What happens if I provide more arguments than format specifiers in printf?

  • If you provide more arguments than format specifiers, the extra arguments will be ignored.

Can I use printf to output binary or hexadecimal numbers directly?

  • Yes, you can use %o for octal numbers and %x or %X for hexadecimal numbers. However, these require additional header files: ` for fixed-width integers and ` for extended integer types.

Is it possible to print a newline character using printf?

  • Yes, you can use \n as a placeholder in the format string to print a newline character.

How do I print a tab character using printf?

  • To print a tab character, use \t as a placeholder in the format string.

Can I control the width and precision of my output using printf?

  • Yes, you can control the width and precision using optional modifiers (. for precision and 0 for width).

How do I handle input errors with scanf?

  • To handle input errors, you should check the return value of scanf after each scan operation. A failure to read the expected number of values indicates an error, such as non-numeric input.

What is the difference between left-justified and right-justified output in printf?

  • By default, printf left-justifies its output within the specified field width. However, you can change this behavior by using a plus sign (+) or a minus sign (-) before the precision modifier. A positive sign will right-align the output, while a negative sign will left-align it.

How do I print a literal % character using printf?

  • To print a literal % character, use two percent signs (%%) in the format string.