Format Specifiers In C Language | Syntax with Examples

Format Specifiers In C
Format Specifiers In C Language

Format Specifiers In C

A Format specifier is an operator that is used with the input output function (Ex – printf (), scanf ())  to specify the size and data type of the data stored inside a Variable.

For example – In C language, when we want to print the store data inside a variable, then we cannot print that data directly, we have to use Format specifiers to print that data.

If you do not know about Variable and Data Types, then you can read about Variable and Data Types from here: – Variable In C, and Data Types In C |

Format specifiers are used inside the printf () function when we have to print or display on a screen the data stored inside a variable. And we use Format specifiers with scanf () function when we have to store any data in a variable.

Using Format specifiers, the compiler knows which type of data to take when taking input via the scanf () function and which type value to print when printing the data of a variable through the printf () function.

Format specifiers begin with % Operator And after % operator some characters like – c, i, f, d, etc are used.

In Format specifiers, apart from the % operator and some characters, we can use some more elements which are given below:

  • minus (-) sign use |
  • Use a number after % |
  • (.) Use of Symbols.

These were some elements that we can use with Format specifiers. We will understand these elements further by elaborating on the examples.

List of Format Specifiers in C Language

Format SpecifierType
%cCharacter Format Specifier
%dInteger Format Specifier
%fFloating Format Specifier
%sString Format Specifier
%lflong-range of floating point number (for double data type)
%e or %EScientific notation of floats
%g or %GSimilar as %e or %E
%hiSigned integer (short)
%huUnsigned Integer (short)
%iDecimal, Octal, or Hexadecimal integer
%l or %ld or %liLong
%LfLong double
%luUnsigned int or unsigned long
%lli or %lldLong long
%lluUnsigned long long
%oAn octal (base 8) integer
%pPointer or An Address
%uUnsigned integer Format Specifier
%x or %XHexadecimal representation
%nPrints nothing
%%Prints % character
Format Specifiers In C

Let us now know about one of the most commonly used format specifiers in all of these Format specifiers.

1. Character Format Specifier – %c

  • Format Specifier %c is used to represent characters.
  • it is used with the printf () function and the scanf () function. 
  • Format Specifier %c is used in C language when printing data of character type in a variable, as well as Format Specifier %c is also used to store the data of the character type in a variable. 

Syntax -:

printf ("%c", variable_name);
scanf ("%c", &variable_name);

2. Integer Format Specifier – %d

  • Format Specifier %d is used to represent integer numbers. 
  • It is used with the printf () function and the scanf () function.
  • It is used when printing data of an Integer type variable, as well as Format Specifier %d, is used to store data of Integer type in a variable.

Syntax -:

printf ("%d", variable_name);
scanf ("%d", &variable_name);

3. Float Format Specifier – %f

Format Specifier %f is used to represent floating-point numbers. Format Specifier% f is used with both the printf () and scanf () functions.

Format Specifier %f is used in C language when printing data of floating-point type variable, as well as Format Specifier %f is also used to store data of floating point type variable.

Syntax -:

printf ("%f", variable_name);
scanf ("%f", &variable_name);

4. String Format Specifier – %s

Format Specifier %s is used to represent Strings.it is used with both printf () and scanf () functions.

It is used when in C language to print the data of a String type Character Array variable, and it is also used to store String type data in a Character Array Variable. 

Syntax -:

printf ("%s", variable_name);
scanf ("%s", &variable_name);

5. Unsigned Integer Format Specifier – %u

Format Specifier %u is used to represent an unsigned integer value. It is used to print unsigned integer type data stored in a variable by the printf () function.

Syntax -:

printf ("%u", variable_name);

6. Long-range of Float Format Specifier -%lf

Format Specifier %lf is used to represent floating-point numbers with Long-Rang. Format Specifier %lf is used with printf ().

It is used in C language when we want to print floating-point data that are stored in double type variable

Syntax -:

printf ("%lf", variable_name);

Let us now understand all these Format Specifiers by an example.

Example of Format Specifier In C -:

#include <stdio.h>
int main ()
{
int x = 5;
char ch = 'a';
float f1 = 24.5;
double d1 = 31.08;
char s [20] = "CsTutorialpoint";

printf ("%d \n", x);
printf ("%c \n", ch);
printf ("%f \n", f1);
printf ("%lf \n", d1);
printf ("%s \n", s);

return (0);
}

Output -:

5
a
24.500000
31.080000
CsTutorialpoint

Conclusion -:

Friends, I hope that you have found the answer of your question and you will not have to search about the Format Specifiers in C Language 

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts 

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Format Specifiers in C Language 

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

Leave a Comment