Format Specifier
The format specifier is used in printf() function to specify the format according to which a data value is to be displayed / printed on the output device. It is also used in scanf() function to specify the format according to which a value is to be read from an input device. The format specifiers are written starting (or prefix) with % symbol.The format specifier must be used if the variable name is given in the printf() or scanf() function as argument. If multiple variables are used as arguments then format specifier for each variable must be specified. Otherwise compiler will display an error message. In printf() function, format specifier can be used before the text message, between the text message or after the text message.
Different format specifiers are used for different types of variables. These format specifiers are as follows:
- Integer format specifiers
- Character format specifiers
- Floating-point format specifiers
i) Integer Format Specifiers
Different format specifiers for integer values are as follows:
Format Specifier
|
Purpose
|
%d
|
It is used for signed decimal integer value
|
%i
|
It is used for signed integer value.
|
%ld
|
It is used for long integer value.
|
%u
|
It is used for unsigned integer or unsigned short integer value.
|
%o
|
It is used for unsigned octal value. It means that the output is converted into unsigned octal integer value.
|
%x
|
It is used for unsigned hexadecimal value with lowercase letters such as; a, b, c, d, e, f.
|
%X
|
It is used for unsigned hexadecimal value with uppercase letters such as; A, B, C, D, E, F.
|
ii) Character Format Specifiers
Different format specifiers for character values are as follows:
Format Specifier
|
Purpose
|
%c
| It is used for a single character. |
%s
| It is used for a string. |
iii) Floating-Point Format Specifiers
Different format specifiers for floating point values are as follows:
Format Specifier
| Purpose |
%f
| It is used for signed float value. |
%lf
| It is used for signed double value. |
%e
| It is used for signed float or double value. The output is converted to signed real value using e-notation. |
%E
| It is also used for signed float or double value. The output is converted to signed real value using E-notation. |
%g
| It is used for large floating-point value using e-notation format with extra zero dropped. |
Write a program that initializes value to a variable x1 of float type and displays the value of x1 in the following base:
- using %f
- using %e
- using %E
- using %g
Example in C
Example in C
#include < stdio.h >
#include < conio.h >
void main ( )
{
float x1 =125.42547125 ;
clrscr() ;
printf ( " Using format character f : %f \n " , x1 ) ;
printf ( " Using format character e : %e \n " , x1 ) ;
printf ( " Using format character E : %E \n " , x1 ) ;
printf ( " Using format character g : %g \n " , x1 ) ;
getch ( ) ;
}
Output of the program
Using format character f : 125.425468
Using format character e : 1.254255e+02
Using format character E : 1.254255E+02
Using format character g :125.425
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.