Categories: C

Format specifiers in C

Format Specifiers in C are used during input and output operations. They specify what kind of data is getting printed or accepted during program execution, that is to say,  using this concept compiler can understand what type of data is in a variable during taking input using the scanf() function and printing using printf() function.

Key points :
  • The format string always starts with a ‘%‘ character.
  • A minus(-) signifies left alignment.
  • 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.
  • A period( . ) symbol separates field width with precision.
Various Format Specifiers in C:
Format specifierDescription
%d or %iIt is used to print the signed integer value where the signed integer means that the variable can hold both positive and negative values.
%uIt is used to print the unsigned integer value where the unsigned integer means that the variable can hold only a positive value.
%oIt is used to print the octal unsigned integer where the octal integer value always starts with a 0 value.
%xIt is used to print the hexadecimal unsigned integer where the hexadecimal integer value always starts with a 0x value. In this, alphabetical characters are printed in small letters such as a, b, c, etc.
%XIt is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc.
%fIt is used for printing the decimal floating-point values. By default, it prints the 6 values after ‘.’.
%e/%EIt is used for scientific notation. It is also known as Mantissa or Exponent.
%gIt is used to print the decimal floating-point values, and it uses fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output.
%pIt is used to print the address in a hexadecimal form.
%cIt is used to print the unsigned character.
%sIt is used to print the strings.
%ldIt is used to print the long-signed integer value.
Example:
#include <stdio.h>
main() {
   char ch = 'Z';
   printf("%c\n", ch); //printing character data
   //print decimal or integer data with d and i
   int i = 20, j = 100;
   printf("%d\n", i);
   printf("%i\n", j);
   float f = 32.67;
   printf("%f\n", f); //print float value
   printf("%e\n", f); //print in scientific notation
   int a = 45;
   printf("%o\n", a); //print in octal format
   printf("%x\n", a); //print in hex format
   char str[] = "Hello Coderzpy!";
   printf("%s\n", str);
   printf("%20s\n", str);//shift to the right 20 characters including the string
   printf("%-20s\n", str); //left align
   printf("%20.5s\n", str); //shift to the right 20 characters including the string, and print string up to 5 character
   printf("%-20.5s\n", str); //left align and print string up to 5 character
}
Output:

Z
20
100
32.669998
3.267000e+01
55
2d
Hello Coderzpy!
     Hello Coderzpy!
Hello Coderzpy!     
               Hello
Hello               

Note: also read about Precedence of Operators in C & Comments in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago