Article by: Manish Methani
Last Updated: November 7, 2021 at 2:04pm IST
In C programming, the printf() function is used to output values to the console. The function uses format specifiers to determine the type of data that is being output. The % format specifiers are used to format the output and can be used with various flags and modifiers to achieve different effects.
There are several types of format specifiers in C programming, including:
The % format specifiers are used in conjunction with the printf() function to format the output. Here are some examples of how to use the % format specifiers with the printf() function:
int num = -123; printf("The value of num is %d", num);
Output:
The value of num is -123
unsigned int num = 123; printf("The value of num is %u", num);
Output:
The value of num is 123
float num = 3.14159; printf("The value of num with 2 decimal places is %.2f", num);
Output:
The value of num with 2 decimal places is 3.14
char ch = 'A'; printf("The character is %c", ch);
Output:
The character is A
char str[] = "Hello, world!"; printf("The string is %s", str);
Output:
The string is Hello, world!
float a = 3.122222223; printf(“a = %.2f ”, a);
Output:
a = 3.12
float a = 3.122222223; printf(“a = %.3f ”, a);
Output:
a = 3.122
%.3f will restrict the values upto three decimal values.
float a = 1.289999; printf(“ a = %6.2f ”, a);
Output:
_ _1.29
where _ _ are spaces
%6.2f :- means output will be in 6 columns
a = 1.28999;
While printing float values using %6.2f format specifier, the final output should be of length 6. As 6.2f indicates, the total length of output should be 6 and 2 values after "." The rest part is covered with spaces. As you can see in the output image below, 2 decimal values after dot and before that we need three more values so that total length becomes 6.
float a = 1.289999; printf(“ a = %5.3f ”, a);
Output:
In this case, the final output length should be of length 5 and 3 decimal values after "." dot and 2 values before "." dot
float a = 1.289999; printf(“ a = %5.2f ”, a);
In this case, the final output length should be of length 5 and 2 decimal values after "." dot and 3 values before "." dot
float a = 1111.289999; printf(“ a = %5.3f ”, a);
Output:
a = 1111.290
In this case, before dot it contains enough digits than 5 so it will store 1111 in one block and rest in another.
Space will only be added when there are not enough digits.
Question
printf(“ %5.2f “, a);
Answer
If a is 1111.2344444, it prints 1111.23 If a is 11.2344444, it prints 11.23 If a is 1.2344444 , it prints _1.234 , where _ is one leading whitespace character.
Example 1:
int a = 1234; printf(“a = %3d”, a);
Output:
a = 1234
Example 2:
int a = 123; printf(“a = %3d”, a);
Output:
a = 123
Example 3:
int a = 12; printf(“a = %3d”, a);
Output:
a = _12 where, _ is space
Example 4:
int a = 1; printf(“a = %3d”, a);
Output:
a = _ _1 where, _ are spaces.
In conclusion, the % format specifiers are a powerful tool in C programming that allows you to format output in various ways. By using the examples provided in this tutorial, you can master the art of formatting output and improve your C programming skills.
A: Format specifiers are placeholders used in C language to print the value of a variable in a specific format. They are used with printf() and scanf() functions to format input and output.
A: The syntax of a format specifier starts with the percentage sign (%) followed by optional flags, width, precision, length modifier and conversion specifier.
A: Width specifies the minimum number of characters to be printed while printing a variable. Precision specifies the number of digits to be printed after the decimal point while printing floating-point numbers.
A: The most commonly used format specifiers in C are %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and %p for pointers.
A: Yes, you can use multiple format specifiers in a printf() or scanf() statement to format the output or input of multiple variables.
A: Some advanced format specifiers in C include %e and %E for scientific notation, %u for unsigned integers, %x and %X for hexadecimal numbers, and %n for the number of characters printed so far.
A: Using the wrong format specifier for a variable can result in undefined behavior and may cause the program to crash or produce unexpected results. It is important to always use the correct format specifier for the type of variable being printed or scanned.