The Wayback Machine - https://web.archive.org/web/20240814015555/https://www.geeksforgeeks.org/printf-in-c/
Open In App

printf in C

Last Updated : 27 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen).  The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output in numerous ways.

Syntax of printf

printf ( "formatted_string", arguments_list);

Parameters

  • formatted_string: It is a string that specifies the data to be printed. It may also contain a format specifier to print the value of any variable such as a character and an integer.
  • arguments_list: These are the variable names corresponding to the format specifier.

Return Value

  • printf() returns the number of characters printed after successful execution.
  • If an error occurs, a negative value is returned.

Example of printf

In this example, we are printing the string “Hello Geek!” in the output using printf() function. In printf() function what we will write inside the double quotes (” “) is printed in the output.

C




// C program to illustrate the use of printf function
#include <stdio.h>
 
int main()
{
    // using printf to print "Hello Geek!"
    printf("Hello Geek!");
 
    return 0;
}


Output

Hello Geek!

Formatting in C printf

In C, a value can be a character type, integer type, float type, and so on. To display and format these values using printf, we have format specifiers that are used in the formatted string. These format specifiers start with the percentage symbol ‘%’.

Syntax of Format Specifier

%[flags][width][.precision][length]specifier

1. Specifier

It is the character that denotes the type of data. Some commonly used specifiers are:

  • %d: for printing integers
  • %f: for printing floating-point numbers
  • %c: for printing characters
  • %s: for printing strings
  • %p: for printing memory addresses
  • %x: for printing hexadecimal values

Example

printf("%c", char_variable);

2. Width

It is the sub-specifier that denotes the minimum number of characters that will be printed.

If the number of characters is less than the specified width, the white space will be used to fill the remaining characters’ places. But if the number of characters is greater than the specified width, all the characters will be still printed without cutting off any.

Example

printf("%25s", some_string);

or

printf("%*s", 25, some_string);

3. Precision

Precision subspecifier meaning differs for different format specifiers it is being used with.

  • For Integral data(d, i, u, o, x, X): Specifies the minimum number of digits to be printed. But unlike the width sub-specifier, instead of white spaces, this sub-specifier adds leading zeroes to the number. If the number has more digits than the precision, the number is printed as it is.
  • For Float or Double Data(f, e, a, A): Specifies the number of digits to be printed after the decimal point.
  • For String (s): Specifies the length of the string to be printed.

Example

printf("%.10d", some_integer);
printf("%.3f", some_float);
printf("%.25s", some_string);

or

printf("%.*d", 10,  some_integer);
printf("%.*f", 3, some_float);
printf("%.*s", 25, some_string);

4. Length

Specifies the length of the data type in the memory. It is used in correspondence with data type modifiers.

There are 3 length sub-specifiers:

  • h: With short int and unsigned short int
  • l: With long int and unsigned long int.
  • L: With long double

Example

printf("%lf", double_variable);

Examples of printf() in C

Example 1:  Print a Variable using Specifier in printf()

In this example, we are printing an integer using a format specifier “%d” which is used for an integer. In the printf() function we are printing integers along with string using %d and in the arguments, we have passed variable names in a sequence corresponding to their format specifiers.

C




// C program to print a variable
#include <stdio.h>
int main()
{
    int num1 = 99;
    int num2 = 1;
    printf("The sum of %d and %d is %d\n", num1, num2,
           num1 + num2);
    return 0;
}


Output

The sum of 99 and 1 is 100

Example 2: printf with Specified Width

In this example, we will specify the width of the output which will be printed by the printf() function.

C




// C program to illustrate the use of printf with width
// specifier
#include <stdio.h>
int main()
{
    // number to be printed
    int num = 123456;
    // printing the num with 10 width and getting the
    // printed characters in char_printed
    printf("Printing num with width 10: ");
    int chars_printed = printf("%10d", num);
    printf("\nNumber of characters printed: %d",
           chars_printed);
 
    // specifying with using other method
    printf("\nPrinting num with width 3: ");
    chars_printed = printf("%*d", 3, num);
    printf("\nNumber of characters printed: %d",
           chars_printed);
 
    return 0;
}


Output

Printing num with width 10:     123456
Number of characters printed: 10
Printing num with width 3: 123456
Number of characters printed: 6

As we can see, even if we define the width that is less than the present characters, all the characters are still printed. Also, we have seen the two ways in which we can define the width.

Example 3: printf with Precision Sub-Specifier

In this example, we will demonstrate the precision sub-specifier in the printf() function

C




// C program to illustrate the use of precision
// sub-specifier
#include <stdio.h>
 
int main()
{
    int num = 2451;
    float dec = 12.45126;
    char* str = "GeeksforGeeks";
 
    // precision for integral data
    printf("For integers: %.10d\n", num);
    // precision for numbers with decimal points
    printf("For floats: %.2f\n", dec);
    // for strings
    printf("For strings: %.5s", str);
 
    return 0;
}


Output

For integers: 0000002451
For floats: 12.45
For strings: Geeks

Example 4: printf with Length Sub-Specifier

C




// C program to illustrate the length modifier
#include <stdio.h>
 
int main()
{
    long var = 3000000000;
    // printing var using %d
    printf("Using %%d: %d", var);
    // printing var using %ld
    printf("\nUsing %%ld: %ld", var);
 
    return 0;
}


Output

Using %d: -1294967296
Using %ld: 3000000000

Related Articles:



Previous Article
Next Article

Similar Reads

Nested printf (printf inside printf) in C
Predict the output of the following C program with a printf inside printf. #include&lt;stdio.h&gt; int main() { int x = 1987; printf(&quot;%d&quot;, printf(&quot;%d&quot;, printf(&quot;%d&quot;, x))); return(0); } Output : 198741 Explanation : 1. Firstly, the innermost printf is executed which results in printing 1987 2. This printf returns total n
1 min read
How to print % using printf()?
Here is the standard prototype of printf function in C: int printf(const char *format, ...); The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of argument (and it is an error if insufficiently many arguments are given). The char
1 min read
Return values of printf() and scanf() in C/C++
What values do the printf() and scanf() functions return ? printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error Example 1: The printf() function in the code written below returns 6. As 'CODING' contains 6 characters. C/C++ Code // C/C++ program to demonstrate return value // of printf()
2 min read
Use of &amp; in scanf() but not in printf()
Why there is need of using '&amp;' in case of scanf function while not in case of printf function. Examples: scanf("%d %d", &amp;a, &amp;b);printf("%d %d", a, b);As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values
1 min read
How to Write Your Own printf() in C?
In this article, we will learn how to implement your own custom printf() function in C language. The printf() function is present in &lt;stdio.h&gt; header and uses a const char* and variable length arguments as its parameters. Hence we would be using &lt;stdarg.h&gt; header file which provides operations available on variable arguments passed to a
4 min read
Difference between return and printf in C
In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let's see each of them and see what are their functions and differences. 1. Return Statement In C, return is a statement used within a function to terminate the function's execution and return a value to the ca
2 min read
What is use of %n in printf() ?
In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n. #include&lt;stdio.h&gt; int main() { int c; printf(&quot;geeks for %ngeeks
1 min read
puts() vs printf() for printing a string
In C, both puts() and printf() functions are used for printing a string on the console and are defined in &lt;stdio.h&gt; header file. In this article, we will discuss the differences in the usage and behavior of both functions. puts() FunctionThe puts() function is used to write a string to the console and it automatically adds a new line characte
3 min read
How to Change the Output of printf() in main() in C?
To change the output of printf() in main(), we can use Macro Arguments. #define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be called first to change the output of printf() in ma
2 min read
What is the difference between printf, sprintf and fprintf?
printf: printf function is used to print character stream of data on stdout console. Syntax : printf(const char* str, ...); Example : C/C++ Code // simple print on stdout #include &lt;stdio.h&gt; int main() { printf(&quot;hello geeksquiz&quot;); return 0; } Outputhello geeksquiz sprintf: String print function instead of printing on console store it
2 min read
Article Tags :
three90RightbarBannerImg