In C programming language, values can be type integer, floating-point, single character, or sequence of characters. We use format specifiers in C to display values of a variable of a different type.
C contains different format specifiers used in printf() and scanf() functions; in this tutorial, we will go through a few important and most commonly used format specifiers in our C programs.
Why Do We Use C Format Specifiers?
Format specifiers in C are used to take inputs and print the output of a type. The symbol we use in every format specifier is %. Format specifiers tell the compiler about the type of data that must be given or input and the type of data that must be printed on the screen.
Now that you have a brief idea of format specifiers in C, we have a few lists of format specifiers moving ahead.
The Most Commonly Used Format Specifiers in C
The Most commonly used format specifiers are given below:
Format Specifiers |
Type of Output |
%d or %i |
A decimal integer or signed integer |
%c |
Signed character |
%f |
Signed float |
%e |
A floating-point number |
%s |
A string or sequence of character |
%lf |
double |
%Lf |
Long double |
%o %u |
Octal integer Short unsigned integer |
%ld |
Long decimal integer |
%x |
Hexadecimal integer |
%p |
Print memory address in the hexadecimal form |
We will go through a few examples that will help you understand how to use format specifiers in printf() and scanf() functions for a better understanding.
%d (Decimal Integer) Format Specifier
#include
int main()
{
int a=50;
printf(“The integer value of a is %d \n”,a);
return 0;
}
Output:
%c (Character) Format Specifier
#include
int main()
{
char s;
printf(“Enter the character \n”);
scanf(“%c”,&s);
printf(“The character is: %c”,s);
return 0;
}
Output:
%f (Floating Point) Format Specifier
#include
int main()
{
float a=3;
printf(“The floating point of a is %f \n”,a);
return 0;
}
Output:
%e (Floating Pointer Number) Format Specifier
#include
int main()
{
float a=12.5;
printf(“The floating-point of a is %e \n”,a);
return 0;
}
Output:
%s (String) Format Specifier
#include
int main()
{
char s[15]=”simplilearn”;
printf(“The string value of s is %s \n”,s);
return 0;
}
Output:
%lf (Double) Format Specifier
#include
int main()
{
double d=12.5;
printf(“The double value of d is %lf \n”,d);
return 0;
}
Output:
%o (octal integer) Format Specifier
#include
int main()
{
int oct=11;
printf(“The octal integer value of oct is %o \n”,oct);
return 0;
}
Output:
%x (Hexadecimal Integer) Format Specifier
#include
int main()
{
int h=14;
printf(“The hexadecimal value of h is %x \n”,h);
return 0;
}
Output:
%p (Prints Memory Address) Format Specifier
To find the memory address that holds values of a variable, we use the %p format specifier, and it prints in hexadecimal form.
#include
int main()
{
int sum=0;
printf(“The memory address of sum is %p \n”,&sum);
return 0;
}
output:
With that, you can conclude this tutorial on Format Specifiers in C
Next Steps
“Data Structures in C” can be your next topic. So far, you have learned why we use format specifiers in C. The next fundamentals will be the data structures and the varieties in data structures used for different purposes.
If you are interested in building a career in software development, then feel free to explore Simplilearn’s Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Full Stack Developer – MERN Stack should be the right thing for your career. Explore the course and enroll soon.
Please let us know in the comment section below if you have any questions regarding the “Format specifiers in C ” tutorial. Our experts will get back to you at the earliest.
Source link