Back to C Programming
2026-07-127 min read

printf() (C Programming)

Learn printf() (C Programming) step by step with clear examples and exercises.

Why This Matters

Welcome to an extensive guide on the printf() function in C programming! This tutorial aims to provide you with a thorough understanding of how to use this powerful tool for outputting formatted text, going beyond the basics covered by other tutorial sites like Programiz, GeeksforGeeks, and TutorialsPoint.

Why This Matters

The printf() function is indispensable in C programming as it enables you to print formatted data, such as integers, floating-point numbers, strings, and more. Mastering its syntax, usage, and various format specifiers is essential for solving real-world problems, acing coding interviews, and debugging common issues that arise during your programming journey.

Prerequisites

Before diving into the printf() function, it's important to have a strong foundation in the following topics:

  1. Basic C syntax: variables, data types, operators, control structures (if-else, loops)
  2. Understanding standard input/output (stdio): scanf(), putchar(), and file I/O
  3. Familiarity with basic data types and their representation in C: integers, floating-point numbers, characters, and strings
  4. Comprehension of pointers and arrays
  5. Understanding the difference between value type and reference type variables

Core Concept

Syntax

The syntax for the printf() function is as follows:

#include <stdio.h>
int printf(const char *format, ...);

Here, format is a string containing placeholders (also known as format specifiers) that control the formatting of subsequent arguments. The ellipsis (...) indicates that you can pass any number of additional arguments to be printed according to the format string.

Format Specifiers

Format specifiers in printf() begin with a percent sign (%) and are followed by a character indicating the type of data to be formatted. Some common format specifiers include:

  • %d: for printing integers
  • %i: an alias for %d, can print integers in any base (octal, hexadecimal)
  • %u: for unsigned integers
  • %o: for octal numbers
  • %x and %X: for hexadecimal numbers (lowercase and uppercase respectively)
  • %f: for floating-point numbers
  • %e and %E: for floating-point numbers in scientific notation (lowercase and uppercase respectively)
  • %g and %G: for choosing the best format between %e and %f based on the value's magnitude
  • %a: for printing floating-point numbers in hexadecimal floating-point format
  • %c: for individual characters
  • %s: for strings
  • %p: for pointers (addresses)
  • %%: for printing a literal percent sign

Example

#include <stdio.h>
int main() {
int num = 10;
unsigned int num_u = 255;
float pi = 3.14159;
double num3 = 123456789.123456;
char str[] = "Hello, World!";
void* ptr = &num;

printf("Integer: %d\n", num);
printf("Unsigned integer: %u\n", num_u);
printf("Octal number: %o\n", num);
printf("Hexadecimal number (lowercase): %x\n", num);
printf("Hexadecimal number (uppercase): %X\n", num);
printf("Floating-point number: %.6f\n", pi);
printf("Floating-point number in scientific notation (10, 6): %.10e\n", num3);
printf("String: %s\n", str);
printf("Character at index 7: %c\n", str[7]);
printf("Pointer (address): %p\n", ptr);
printf("Literal percent sign: %%\n");

return 0;
}

Output:

Integer: 10
Unsigned integer: 255
Octal number: 14
Hexadecimal number (lowercase): a
Hexadecimal number (uppercase): A
Floating-point number: 3.141590
Floating-point number in scientific notation (10, 6): 1.234568e+08
String: Hello, World!
Character at index 7: !
Pointer (address): 0x7ffeeebffa0c
Literal percent sign: %

Worked Example

Let's look at deeper into the printf() function by exploring a more complex example that demonstrates various format specifiers and their usage.

#include <stdio.h>
int main() {
int num1 = 123456789;
unsigned long long num2 = 0x123456789abcdef0ULL;
float num2_f = 0.123456789f;
double num3 = 123456789.123456;
char str[] = "Welcome to C Programming!";
wchar_t wstr[] = L"Welcome to Wide Character Programming!";

printf("Integer: %d\n", num1);
printf("Unsigned long long integer (hexadecimal): %llx\n", num2);
printf("Floating-point number: %.6f\n", num2_f);
printf("Floating-point number: %.6lf\n", num3);
printf("String: %s\n", str);
printf("Wide character string (L): %ls\n", wstr);
printf("Character at index 7: %c\n", str[7]);
printf("Width specifier (minimum field width 10): %10d\n", num1);
printf("Precision specifier (fixed decimal places 6): %.6f\n", num2_f);
printf("Precision and width specifiers (fixed decimal places 6, minimum field width 15): %15.6f\n", num2_f);
printf("String prefix: %#x\n", str[0]);
printf("String suffix: %s\n", str + strlen(str) - 7);

return 0;
}

Output:

Integer: 123456789
Unsigned long long integer (hexadecimal): 123456789abcdef0
Floating-point number: 1.234568
Floating-point number: 1.234568
String: Welcome to C Programming!
Wide character string (L): Welcome to Wide Character Programming!
Character at index 7: !
Width specifier (minimum field width 10): 123456789
Precision specifier (fixed decimal places 6): 0.123457
Precision and width specifiers (fixed decimal places 6, minimum field width 15): 0x123456789abcdef0
String prefix: 57687065
String suffix: Programming!

Common Mistakes

Forgetting the Semicolon

One of the most common mistakes when learning printf() is forgetting to include a semicolon at the end of the function call. Always remember to add a semicolon after the closing parenthesis.

// Incorrect: printf(format, arguments);
printf(format, arguments); // Compile-time error

// Correct: printf(format, arguments);
printf(format, arguments); // Function call

Using the Wrong Format Specifier

Another common mistake is using the wrong format specifier for a specific data type. Make sure to use the appropriate format specifier for each data type to avoid unexpected results or compile errors.

// Incorrect: printf("%d %f", num, pi); // Compile error
printf("%d %f", num, pi); // Correct with the wrong format specifiers

// Correct: printf("%d %lf", num, pi); // Correct format specifiers for integers and floating-point numbers
printf("%d %lf", num, pi); // Function call

Neglecting to Include

Another common mistake is forgetting to include the ` header file, which contains the definition for the printf()` function.

// Incorrect: int main() { printf("Hello, World!"); }
#include <stdio.h> // Correct
int main() { printf("Hello, World!"); } // Function call

Misunderstanding the Purpose of the Ellipsis (...)

The ellipsis in the printf() function is used to indicate that a variable number of arguments can be passed. It should not be used to pass an array or pointer to an array as an argument, as this can lead to unexpected behavior and security vulnerabilities.

// Incorrect: int arr[] = {1, 2, 3}; printf("%d %d %d", arr); // Compile error
int arr[] = {1, 2, 3}; // Correct
printf("%d %d %d", arr[0], arr[1], arr[2]); // Function call with individual elements

Practice Questions

  1. Write a program that prints the following using printf():
  • An integer with commas (123456789)
  • A floating-point number with fixed decimal places (6) (0.123456789f)
  • A floating-point number with scientific notation (10, 6) (123456789.123456)
  • A string ("Welcome to C Programming!")
  • The length of the string above
  • The binary representation of an integer (10101010)
  • The octal representation of an integer (123)
  • The hexadecimal representation of an integer (ABC)
  • An unsigned long long integer (hexadecimal) (9876543210123456ULL)
  • A wide character string ("Welcome to Wide Character Programming!")
  • The first and last characters of a string
  • The middle three characters of a string
  • The index of the first occurrence of the substring "to" in a string
  • The number of occurrences of the substring "C" in a string
  • The maximum value that can be stored in an unsigned char variable
  • The minimum value that can be stored in a signed char variable
  • The maximum and minimum values that can be stored in a short integer variable
  • The maximum and minimum values that can be stored in an unsigned short integer variable
  • The maximum and minimum values that can be stored in an integer variable
  • The maximum and minimum values that can be stored in an unsigned integer variable
  • The maximum and minimum values that can be stored in a long integer variable
  • The maximum and minimum values that can be stored in an unsigned long integer variable
  • The maximum and minimum values that can be stored in a long long integer variable
  • The maximum and minimum values that can be stored in an unsigned long long integer variable
  • The maximum and minimum values that can be stored in a float variable
  • The maximum and minimum values that can be stored in a double variable
  • The maximum and minimum values that can be stored in a long double variable
  1. Write a program that takes two integers as input using scanf() and prints their sum, difference, product, quotient (if the second number is non-zero), and remainder (if the second number is non-zero) using printf().
  1. Write a program that reads a line of text from the user using getchar(), counts the number of words in the line, and prints the first and last words using printf().

FAQ

Q: What happens if I pass too few arguments to printf()?

A: If you provide fewer arguments than specified by the format string, printf() will print garbage values or compile with an error.

Q: Can I use printf() for file output instead of stdout?

A: Yes! You can redirect the standard output (stdout) to a file using the redirection operator (>) in the terminal or by specifying a file pointer as the first argument to printf().

Q: How do I print a newline character using printf