The printf() function is defined in the stdio.h header file. It helps to output the result to the console. It gives formatted output to stdout stream.
int printf(const char *format, ...); #where format can be a integer, character, string, float
The printf() function takes a format string as a parameter.Format tags prototype is Format tags prototype is %[flags][width][.precision][length]specifier.
Where specifiers can be - c, d or i, e, E, f, g, G, o, s, u, x, X, P, n, %
Flags can be - +, -, (space), #, 0
Width can be - (number), *
Precision can be - .number, .*
Length can be - h, l, L
Parameter | Description | Required / Optional |
---|---|---|
format | the string that contains the text to be written to the stdout stream | Required |
additional arguments | containing one value to be inserted instead of each %-tag specified in the format parameter | Optional |
The return value of printf() function is a total number of written characters in success and a negative number on failure.
Input | Return Value |
---|---|
If successful, | total number of characters |
On failure | a negative number |
#include <stdio.h>
int main()
{
int year = 5;
float page = 2.1;
/* Display the results using the appropriate format strings for each variable */
printf("ontest.com is over %d years old and pages load in %.1f seconds.\n", year, page);
return 0;
}
Output:
ontest.com is over 5 years old and pages load in 2.1 seconds
#include <stdio.h>
int main (){
char char = 'P';
char strg[25] = "ontest.com";
float flt = 8.102;
int num = 100;
int no = 150;
double dble = 15.123456;
printf("Character is %c \n", char);
printf("String is %s \n" , strg);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , num);
printf("Double value is %lf \n", dble);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;
}
Output:
Character is P String is ontest.com Float value is 8.102 Integer value is 100 Double value is 15.123456 Octal value is 226 Hexadecimal value is 96