printf() | Examples and Best Practices

Here, we are going to learn about the printf(), its usages with different types of format specifiers in the C programming language?
Submitted by IncludeHelp, on September 14, 2018 [Last updated : March 15, 2023]

printf() Examples

As we know that, printf() is used to print the text and value on the output device, here some of the examples that we wrote to use the printf() in a better way or for an advance programming.

1) Print normal text

printf ("Hello world");

Output

Hello world

2) Print text in new line

printf ("Hello world\nHow are you?");

Output

Hello world
How are you?

3) Print double quote

To print double quote, we use \".

printf("Hello \"World\", How are you?\n");

Output

Hello "World", How are you?

4) Print percentage sign (%)

To print percentage sign/ character, we use %%.

printf ("Hey I got 84.20%% in my final exams\n");

Output

Hey I got 84.20% in my final exams

5) Print octal value

To print octal value, we use %o (It's alphabet o in lowercase)

int num = 255;
printf("num in octal format: %o\n", num);

Output

num in octal format: 377

6) Print hexadecimal value (with lowercase alphabets)

To print hexadecimal value, we use %x (its alphabet 'x' in lowercase) - as we know that a hexadecimal value contains digits from 0 to 9 and alphabets A to F, "%x" prints the alphabets in lowercase.

int num = 255;
printf ("num in hexadecimal format(lowercase) : %x\n", num);

Output

num in hexadecimal format(lowercase) : ff

7) Print hexadecimal value (with uppercase alphabets)

To print hexadecimal value, we use %X (it's alphabet X in uppercase) - as we know that a hexadecimal value contains digits from o to 9 and alphabets A to F, "%X" prints the alphabets in uppercase.

int num = 255;
printf ("num in hexadecimal format(uppercase) : %X\n", num);

Output

num in hexadecimal format(uppercase) : FF

8) Print long string using \ (slash)

If there is a long string, that you want to print with a single printf() with two or more lines, we can use slash (\).

printf ("Hello world, how are you?\
I love C programing language.\n");

Output

Hello world, how are you?    I love C programing language.

9) Print backslash (\)

To print backslash (\), we use double backslash (\\).

printf ("The file is store at c:\\files\\word_files\n");

Output

The file is store at c:\files\word_files

10) To get total number of printed characters

We can also get the total number of printed character using printf(), printf() returns the total number of printed character, that we can store in a variable and print.

int len = 0;
len = printf ("Hello\n");
printf ("Length: %d\n", len);

Output

Hello
Length: 6

11) Print integer value 5 digit left padded with 0

To print value with 0 padded in 5 digits, we can use "%05d".

int num = 255;
printf ("num (padded): %05d\n", num);

Output

num (padded): 00255

12) Print text with left and right padding

To print left padded text with space, we use "%20s" - Here, 20 is the number of characters, if string contains 5 characters then 15 spaces will be added at the left of the text.

Similarly, to print right padded text with space, we use a flag "-" like "%-20s" - Here, 20 is the number of characters, if string contains 5 characters then 15 spaces will be added at the right of the text.

printf ("str1=\"%20s\", str2=\"%-20s\"\n", "Hello", "World");

Output

str1="               Hello", str2="World               "

13) Print float value to specified number of digits after the decimal point

To print float value with specified number of digits after the decimal, we use "%.2f". Here, 2 is the number of digits after the decimal point.

float val = 1.234567;
printf("val = %.2f\n", val);

Output

val = 1.23

14) Print integer value with "%i" format specifier

While printing the value "%d" and "%i" are same, they are used to print an integer value, we can also print the integer value by using "%i".

int num = 255;
printf("num = %i \n", num);

Output

num = 255

15) “%p” can be used to print the address of a variable

Since, address of the variable is a large hexadecimal value - to print it we should not (or cannot) use "%d", "%i" etc. To print an address of a variable, we must use "%p".

int num = 255;
printf("Address of num is: %p\n", &num);

Output

Address of num is: 0x7ffca0b5855c

Example with all above printf() statements

#include <stdio.h>

int main()
{
    int num = 255;
    int len = 0;
    float val = 1.234567;
    
    printf("Hello World");
    printf("Hello world\nHow are you?");
    printf("Hello \"World\", How are you?\n"); 
    printf ("Hey I got 84.20%% in my final exams\n");
    printf("num in octal format: %o\n", num);
    printf ("num in hexadecimal format(lowercase) : %x\n", num);
    printf ("num in hexadecimal format(uppercase) : %X\n", num);
    printf ("Hello world, how are you?\
    I love C programing language.\n");
    printf ("The file is store at c:\\files\\word_files\n");
    len = printf ("Hello\n");
    printf ("Length: %d\n", len);
    printf ("num (padded): %05d\n", num);
    printf ("str1=\"%20s\", str2=\"%-20s\"\n", "Hello", "World");
    printf("val = %.2f\n", val);
    printf("num = %i \n", num);
    printf("Address of num is: %p\n", &num);
           
    return 0;
}

Output

Hello WorldHello world
How are you?Hello "World", How are you?
Hey I got 84.20% in my final exams
num in octal format: 377
num in hexadecimal format(lowercase) : ff
num in hexadecimal format(uppercase) : FF
Hello world, how are you?    I love C programing language.
The file is store at c:\files\word_files
Hello
Length: 6
num (padded): 00255
str1="               Hello", str2="World               "
val = 1.23
num = 255
Address of num is: 0x7ffca0b5855c

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.