Skip to main content

examples of format specifers in c

 //format specifers - it a way to tell the compiler what type of data is in variable during taking input,displaying output to the user*/
  // character format specifers
  
  #include <stdio.h> 
int main() 
   char ch[] = "satyam";  //[empty]-it contains infinite value [12]- it contains character upto 12 letters
   printf("%s\n"ch);    //%s -is for words or line
   return 0

#include <stdio.h> 
int main() 
   char ch = 's';    // for single letter 'letter' is used while "letter" shows error
   printf("%c\n"ch);  // %c - is for single letter
   return 0

// Integer format specifiers
#include <stdio.h> 
int main() 
   int x = 45y = 90
   printf("%d\n"x);     //%d &%i - do the same thing
   printf("%i\n"x); 
   return 0

// Floating point specifers
#include <stdio.h> 
int main() 
   float a = 12.67
   printf("%f\n"a); 
   printf("%e\n"a); 
   printf("%E\n"a);
   return 0
// example
#include <stdio.h> 
int main() 
   char str[] = "geeksforgeeks"
   printf("%20s\n"str); 
   printf("%-20s\n"str); 
   printf("%20.5s\n"str); 
   printf("%-20.5s\n"str); 
   return 0

#include<stdio.h>
int main()
{
   float a=7.333;
   printf("%8.4f",a); // %8 means it insert 8 space before decimal including 7 & .4 means it insert 4 digit after decimal
   return 0;
}


#include<stdio.h>
int main()
{
   float a=7.333;
   printf("%-8.4f satyam",a); // %-8 means it insert 8 space after decimal including 3330 & .4 means it insert 4 digit after decimal
   return 0;
}
/*1.%u for an unsigned integer.
2.%lld for long long int.
3.%o octal integer without leading zero
4.%x hexadecimal integer without 0x before the number
*/

Comments

Popular posts from this blog

example of typecasting in c

/*Type Casting in C Typecasting allows us to convert one data type into other. */ //simple example to cast int value into the float. #include <stdio.h>    int   main (){   float   f = ( float ) 9 / 4 ;     printf ( "f : %f \n " ,  f  );     return   0 ;   }