printf() Function

The printf() function in C writes a formatted string to the standard output. It processes a format string that can include plain text and format specifiers, replacing each specifier with the corresponding argument to produce the desired output.


Syntax of printf()

</>
Copy
int printf(const char *format, ...);

Parameters

ParameterDescription
formatA C string that contains the text to be written to stdout. It can include embedded format specifiers, which are placeholders replaced by the values of subsequent arguments.

The format specifiers follow the structure %[flags][width][.precision][length]specifier, which allows fine control over the display of various data types such as integers, floats, characters, and strings.

Return Value

On success, printf() returns the total number of characters written to stdout. In case of an error, a negative value is returned.


Examples for printf()

Example 1: Printing a Simple String

This example demonstrates printing a simple string using printf().

Program

</>
Copy
#include <stdio.h>

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

Explanation:

  1. The program includes the stdio.h header file for standard input and output functions.
  2. The printf() function prints the string “Hello, World!” followed by a newline.
  3. The program returns 0 to indicate successful execution.

Program Output:

Hello, World!

Example 2: Printing Integers with Format Specifiers

This example shows how to print integers using the %d or %i format specifiers.

Program

</>
Copy
#include <stdio.h>

int main() {
    int a = 10, b = 20;
    printf("The values are %d and %d\n", a, b);
    return 0;
}

Explanation:

  1. Two integer variables, a and b, are declared and initialized with values 10 and 20.
  2. The printf() function uses %d to format these integers.
  3. The formatted string is printed to show both values.

Program Output:

The values are 10 and 20

Example 3: Formatting Floating-Point Numbers

This example demonstrates printing a floating-point number using the %f specifier with precision control.

Program

</>
Copy
#include <stdio.h>

int main() {
    double num = 123.456789;
    printf("Formatted number: %.2f\n", num);
    return 0;
}

Explanation:

  1. A double variable num is initialized with 123.456789.
  2. The %.2f specifier formats the number to two decimal places.
  3. The formatted number is printed as “123.46”.

Program Output:

Formatted number: 123.46

Example 4: Using Character and String Specifiers

This example illustrates printing a character using %c and a string using %s.

Program

</>
Copy
#include <stdio.h>

int main() {
    char ch = 'Z';
    char str[] = "Sample String";
    printf("Character: %c, String: %s\n", ch, str);
    return 0;
}

Explanation:

  1. A character variable ch is initialized to ‘Z’.
  2. A string str is initialized with “Sample String”.
  3. The %c and %s specifiers print the character and the string respectively.

Program Output:

Character: Z, String: Sample String

Example 5: Printing Unsigned Integers and Hexadecimals

This example shows how to print an unsigned integer using %u and its hexadecimal representation using %x and %X.

Program

</>
Copy
#include <stdio.h>

int main() {
    unsigned int num = 3735928559U;
    printf("Unsigned: %u\n", num);
    printf("Hex (lowercase): %x\n", num);
    printf("Hex (uppercase): %X\n", num);
    return 0;
}

Explanation:

  1. An unsigned integer num is initialized with the value 3735928559U (0xDEADBEEF).
  2. The %u specifier prints the number in unsigned decimal format.
  3. The %x and %X specifiers print the number in hexadecimal (lowercase and uppercase respectively).

Program Output:

Unsigned: 3735928559
Hex (lowercase): deadbeef
Hex (uppercase): DEADBEEF

Example 6: Using Width and Precision Specifiers

This example demonstrates controlling output width and precision for numbers and strings.

Program

</>
Copy
#include <stdio.h>

int main() {
    int num = 42;
    char str[] = "Hello";
    printf("Number with width 5: %5d\n", num);
    printf("String with precision 3: %.3s\n", str);
    return 0;
}

Explanation:

  1. The integer num is printed with a minimum field width of 5, padding with spaces if necessary.
  2. The string str is printed with a precision of 3, meaning only the first 3 characters are shown.
  3. This demonstrates control over field width and precision in formatted output.

Program Output:

Number with width 5:    42
String with precision 3: Hel

Example 7: Printing Floating-Point Numbers in Scientific Notation

This example prints a floating-point number in scientific notation using the %e specifier.

Program

</>
Copy
#include <stdio.h>

int main() {
    double num = 12345.6789;
    printf("Scientific notation: %e\n", num);
    return 0;
}

Explanation:

  1. A double variable num is initialized with 12345.6789.
  2. The %e specifier formats the number in scientific (exponential) notation.
  3. The output displays the number in exponent format.

Program Output:

Scientific notation: 1.234568e+04

Example 8: Using Flags for Padding and Sign Display

This example demonstrates the use of flags such as +, -, and 0 to control the formatting of numeric output.

Program

</>
Copy
#include <stdio.h>

int main() {
    int num = 7;
    printf("Signed with flag +: %+d\n", num);
    printf("Left-justified within 5 spaces: %-5dEnd\n", num);
    printf("Zero-padded within 5 spaces: %05d\n", num);
    return 0;
}

Explanation:

  1. The integer num is printed with a leading plus sign using the + flag.
  2. It is printed left-justified in a 5-character field using the - flag.
  3. It is also printed with zero-padding in a 5-character field using the 0 flag.

Program Output:

Signed with flag +: +7
Left-justified within 5 spaces: 7    End
Zero-padded within 5 spaces: 00007

Example 9: Printing Pointers and Special Characters

This example demonstrates printing a pointer address using %p and printing a literal percent sign using %%.

Program

</>
Copy
#include <stdio.h>

int main() {
    int var = 100;
    printf("Pointer address: %p\n", (void *)&var);
    printf("Print a percent sign: %%\n");
    return 0;
}

Explanation:

  1. The address of the variable var is printed using the %p specifier.
  2. A literal percent sign is printed using the %% escape sequence.

Program Output:

Pointer address: 0x7ffc3361f084
Print a percent sign: %

Example 10: Advanced Usage with Multiple Data Types

This comprehensive example demonstrates printing multiple data types in a single printf() call using various format specifiers.

Program

</>
Copy
#include <stdio.h>

int main() {
    int integer = 42;
    double floating = 3.14159;
    char character = 'A';
    char string[] = "Test";

    printf("Integer: %d, Double: %.2f, Character: %c, String: %s\n", integer, floating, character, string);
    return 0;
}

Explanation:

  1. An integer, a double, a character, and a string are declared and initialized.
  2. The printf() function uses the format specifiers %d, %.2f, %c, and %s to print each value appropriately.
  3. This example shows how to combine various types into a single formatted output.

Program Output:

Integer: 42, Double: 3.14, Character: A, String: Test