Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

C Format Specifier ❮ Edit Details

The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character.

The commonly used format specifiers in printf() function are:

Format specifier Description
%d or %i It is used to print the signed integer value where signed integer means that the variable can hold both positive and negative values.
%u It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only positive value.
%o It is used to print the octal unsigned integer where octal integer value always starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc.
%X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc.
%f It is used for printing the decimal floating-point values. By default, it prints the 6 values after '.'.
%e/%E It is used for scientific notation. It is also known as Mantissa or Exponent.
%g It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output.
%p It is used to print the address in a hexadecimal form.
%c It is used to print the unsigned character.
%s It is used to print the strings.
%ld It is used to print the long-signed integer value.

Let's understand the format specifiers in detail through an example.

  • %d
  1. int main()  
  2. {  
  3.   int b=6;  
  4.   int c=8;  
  5.   printf("Value of b is:%d", b);  
  6.   printf("\nValue of c is:%d",c);  
  7.   
  8.     return 0;  
  9. }  

In the above code, we are printing the integer value of b and c by using the %d specifier.

 

 

 

 

 

Output

C Format Specifier

  • %u
  1. int main()  
  2. {  
  3.   int b=10;  
  4.   int c= -10;  
  5.   printf("Value of b is:%u", b);  
  6.   printf("\nValue of c is:%u",c);  
  7.   
  8.     return 0;  
  9. }  

In the above program, we are displaying the value of b and c by using an unsigned format specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it does not print the value of c as c contains the negative value.

Output

C Format Specifier

  • %o
  1. int main()  
  2. {  
  3.   int a=0100;  
  4.   printf("Octal value of a is: %o", a);  
  5.   printf("\nInteger value of a is: %d",a);  
  6.   return 0;  
  7. }  

In the above code, we are displaying the octal value and integer value of a.

Output

C Format Specifier

  • %x and %X
  1. int main()  
  2. {  
  3.   int y=0xA;  
  4.   printf("Hexadecimal value of y is: %x", y);  
  5.   printf("\nHexadecimal value of y is: %X",y);  
  6.   printf("\nInteger value of y is: %d",y);  
  7.     return 0;  
  8. }  

In the above code, y contains the hexadecimal value 'A'. We display the hexadecimal value of y in two formats. We use %x and %X to print the hexadecimal value where %x displays the value in small letters, i.e., 'a' and %X displays the value in a capital letter, i.e., 'A'.

Output

C Format Specifier

  • %f
  1. int main()  
  2. {  
  3.   float y=3.4;  
  4.   printf("Floating point value of y is: %f", y);  
  5.   return 0;  
  6. }  

The above code prints the floating value of y.

Output

C Format Specifier

  • %e
  1. int main()  
  2. {  
  3.   float y=3;  
  4.   printf("Exponential value of y is: %e", y);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %E
  1. int main()  
  2. {  
  3.   float y=3;  
  4.   printf("Exponential value of y is: %E", y);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %g
  1. int main()  
  2. {  
  3.   float y=3.8;  
  4.   printf("Float value of y is: %g", y);  
  5.   return 0;  
  6. }  

In the above code, we are displaying the floating value of y by using %g specifier. The %g specifier displays the output same as the input with a same precision.

Output 

C Format Specifier

  • %p
  1. int main()  
  2. {  
  3.   int y=5;  
  4.   printf("Address value of y in hexadecimal form is: %p", &y);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %c
  1. int main()  
  2. {  
  3.   char a='c';  
  4.   printf("Value of a is: %c", a);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %s
  1. int main()  
  2. {  
  3.   printf("%s", "javaTpoint");  
  4.   return 0;  
  5. }  

 

Output

C Format Specifier

Minimum Field Width Specifier

Suppose we want to display an output that occupies a minimum number of spaces on the screen. You can achieve this by displaying an integer number after the percent sign of the format specifier.

  1. int main()  
  2. {  
  3.  int x=900;  
  4.   printf("%8d", x);  
  5.   printf("\n%-8d",x);  
  6.   return 0;  
  7. }  

C Format Specifier

  • %p
  1. int main()  
  2. {  
  3.   int y=5;  
  4.   printf("Address value of y in hexadecimal form is: %p", &y);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %c
  1. int main()  
  2. {  
  3.   char a='c';  
  4.   printf("Value of a is: %c", a);  
  5.   return 0;  
  6. }  

Output

C Format Specifier

  • %s
  1. int main()  
  2. {  
  3.   printf("%s", "javaTpoint");  
  4.   return 0;  
  5. }  

Output

C Format Specifier

Minimum Field Width Specifier

Suppose we want to display an output that occupies a minimum number of spaces on the screen. You can achieve this by displaying an integer number after the percent sign of the format specifier.

  1. int main()  
  2. {  
  3.  int x=900;  
  4.   printf("%8d", x);  
  5.   printf("\n%-8d",x);  
  6.   return 0;  
  7. }