Ads block

Banner 728x90px

Format Specifiers in C

Format specifiers can be defined as operators which are used to print data that are stored in the variables using the printf() function and taking input data to store in a variable using scanf() function.

Simply, we can say that format specifiers are used for input and output purpose because we cannot print the value stored in the variable directly without using the format specifiers available in C. It is a way to do this job by using some format specifiers.


List of C Format Specifiers

Format SpecifiersDescription
%dInteger Format Specifier
It is used to print the signed integer value.
%fFloat Format Specifier
It is used to print decimal floating point values.
%sString Format Specifier
It is used to print strings.
%cCharacter Format Specifier
It is used to print single unsigned character.
%uUnsigned Int Format Specifier
It is used to print unsigned( only positive ) integer value.
%ldLong Int Format Specifier
It is used to print long-singed integer value.
%oOctal Unsigned Int Format Specifier
It is used to print the octal unsigned integer value.

Integer Format Specifier '%d'

It is used to print the signed integer value. Here, signed integer means a variable holds value with their sign(+ve or -ve). %d specifier is used in printf() and scanf() function to print a value and taking input by users respectively.
Let's understand Integer format specifier by the help of an example.

#include<stdio.h>
int main()
 {
   int a=-5;
   int b;
   scanf("%d" ,&b);
   printf("value of a = %d\n" a);
   printf("value of b = %d" b);
   return 0;
 }

hello world



Float Format Specifier '%f'

It is used to print fractional values. %f format specifier indicates that the value you want to print or store in the variable, is floating type value. C compiler can understand that float type of data is in a variable during printing data of the variable.
Let's take an example of Float format specifier.

#include<stdio.h>
int main()
 {
   float a=2.0034;
   printf("value of a = %f",a);
   return 0;
 }

hello world



String Format Specifier '%s'

This specifier is used to print collection of character (known as string). %s format specifier is used in printf() function to print string that is stored in array variable.

#include<stdio.h>
int main()
 {
   char a[]={'w','e','b','k','i','t'}
   printf("%s",a[]);

   return 0;
 }

hello world



Character Format Specifier '%c'

It is used to print single character stored in the variable. %c format specifier is used in the both functions(printf & scanf). To print a character we use %c format specifier in the printf() function and in the scanf() function, it is used to take a character from keyboard as input.

Let's see an example to understand it.

#include<stdio.h>
 {
   int main()
   char a;
   scanf("%c",&a);
   printf("character is %c",a);
   return 0;
 }

hello world



Unsigned Int Format Specifier '%u'

It is used to print the unsigned integer value. It means a variable can hold only positive value. To print unsigned integer value, we use %u format specifier in the printf() function.

To illustrate this, here is a suitable example.

#include<stdio.h>
int main()
 {
   int a=27499;
   printf("value is %u",a);

   return 0;
 }

hello world



Long Int Format Specifier '%ld'