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.
Format specifier | Description |
---|---|
%d or %i | It is used to print the signed integer value where the signed integer means that the variable can hold both positive and negative values. |
%u | It is used to print the unsigned integer value where the unsigned integer means that the variable can hold only a positive value. |
%o | It is used to print the octal unsigned integer where the octal integer value always starts with a 0 value. |
%x | It 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. |
%X | It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical characters in uppercase such as A, B, C, etc. |
%f | It is used for printing the decimal floating-point values. By default, it prints the 6 values after ‘.’. |
%e/%E | It is used for scientific notation. It is also known as Mantissa or Exponent. |
%g | It 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. |
%p | It is used to print the address in a hexadecimal form. |
%c | It is used to print the unsigned character. |
%s | It is used to print the strings. |
%ld | It is used to print the long-signed integer value. |
#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
}
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.
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…