In C language, printf function is used to print output on the screen. This 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(” format String”, Arguments);
Here,
- Format String: It is a string that specifies the output. It may also contain a format specifier to print the value of any variable such as character and an integer value.
- Arguments: These are the variable names corresponding to the format specifier.
Format Specifier in C
In C, a value can be a character type, integer type, float type, and so on. To display these values we have format specifiers used in printf function. These format specifiers start with the percentage symbol ‘%’. Some of the commonly used format specifiers are given below.
%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 1: 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
#include <stdio.h>
int main()
{
printf("Hello Geek!");
return 0;
}
|
Example 2: 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
#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;
}
|
OutputThe sum of 99 and 1 is 100
Example 3: In this example, the printf() function is used to print the value of the variable “num” in the output. The “%f” format specifier is used to print a floating-point value.
C
#include <stdio.h>
int main()
{
float num = 3.14159;
printf("The value of pi is approximately %f\n", num);
return 0;
}
|
OutputThe value of pi is approximately 3.141590
Example 4: In this example, the printf() function is used to print the character value of the variable ‘letter’ in the output. The “%c” format specifier is used to print a character that corresponds to their variable name ‘letter’.
C
#include <stdio.h>
int main()
{
char letter = 'a';
printf("The letter is %c\n", letter);
return 0;
}
|
Example 5: The “%p” format specifier in C is used to print the memory address of a variable. When using “%p” in a printf() statement, it expects the corresponding argument to be a pointer to a memory location rather than an actual value.
C
#include <stdio.h>
int main() {
int a = 10;
printf("Address of variable 'a' is %p", &a;);
return 0;
}
|
OutputAddress of variable 'a' is 0x7ffee324432c
Example 6: In C, the %x format specifier is used to print the hexadecimal representation of an unsigned integer. When used with printf(), the value of the integer variable is printed as a string of hexadecimal digits (0-9 and A-F).
C
#include <stdio.h>
int main() {
unsigned int num = 255;
printf("The value of num in hexadecimal is: %x\n", num);
return 0;
}
|
OutputThe value of num in hexadecimal is: ff