It has the following syntax:
printf("format string", variable);
scanf("format string", &variable);
In this particular syntax,
- format specifier denotes the format specifier that starts with the % symbol.
- variable signifies the identifier of the variable.
Types of Format Specifiers in C
There exist various format specifiers that are frequently utilized in the C programming language. A few examples include:
| Format specifier | Description |
|---|---|
| %d or %i | It is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values. |
%u |
It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value. |
%o |
It is used to print the octal unsigned integer in the octal integer value, where the octal number always represented with a leading 0 value. |
%x |
It is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc. |
%X |
It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase, such as A, B, C, etc. |
%f |
It is used for printing the decimal floating-point values. By default, it prints the 6 values after '.'. |
| %e or %E | It is used for scientific notation. It is also known as Mantissa or Exponent. |
| %g or %G | It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output. |
%p |
It is used to print the memory address in a hexadecimal form. |
%c |
It is used to print the signed character. |
%s |
It is used to print the strings. |
%ld |
It is used to print the long-signed integer value. |
%% |
It is used to print the % character itself. |
%llu |
It is mainly utilized to print the large unsigned integers. |
Now, let’s understand all the format specifiers in detail through an example.
1. Signed Integer Format Specifier (%d)
In C programming, the signed integer format specifiers are commonly employed within the printf and scanf functions to handle signed integer values. They are capable of storing both positive and negative numbers.
Signed Integer Format Specifier Example in C
Let's consider a scenario to demonstrate the signed integer format specifier in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int x=5;
int y=10;
printf("Value of x is: %d", x);
printf("\nValue of y is: %d",y);
return 0;
}
Output:
Value of x is: 5
Value of y is: 10
Explanation:
In the provided code snippet, we have utilized the %d format specifier to display the integer values of x and y.
2. Unsigned Integer Format Specifier (%u)
In the realm of C programming, the unsigned integer (%u) format specifier is primarily employed for representing unsigned integer data types. This format is specifically designed to accommodate solely positive numbers. In the event that a negative number is assigned to %u, it automatically transforms the integer into its 2’s complement representation.
Unsigned Integer Format Specifier Example in C
Let's consider an example to demonstrate an unsigned integer in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int x=15;
int y= -20;
printf("Value of x is: %u", x);
printf("\nValue of y is:%u", y);
return 0;
}
Output:
Value of x is: 15
Value of y is:4294967276
Explanation:
In this illustration, we are utilizing the unsigned (%u) format specifier to display the values of x and y. In this scenario, x is assigned a positive value, hence the %u specifier accurately displays the value of x. Nevertheless, it fails to display the correct value of y as y holds a negative value.
3. Unsigned Octal Number for Integer (%o)
In the realm of C programming, the unsigned octal (%o) format specifier is primarily employed for displaying the octal integer value, with the octal number consistently denoted by a leading 0 value.
Unsigned Octal Format Specifier Example in C
Let's consider an example to demonstrate the unsigned octal integer format specifier in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
int a=0100;
printf("Octal value of a is: %o", a);
printf("\nInteger value of a is: %d",a);
return 0;
}
Output:
Octal value of a is: 100
Integer value of a is: 64
Explanation:
In the provided code snippet, we have utilized the %o octal format specifier to display both the octal and integer representations of variable a.
4. Unsigned Hexadecimal Format Specifier %x and %X
In C programming, the hexadecimal %x format specifier is primarily employed within the formatted string to represent hexadecimal integers. When using the %x format specifier, the letters in the hexadecimal values will be displayed in lowercase. Conversely, if the %X format specifier is used, the letters in the hexadecimal numbers will be shown in uppercase.
The ```
printf("format string", variable);
scanf("format string", &variable);
printf("format string", variable);
scanf("format string", &variable);
#include <stdio.h>
int main() {
unsigned int hexValue = 255;
printf("The hexadecimal value is: %x\n", hexValue);
return 0;
}
In this code snippet, the hexValue variable is assigned the decimal value 255. When printed using the %x format specifier, it will display the hexadecimal equivalent "ff" without any sign information.
Let's consider an example to demonstrate the unsigned hexadecimal format specifier in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int y=0xA;
printf("Hexadecimal value of y is: %x", y);
printf("\nHexadecimal value of y is: %X",y);
printf("\nInteger value of y is: %d",y);
return 0;
}
Output:
Hexadecimal value of y is: a
Hexadecimal value of y is: A
Integer value of y is: 10
Explanation:
In this instance, we've selected the variable y which holds the hexadecimal value 'A'. We exhibit the hexadecimal value of y in two different formats. We employ %x and %X to showcase the hexadecimal value where %x exhibits the value in lowercase, for instance, 'a' and %X showcases the value in uppercase, for example, 'A'.
5. Floating-point Format Specifier (%f)
In the C programming language, the floating-point (%f) format specifier is employed to display floating-point numbers. By default, it displays six digits after the decimal point.
Floating-point Format Specifier Example in C
Let's consider a scenario to demonstrate the floating-point format specifier in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
float y=3.4;
printf("Floating point value of y is: %f", y);
return 0;
}
Output:
Floating point value of y is: 3.400000
Explanation:
In this instance, we have assigned the y variable as a float data type which displays the floating point value of y.
6. Exponent Format Specifier (%e or %E)
In the realm of C programming, the exponent format specifier (%e or %E) is employed to represent numbers in scientific notation. This format is alternatively referred to as Mantissa or Exponent.
Example
#include<stdio.h>
int main() //main function
{
float y=3;
printf("Exponential value of y is: %e", y);
printf("\nExponential value of y is: %E", y);
return 0;
}
Output:
Exponential value of y is: 3.000000e+00
Exponential value of y is: 3.000000E+00
Explanation:
In this instance, we showcase the process of displaying a floating-point value in exponential (scientific) notation by utilizing the %e and %E format specifiers. The %e format specifier exhibits the value in lowercase (for instance, 3.000000e+00), whereas %E employs uppercase (for example, 3.000000E+00).
7. Fixed Floating-Point Format Specifier (%g)
In C programming, this particular fixed floating-point format specifier is primarily employed for displaying decimal floating-point values with a fixed precision. This implies that the decimal value in the input will be preserved exactly as it appears in the output.
Fixed Floating-Point Format Specifier in C
Let's consider a demonstration to explain the constant floating-point format specifier in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
float a=4.5;
printf("Float value of a is: %g", a);
return 0;
}
Output:
Float value of a is: 4.5
Explanation:
In this instance, we display the floating-point value of variable a utilizing the %g format specifier. Subsequently, the %g specifier automatically selects between %f and %e.
8. Address Format Specifier (%p)
In C programming, the address (%p) format specifier is employed for displaying memory addresses and pointers.
Address Format Specifier in C
Let's consider a scenario to demonstrate the Address format specifier in the C programming language.
Example
#include <stdio.h>
int main() //main function
{
int x=5;
printf("Address value of x in hexadecimal form is: %p", &x);
return 0;
}
Output:
Address value of x in hexadecimal form is: 0x7ffca1a7a284
Explanation:
In this instance, we showcase the memory location of the variable x employing the %p format specifier. Following this, we employ the %p format specifier to exhibit pointer (address) values in hexadecimal form, while &x fetches the address of the variable x.
9. Character Format Specifier (%c)
In C coding, the character (%c) format specifier is employed to display the char data type value. It is also applicable for performing both formatted input and output in the C programming language.
Character Format Specifier Example in C
Let's consider an example to demonstrate the character format specifier in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
char a='c';
printf("Value of a is: %c", a);
return 0;
}
Output:
Value of a is: c
Explanation:
In this instance, a character variable 'a' is initialized with the value 'c' and then displayed using the %c format specifier. The %c specifier is specifically employed to exhibit a singular character output within the printf function.
10. String Format Specifier (%s)
In the C programming language, the (%s) format specifier is primarily utilized for displaying string values.
String Format Specifier (%s) in C
Let's consider an example to demonstrate the character format specifier in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
printf("%s", "Logic Practice");
return 0;
}
Output:
Logic Practice
Explanation:
In this instance, we display the phrase "Logic Practice" by employing the %s format specifier within the printf function. The %s placeholder is utilized to showcase a string of characters, also known as a sequence of characters.
Minimum Field Width Specifier
If the goal is to showcase an outcome that takes up minimal screen space, this can be accomplished by appending an integer value following the percent symbol within the format specifier.
Example
#include<stdio.h>
int main() //main function
{
int x=900;
printf("%8d", x);
printf("\n%-8d",x);
return 0;
}
Output:
900
900
Explanation:
In this instance, we employ the %8d format specifier to exhibit the value with a width of 8 spaces, whereas the %-8d specifier will align the value to the left.
Next, we will explore the process of populating vacant areas, as demonstrated in the following code snippet:
Example
#include<stdio.h>
int main() //main function
{
int x=12;
printf("%08d", x);
return 0;
}
Output:
00000012
Explanation:
In this instance, %08d indicates that the vacant space is populated with zeros.
Specifying Precision
In C programming, we have the ability to define the precision by utilizing the '.' (dot) operator, which is then succeeded by an integer and the format specifier.
Specifying Precision Example in C
Let's consider an example to demonstrate setting precision in the C programming language.
Example
#include<stdio.h>
int main() //main function
{
float x=12.2;
printf("%.2f", x);
return 0;
}
Output:
Explanation:
In this instance, the float value x is printed with precision up to two decimal places by employing the %.2f format specifier, followed by showcasing the rounded result as 12.20.