Tuesday, September 17, 2013

FORMAT SPECIFIER


FORMAT SPECIFIER
Ø  Format Specifiers in C language tells us which type of data to read and which type of data to print. It tells about the type of the data to be viewed.
Ø  It is also called as format code or control string.
Ø  There are several format specifiers available in printf.

Ø  The given below are some of the format specifiers used with printf in C program.
·        For integer data type the format specifier used with printf is %d
·        For float data type the format specifier used with printf is %f
·        For double data type the format specifier used with printf is %lf
·        For char data type the format specifier used with printf is %c
·        For string data type the format specifier used with printf is %s

Ø  The following is the list of format specifiers in C.
·        %d or %I or %n     For integers
·        %c                        For characters
·        %f                         For floating point values
·        %ld                       For long integer values
·        %lf                        For double type values
·        %s                        For char.arrays or strings
·        %x                        For hexa decimal values
·        %o                        For octal values
·        %u                        For unsigned integers

Ø  Format specifier is used in only 2 places:
1) In taking Input
2) In displaying Output
  §    Here is an example
                   int x=5;
                   printf( "%d", x );
             The Result will be
                             5
   §      What %d does is that it tells the function printf that the argument to be printed is a int type variable. .And x in function printf tells it to print the integer value from the variable named ‘x’.
§  Printing Out Characters
§  %c indicates to the printf() function that the argument to be printed is a character.
§  Let's first have a look at the program which prints out characters on the screen.
§  OUTPUT. The following is the output printed on the  screen:
Convert the value of c1 to character: A
Convert the value of c2 to character: a

Printing out characters on the screen.
  // Printing out characters 
#include <stdio.h>
#include <conio.h>
void main()
            {
                    char c1;
                    char c2;
                    c1 = 'A';
                    c2 = 'a';
printf("Convert the value of c1 to character: %c", c1);
printf("\nConvert the value of c2 to character: %c", c2);
getch();
 }


1 comment: