w3resource

C printf()

Formatted Output and the printf() function

One of the common task in every program is the printing of output. We use the output to request input from a user and later display the status/result, computations etc. In C programming there are several functions for printing formatted output.

The printf() function is used to format and print a series of characters and values to the standard output.

Syntax:

int printf(const char *format-string, argument-list);

Parameters:

Name Description
format The format argument is a string containing C language conversion specifications. The conversion specification specifies the notation, alignment, significant digits, field width, and other aspects of the output format. To represent non-printing characters, such as newlines and tabs, the format string may contain escape characters. Below are the details.
argument-list Depending on the format string, the function may require additional arguments.

Format strings specify notation, alignment, significant digits, field width, and other aspects of output formats. It can contain ordinary alphanumeric characters; along with escape characters, conversion specifiers, and other characters. Format specifications are made up of a the percent sign (%) followed by one of the following conversion operators, which determine what printf does with its arguments.

Conversion Specifiers:

Specifier Description
%% Print a single % character
%c Convert an int to an unsigned character and print the resulting character
%d or %i Print an int as a signed decimal number
%u Print an unsigned as an unsigned decimal number
%o Print an unsigned as an unsigned octal number
%x Hexadecimal notation (using lowercase letters a-f)
%X Hexadecimal notation (using uppercase letters A-F)
%e Exponential notation (using a lowercase e as in 3.1415e+00)
%E Exponential notation (using an uppercase E as in 3.1415E+00)
%f Print a double using a decimal format like [-]ddd.ddd
%g The more compact of %e or %f, insignificant zeros do not print.
%G Same as g, but using an uppercase E
%s Print the string pointed to by a char *
%p Print a void * argument in hexadecimal (ANSI C only)
%n Store the number of characters printed at this point in the integer pointed to by the int * argument. Nothing is printed. (ANSI C only).

Conversion Flags:

You can control the alignment of the output using any of these optional flags.

Flag Format Example
+ (plus) Always prints a sign character (+ or -). %+7.2d
- (minus) Left-justifies the converted argument in its field. %-7.2d
0 (Zero) Pad with zeros rather than spaces. %07.2d
# (hash)

Use an alternate form for the output. The effect differs depending on the conversion specifier.

  • For o, the precision (described below) is increased so that the first digit printed is a 0
  • For x or X, a non-zero value has a 0x prepended (or 0X for the X specification)
  • For eEfg or G, the result will always contain a decimal point, even if no digits follow it. Additionally, trailing zeros are not removed from numbers formatted with g or G

Conversion width and precision:

By including these options in the format string, you can control the width and precision of the output:

Character Description Example
Field width A digit string specifying the minimum number of digits to be printed. %5f
Precision A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point. %5.2f

Some conversion operators may also be preceded by a size specification:

  • h indicates that the argument associated with a dioux or X operator is a short or unsigned short.
  • l indicates that the argument associated with a dioux or X operator is a long or unsigned long.
  • L indicates that the argument associated with a eEfg or G operator is a long double (ANSI C only)

Escape sequences in C:

An escape sequence is a series of characters that represents a special character. It begins with a backslash character (\), which indicates that the character(s) that follow the backslash character should be treated in a special way. C uses escape sequences within a format string to print certain special characters. For example \n moves the output position to the beginning of the next line. The following is a list of escape sequences.

Escape sequence Action
\n prints a new line
\b backs up one character
\t moves the output position to the next tab stop
\\ prints a backslash
\" prints a double quote
\' prints a single quote

Return value

  • Returns the number of bytes printed.

Examples: printf() function

Function Result
printf("%%|\n"); 1 %|
 
printf("%c|\n", 'p'); p|
printf("%5c|\n", 'p');     p|
printf("%-5c|\n", 'p'); p    |
 
printf("%d|\n", 234567); 234567
printf("%5d|\n", 234567); 234567
printf("%6d|\n", 234567); 234567
printf("%6d|\n", 1);           1
 
printf("%4.2d|\n", 234567); 234567
printf("%6.2d|\n", 234567); 234567
printf("%6.2d|\n", 1);         01
 
printf("%06d|\n", 234567); 234567
printf("%-6d|\n", 234567); 234567
printf("% 6d|\n", 234567);   234567
printf("%+6d|\n", 234567); +234567
 
printf("%08d|\n", 234567); 00234567
printf("%-8d|\n", 234567); 234567    
printf("% 8d|\n", 234567);    234567
printf("%+8d|\n", 234567);  +234567
 
printf("%-6.2d|\n", 234567); 234567 
printf("% 6.2d|\n", 234567);   234567
printf("%+6.2d|\n", 234567); +234567
 
printf("%i|\n", 234567); 234567
printf("%5i|\n", 234567); 234567
printf("%6i|\n", 234567); 234567
printf("%6i|\n", 1);           1
 
printf("%o|\n", 234567); 712107
printf("%4o|\n", 234567); 712107
printf("%5o|\n", 234567); 712107
 
printf("%4.2o|\n", 234567); 712107
printf("%5.2o|\n", 234567); 712107
 
printf("%#5o|\n", 234567); 0712107
printf("%05o|\n", 234567); 712107  
printf("%-5o|\n", 234567); 712107  
 
printf("%#8o|\n", 234567);    0712107
printf("%08o|\n", 234567); 00712107
printf("%-8o|\n", 234567); 712107    
 
printf("%-5.2o|\n", 234567); 712107 
 
printf("%x|\n", 234567); 39447 
printf("%4x|\n", 234567); 39447 
printf("%5x|\n", 234567); 39447 
 
printf("%X|\n", 234567); 39447 
 
printf("%4.2x|\n", 234567); 39447 
printf("%5.2x|\n", 234567); 39447 
 
printf("%#5x|\n", 234567); 0x39447
printf("%05x|\n", 234567); 39447  
printf("%-5x|\n", 234567); 39447 
 
printf("%#8x|\n", 234567);   0x39447
printf("%08x|\n", 234567); 00039447
printf("%-8x|\n", 234567); 39447   
 
printf("%#8X|\n", 234567);   0X39447
printf("%08X|\n", 234567); 00039447
printf("%-8X|\n", 234567); 39447   
 
printf("%-5.2x|\n", 234567); 39447 
 
printf("%e|\n", 23456.7645); 2.345676e+004 
printf("%4e|\n", 23456.7645); 2.345676e+004 
printf("%5e|\n", 23456.7645); 2.345676e+004 
 
printf("%E|\n", 23456.7645); 2.345676E+004 
 
printf("%4.2e|\n", 23456.7645); 2.35e+004 
printf("%5.2e|\n", 23456.7645); 2.35e+004 
printf("%4.2E|\n", 23456.7645); 2.35E+004 
printf("%5.2E|\n", 23456.7645); 2.35E+004 
 
printf("%#5e|\n", 23456.7645); 2.345676e+004
printf("%05e|\n", 23456.7645); 2.345676e+004 
printf("%-5e|\n", 23456.7645); 2.345676e+004 
rintf("% 5e|\n", 23456.7645);   2.345676e+004
printf("%+5e|\n", 23456.7645); +2.345676e+004
 
printf("%#8e|\n", 23456.7645); 2.345676e+004
printf("%08e|\n", 23456.7645); 2.345676e+004
printf("%-8e|\n", 23456.7645); 2.345676e+004
printf("% 8e|\n", 23456.7645);   2.345676e+004
printf("%+8e|\n", 23456.7645); +2.345676e+004
 
printf("%#8E|\n", 23456.7645); 2.345676E+004  
printf("%08E|\n", 23456.7645); 2.345676E+004  
printf("%-8E|\n", 23456.7645); 2.345676E+004 
printf("% 8E|\n", 23456.7645);   2.345676E+004
printf("%+8E|\n", 23456.7645); +2.345676E+004
 
printf("%#5.2e|\n", 23456.7645); 2.35e+004
printf("%05.2e|\n", 23456.7645); 2.35e+004 
printf("%-5.2e|\n", 23456.7645); 2.35e+004 
printf("% 5.2e|\n", 23456.7645);    2.35e+004
printf("%+5.2e|\n", 23456.7645);  +2.35e+004
 
printf("%f|\n", 23456.7645); 23456.764500 
printf("%4f|\n", 23456.7645); 23456.764500 
printf("%5f|\n", 23456.7645); 23456.764500 
 
printf("%4.2f|\n", 23456.7645); 23456.76 
printf("%5.2f|\n", 23456.7645); 23456.76 
 
printf("%#5f|\n", 23456.7645); 23456.764500
printf("%05f|\n", 23456.7645); 23456.764500
printf("%-5f|\n", 23456.7645); 23456.764500 
printf("% 5f|\n", 23456.7645);   23456.764500
printf("%+5f|\n", 23456.7645); +23456.764500
 
printf("%#8f|\n", 23456.7645); 23456.764500 
printf("%08f|\n", 23456.7645); 23456.764500 
printf("%-8f|\n", 23456.7645); 23456.764500
printf("% 8f|\n", 23456.7645);   23456.764500
printf("%+8f|\n", 23456.7645); +23456.764500
 
printf("%#5.2f|\n", 23456.7645); 23456.76
printf("%05.2f|\n", 23456.7645); 23456.76
printf("%-5.2f|\n", 23456.7645); 23456.76
printf("% 5.2f|\n", 23456.7645);   23456.76
printf("%+5.2f|\n", 23456.7645); +23456.76
 
printf("%g|\n", 23456.7645); 23456.8 
printf("%4g|\n", 23456.7645); 23456.8 
printf("%5g|\n", 23456.7645); 23456.8 
 
printf("%G|\n", 23456.7645); 23456.8 
 
printf("%4.2g|\n", 23456.7645); 2.3e+004 
printf("%5.2g|\n", 23456.7645); 2.3e+004 
 
printf("%#5g|\n", 23456.7645); 23456.8 
printf("%05g|\n", 23456.7645); 23456.8 
printf("%-5g|\n", 23456.7645); 23456.8 
printf("% 5g|\n", 23456.7645);   23456.8
printf("%+5g|\n", 23456.7645); +23456.8
 
printf("%#8g|\n", 23456.7645);   23456.8
printf("%08g|\n", 23456.7645); 023456.8
printf("%-8g|\n", 23456.7645); 23456.8 
printf("% 8g|\n", 23456.7645);   23456.8
printf("%+8g|\n", 23456.7645); +23456.8
 
printf("%#8G|\n", 23456.7645);   23456.8
printf("%08G|\n", 23456.7645); 023456.8
printf("%-8G|\n", 23456.7645); 23456.8 
printf("% 8G|\n", 23456.7645);   23456.8
printf("%+8G|\n", 23456.7645); +23456.8
 
printf("%#5.2g|\n", 23456.7645); 2.3e+004 
printf("%05.2g|\n", 23456.7645); 2.3e+004 
printf("%-5.2g|\n", 23456.7645); 2.3e+004 
printf("% 5.2g|\n", 23456.7645);   2.3e+004
printf("%+5.2g|\n", 23456.7645); +2.3e+004
 
printf("%s|\n", "ABCDE"); ABCDE
printf("%4s|\n", "ABCDE"); ABCDE
printf("%5s|\n", "ABCDE"); ABCDE
 
printf("%4.2s|\n", "ABCDE");      AB
printf("%5.2s|\n", "ABCDE");         AB
 
printf("%-5s|\n", "ABCDE"); ABCDE 
 
printf("%-8s|\n", "ABCDE"); ABCDE    
 
printf("%-5.2s|\n", "ABCDE"); AB      
 
printf("%p|\n", "ABCDE"); 000000000040537E 
printf("%4p|\n", "ABCDE"); 000000000040537E 
printf("%5p|\n", "ABCDE"); 000000000040537E 
 
printf("%-5p|\n", "ABCDE"); 000000000040537E 
 
printf("%-8p|\n", "ABCDE"); 000000000040537E 
 

To use the printf() function we must include the stdio library in the source code. To do this just place the following code at the beginning of your program.

#include <stdio.h>

To print a simple message in computer screen you might call printf() function as follows:

#include <stdio.h>
main()
{
printf ("You are learning printf() function");
}

Output:

You are learning printf() function

In the above examples, the cursor will remain at the end of the printed output. If you repeat the above code in the following way the second message would appear immediately after the first one.

#include <stdio.h>
main()
{
printf("You are learning printf() function");
printf("You are learning printf() function");
}

Output:

You are learning printf() function You are learning printf() function

Escape sequences in C

You will get an idea of using the escape sequences from the following example.

#include<stdio.h>
main()
{
printf("Create a new line\n");
printf("Print a double quotes (\") within a string\n");
printf("Print a single quotes (\') within a string\n");
printf("Print a Backslash\\ within a string\n");
printf("Using Backspace\b within a string\n");
printf("Using\tTab within a string\n");
}

Output:

Create a new line
  Print a double quotes (") within a string
  Print a single quotes (') within a string
  Print a Backslash\ within a string
  Using Backspace within a string
  Using     Tab within a string

Previous: C Operators
Next: C if else



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What is the difference between exit(0) & exit(1) and exit(0) & exit(1) in C?

exit(0) indicates successful program termination & it is fully portable, While

exit(1) (usually) indicates unsuccessful termination. However, it's usage is non-portable.

Note that the C standard defines EXIT_SUCCESS and EXIT_FAILURE to return termination status from a C program.

0 and EXIT_SUCCESS are the values specified by the standard to indicate successful termination, however, only EXIT_FAILURE is the standard value for returning unsuccessful termination. 1 is used for the same in many implementations though.

Ref : https://bit.ly/3l4TAjG