Format Specifiers Examples in C: Printf and Scanf Functions

Rumman Ansari   Software Engineer   2024-07-05 06:04:00   11846  Share
Subject Syllabus DetailsSubject Details 7 Program
☰ TContent
☰Fullscreen

Table of Content:

Format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.

The format specifier in printf() and scanf() are mostly same but there are some difference which we will see.

printf(char *format, arg1, arg2, …)

This function prints the character on standard output and returns the number of character printed the format is string starting with % and ends with conversion character (like c, i, f, d etc.).
Between these both there can be elements governing the printing format. Below is its description

  1. A minus(-) sign tells left alignment.
  2. A number after % specifies the minimum field width to be printed if the characters are less than the size of width the remaining space is filled with space and if it is greater than it printed as it is without truncation.
  3. A period( . ) symbol seperate field width with the precision.

Precision tells the maximum number of digits in integer, charcters in string and number of digits after decimal part in floating value.

Integer Format Specifier %d

The %d format specifier is implemented for representing integer values. This is used with printf() function for printing the integer value stored in the variable.

Syntax:
  printf ("%d", ); 

Float Format Specifier %f

The %f format specifier is implemented for representing fractional values. This is implemented within printf() function for printing the fractional or floating value stored in the variable. Whenever you need to print any fractional or floating data, you have to use %f format specifier.

Syntax:
printf("%f", ); 

Character Format Specifier %c

The %c format specifier is implemented for representing characters. This is used with printf() function for printing the character stored in a variable. When you want to print a character data, you should incorporate the %c format specifier.

Syntax:
printf("%c",); 

String Format Specifier %s

The %s format specifier is implemented for representing strings. This is used in printf() function for printing a string stored in the character array variable. When you have to print a string, you should implement the %s format specifier.

Syntax:
printf("%s",);

Unsigned Integer Format Specifier %u

The %u format specifier is implemented for fetching values from the address of a variable having unsigned decimal integer stored in the memory. This is used within printf() function for printing the unsigned integer variable.

Syntax:
printf("%u",);

Long Int Format Specifier %ld

The %ld format specifier is implemented for representing long integer values. This is implemented with printf() function for printing the long integer value stored in the variable.

Syntax:
printf("%ld",);