Format Specifier for C


A character preceded with “%” is known as format specifier. A format specifier is responsible to accept a value from outside/keyboard and it ensure the following three things:-
(i)                  Type of outside value.
(ii)                Total  number of outside value.
(iii)               If coming value is not as a format specifier automatically cast coming value into its compatible value.
Format Specifier                                  Description
%c                                          To accept single character
%d                                          To accept signed integer(range -32768 to 32767)
%u                                          For unsigned integer(range 0 to 65535)
%s                                           For string
%f                                           For fractional (float type)
%ld                                         For long integer
%lf                                          For fractional (double type)
%x                                          For hexadecimal(small letter)
%X                                         For hexadecimal(capital letter) 
%#x                                        For hexadecimal(preceded by 0x,small letter)
%#X                                       For hexadecimal(preceded by 0x,capital letter)
%o                                          For octal 
%#O                                       For octal(preceded by 0)
%%                                        For displaying % sign
Explanation:-
1)      void main()
{
char ch=’A’;
clrscr();
printf(“ASCII of%c: is :%d”,ch,ch);
getch();
}
Output---à ASCII value of A is 65
     2)      void main()
{
clrscr();
printf(“%c:%d”,65,65);
getch();
}
Output-----à A:65
     3)      void main()
{
int a=25;
float b=7.5;
clrscr();
printf(“a=%d,b=%f”,a,b);
printf(“value of b=%b”,b);
getch();
}
Output--------à a=25,b=7.5
                                value of b=0
     4)      void main()
{
clrscr();
printf(“Wel%s\n%s\n%c”,”come”,”To”,’C’);
getch();
}
Output------à Welcome
                                To
                                C
     5)      void main()
{
int a=31;
clrscr();
printf(“Value of a=%d”,a);
printf(:\n hexa=%x,HEXA=%X”,a,a);
printf(:\n hexa=%#x,HEXA=%#X”,a,a);
printf(:\n octal=%o,OCTAl=%#O”,a,a);
getch();
}
Output-------à Value of a=31
                                hexa=1f,HEXA=1F
                                hexa=0x1f,HEXA=0x1F
                                octal=37,OCTAl=037


Comments

Popular Posts