printf() function is originally declared under the <cstdio>header file. It prints the formatted string to the standard output stdout.
Syntax:
int printf(const char*word, .......)
Parameters:
- word: represents the string that needs to be printed on the standard output stdout,
- ……. : represents any no of extra parameters that can be passed to it.
Return Type: It returns the total number of characters written.
Example:
C++
#include <iostream>
using namespace std;
int main()
{
char a[] = "geeksforgeeks";
printf("The total no of characters returned by "
"printf() is %d",
printf("%s", a));
return 0;
}
|
Output
geeksforgeeksThe total no of characters returned by printf() is 13
To know more about the return type of print() refer to return type of print() and scanf().
Example:
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5;
printf("%d", a);
}
|
Note: Anything present with the ‘%’ symbol inside printf() is termed a Format Specifiers.
Components of a Format Specifier:
- A ‘%’ sign
- width – It is an optional field that determines the width field
- Precision
- Length
- Specifier
- Flags
Flags:
It is responsible for conversion behavior between two fields. It can be classified into:
- – : Left Justifies the answer.
- + : attached as a sign to the beginning of the result.
- space : By default, if nothing is present space is attached to the result.
- # : alternative form of conversion
- 0 : used to pad numbers mostly trailing zeroes in integer or floating-point numbers.
Format Specifiers:
Format Specifiers are used for taking inputs and printing the output of a type.
| Specifier |
Output |
Example |
| d or i |
Signed decimal integer |
360 |
| u |
Unsigned decimal integer |
7200 |
| o |
Unsigned octal |
555 |
| x |
Unsigned hexadecimal integer |
7fb |
| e |
Mantissa/Exponent notation |
4.567e+8 |
| c |
Character |
k |
| s |
string of characters |
geeks |
| % |
Prints the % sign to the output screen |
|
Note:
.x f denotes the precision of float variables up to x decimal places
Example 1:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
char course[]="geeksforgeeks";
printf("Owner of this article is %s",course);
return 0;
}
|
Output
Owner of this article is geeksforgeeks
Example 2:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
float Self_paced_price = 3999.0,
system_design_price = 10999.0;
printf("the value of system design price / self paced "
"price is %.3f",
system_design_price / Self_paced_price);
return 0;
}
|
Output
the value of system design price / self paced price is 2.750
.3f depicts the value that should be printed within 3 decimal places.
Example 3:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int decimal_number = 13;
printf("Decimal to Octal conversion of %d is %o",
decimal_number, decimal_number);
return 0;
}
|
Output
Decimal to Octal conversion of 13 is 15
Example 4:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int decimal_number = 13;
printf("Decimal to Hexadecimal conversion of %d is %x",
decimal_number, decimal_number);
return 0;
}
|
Output
Decimal to Hexadecimal conversion of 13 is d
Example 5:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 78;
printf("%c\n", a);
int b = 'g';
printf("%d", b);
return 0;
}
|
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
26 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...