Conversion Specifiers in C
For Complete YouTube Video: Click Here
In this class, we will try to understand Conversion Specifiers in C.
In our previous class, we have covered conversion specifiers like %d and %c.
List of Conversion Specifiers
In this class, we will cover more conversion specifiers.
The image below is the list of some more conversion specifiers.
Using %d or %i the binary value stored on the memory is represented in the signed integer format.
Ex: 10, -40, 98 etc.
Using %u the binary value stored on the memory is represented in the Unsigned decimal format.
Ex: 10, 40, 98 etc.
Even though the binary value is signed value it will consider it as the binary value of unsigned decimal number.
Using %c the binary value stored on the memory is represented in the Character format.
Ex: a, c, & etc.
Using %s the binary value stored on the memory is represented as strings.
Using %e the binary value stored on the memory is represented as a floating point number in the exponential format.
Ex: 42.e3
Using %f the binary value stored on the memory is represented as floating point number in decimal format.
Ex: 42.23, .5 etc.
Using %g or %i the binary value stored on the memory is represented as double number in decimal format or exponential format depending on the value.
Formatting Information
The conversion specifiers can also be provided with the formatting information.
Let us try to understand it by using an example.
For example, consider the following lines of code.
int a = 12;
printf(“%d”, a);
The above function will print the output as 12.
Now consider the printf() function as shown below.
printf(“%10d”, a);
The conversion specifier is provided with formatting information.
Now the output will be printed within 10 spaces including the digits.
_ _ _ _ _ _ _ _ 10.
Similarly, if minus value is used with the formatting information the spaces will be allocated on the other side.
printf(“%-10d”, a);
10 _ _ _ _ _ _ _ _
We can specify the formatting information for floating point numbers also.
For floating point values the precision can decided.
Consider the example given below.
float k = 32.456789;
printf(“%2.3f”, k);
The precision in the output will be adjusted to three values as shown below.
32.460