In the C programming language, values ​​can be of 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 various format specifiers used in the functions printf() and scanf(); in this tutorial we will go through several 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 accept inputs and print the output of a type. The symbol we use in every format specifier is %. Format specifiers tell the compiler the type of data to be given or typed and the type of data to be printed to the screen.
Now that you have a brief idea of ​​format specifiers in C, we have a few lists of format specifiers moving forward.
The most commonly used format specifiers in C
The most commonly used format specifiers are given below:
Format specifiers |
Output type |
%d or %i |
Decimal integer or signed integer |
%° C |
Sign with sign |
%f |
A float with a sign |
%e |
A floating point number |
%c |
A string or sequence of characters |
%lf |
double |
%Lf |
Long double |
%o %u |
An octal integer Short unsigned integer |
%ld |
Long decimal integer |
%x |
A hexadecimal integer |
%p |
Print the memory address in hexadecimal |
We will go through some examples to help you understand how to use format specifiers in printf() and scanf() functions for better understanding.
Format specifier %d (decimal integer).
#include
int main()
{
int a=50;
printf(“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(“Character is: %c”,s);
return 0;
}
Output:
%f (floating point) format specifier.
#include
int main()
{
float a=3;
printf(“Floating point of a is %f n”,a);
return 0;
}
Output:
Format specifier %e (float number).
#include
int main()
{
float a=12.5;
printf(“Floating point of a is %e n”,a);
return 0;
}
Output:
%s (string) format specifier
#include
int main()
{
char s[15]=”just learning”;
printf(“String value of s is %s n”,s);
return 0;
}
Output:
%lf (double) format specifier
#include
int main()
{
double d=12.5;
printf(“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 of oct is %o n”,oct);
return 0;
}
Output:
Format specifier %x (hexadecimal integer).
#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 contains variable values, we use the %p format specifier and it prints in hexadecimal.
#include
int main()
{
int sum=0;
printf(“The memory address of sum is %p n”,&sum);
return 0;
}
output:
This concludes this tutorial on format specifiers in C
If you’re eager to gain the skills needed to work in a challenging, rewarding and dynamic IT role – we’ve got you covered! Discover the endless possibilities through this innovative graduate program in Full Stack Web Development course designed by our partners at Caltech CTME. Sign up today!
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 basics will be data structures and the varieties in data structures used for different purposes.
If you’re interested in building a career in software development, then don’t hesitate to check out Simplilearn’s courses, which will give you the job-ready software development training you need to succeed today. Perhaps you’re looking for a more comprehensive training program in today’s most in-demand software development skills, tools and languages? If so, our Postgraduate Program in Full Stack Web Development should be the right thing for your career. Check out the course and enroll soon.
Please let us know in the comment section below if you have any queries regarding Format Specifiers in C tutorial. Our experts will contact you as soon as possible.
https://www.simplilearn.com/tutorials/c-tutorial/format-specifiers-in-c