All format specifiers in C with examples

Format specifiers in C also known as control strings. These control strings are used with input as well as output functions.

You can use these format specifiers with printf() and scanf() functions in C.

Format specifiers in C

As you already know that printf() function is used to display the output on console and scanf() is used to get input values from the user.

Below is the list of all format specifiers used in C language.

Control String/Format Specifiers in CDescription/ Used for
%dUsed for Integer values
%c or %sIt is used for a character as well as string values.
%fWe use %f for float type values
%uWhen we want to get and display unsigned integer, we use this %u
%ldLong integer format specifier in C

Let’s understand above-listed control strings:

%d used for Integer values

when you need an integer value(s) to display or get from a user. You can use the %d format specifier.

#include<stdio.h>
#include<conio.h>

void main()
{
int x,y;
clrscr();
printf("Enter any value of x");
scanf("%d",&x);
printf("Enter any value of y");
scanf("%d",&y);

printf("Sum of x+y=%d",x+y);
getch();
}

In above programming code, we use %d with printf() and scanf() functions.

%f format specifier

#include<stdio.h>
#include<conio.h>

void main()
{
float x,y;
clrscr();
printf("Enter any value of x");
scanf("%f",&x);
printf("Enter any value of y");
scanf("%f",&y);

printf("Sum of x+y=%f",x+y);
getch();
}

You need %f control string to get and display float (decimal) type values from the user.

Character format specifier %c

It is used to display or input any character value from a user.

#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
clrscr();
printf("Enter any character value");
scanf("%c",&ch);


printf("Value typed by you is: %c",ch);
getch();
}

%s for string format specifier

You can display a string stored in an array variable.

#include<stdio.h>
#include<conio.h>

void main()
{
char ch[]="Coursesafter12th";
clrscr();


printf("Value stored in ch is: %s",ch);
getch();
}

It will give you “Coursesafter12th” as an output.

%ld for long int format specifier

When integer range fails, long int starts his work for calculations etc. %ld is used for long int format specifier in C.

void main()
{
long int x,y;
clrscr();
printf("Enter any value of x");
scanf("%ld",&x);
printf("Enter any value of y");
scanf("%ld",&y);

printf("Sum of x+y=%ld",x+y);
getch();
}

You can read also

Hope that you easily understand the format specifiers in C. If you have any doubt please ask in below comment section.

If you want to learn how to blog? step by step, you can visit our blog zerootech

Leave a Comment

error: Don\'t make piracy - Write your own content :)