What is printf in c programming?

Printf is a function to print formatted or unformatted data to stdout.

Printf is found in stdio header file.

It returns number of character written on to console.

You can simply put printf in if statements.

Prototype

#include<stdio.h>
int printf( char *formatted _string, params... );

Parameters

Look at the function parameters the formatted_string is to specify the output string to console.
It can contain various variables which have to be specified in params list.

If you want to include variables you have to specify the format specifiers that is preceeded with % symbol. Look at the list for various format specifiers for basic datatypes.

The character % has to be followed by one of the characters in below:

  • Conversion specifier.
  • Precision.
  • Length modifier.
  • Flag character.
  • Field width.

Basic conversion specifiers are as below.

int%d
float%f
double%lf
char%c
string (char array)%s

If you are eager to learn more about different conversion specifiers then read the data-types in c post there I have listed every conversion specifiers for different datatypes.

Return value

printf returns number of characters it wrote on to stdout, returns -1 if any error occurs.


Example program for printf in c

//Example program for printf in c
#include<stdio.h>
int main()
{
 printf("Hello scholarsoul.com \n I want to be a scholar, will you help me?");
 return 0;
}
Example program for printf in c
Example program for printf in c

Program for return value of printf function

//What is the return value of printf in c?
#include<stdio.h>
int main()
{
 int rv=printf("scholarsoul.com");
 printf("\nReturn value of previous printf is: %d",rv);
 return 0;
}
What is the return value of printf in c

You may still have some doubts that how can I print some characters to console.

How to print % using printf ?

You may think that I know % is to be used preceeding with a format specifier characters, but how to print % using printf? well you can look at the simple example below.

printf( "%% this is %% " );

If you still not understood then look at the simple code example below:

//How to print % using printf ?
#include<stdio.h>
int main()
{
 printf("%% this is %% ");
 return 0;
}
How to print % using printf

How to print double quotes using printf in c ?

‘ ” ‘ It is double quotes, yes you may have the doubt, You can simply print double quotes using backslash character succeeded by double quotes like this \" .

Look at the below example.

//How to print double quotes using printf ?
#include<stdio.h>
int main()
{
 printf("\" this is double quotes");
 return 0;
}
How to print double quotes using printf in c

Still you are wondering that I have seen \n \t what are these characters?

They are special character combinations.

\n is used for printing a new line.

\t is used for printing a tab space.


People also ask for

What is the output of printf Welcome to C programming?

Above printf statement produces output of "Welcome to C programming"

What is the return value of printf in c?

printf returns number of characters it wrote on to stdout, returns -1 if any error occurs.

printf vs std::cout, Which one is faster?

Printf is faster if you compare to the std::cout object of c++ programming language which is being used for output in c++.

Is printf a keyword in C?

printf is not a keyword, it is a function name defined in stdio.h.

How do I printf an int?

You can print an int using printf function like printf("%d",var);

What is %d in printf?

%d is format specifier. It specifies the type of variabe that we want to print.

Why is it called printf?

printf is called because it means print formatted . As it prints formatted data to stdout.


People also read