The Wayback Machine - https://web.archive.org/web/20240610144947/https://www.geeksforgeeks.org/difference-d-format-specifier-c-language/
Open In App

Difference between %d and %i format specifier in C language

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A format specifier is a special character or sequence of characters used to define the type of data to be printed on the screen or the type of data to be scanned from standard input. A format specifier begins with a ‘%’ character followed by the sequence of characters for different types of data.

In short, it tells us which type of data to store and which to print. Format specifiers are primarily used with scanf() and printf() functions.

For example, If we want to read and print an integer using scanf() and printf() functions, either %i or %d is used but there is a subtle difference in both %i and %d format specifier.

%d specifies signed decimal integer while %i specifies integer of various bases.

‘%d’ and ‘%i’ behave similarly with printf()

There is no difference between the %i and %d format specifiers when used with printf.

Consider the following example.

C




// C program to demonstrate
// the behavior of %i and %d
// with printf statement
#include <stdio.h>
 
int main()
{
    int num = 9;
 
    // print value using %d
    printf("Value of num using %%d is = %d\n", num);
 
    // print value using %i
    printf("Value of num using %%i is = %i\n", num);
 
    return 0;
}


Output

Value of num using %d is = 9
Value of num using %i is = 9

%d and %i behavior is different with scanf()

%d assume base 10 while %i auto detects the base.

Therefore, both specifiers behave differently when they are used with an input function. So, the value of 012 would be 10 with %i but 12 with %d.

  • %d takes an integer value as a signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value.
    (Note: If the input is in the octal format like 012 then %d will ignore 0 and take input as 12).
     
  • %i takes an integer value as an integer value with decimal, hexadecimal, or octal type.
    To enter a value in hexadecimal format, the value should be provided by preceding “0x” and to enter a value in value in octal format, the value should be provided by preceding “0”.

Consider the following example.

C




// C program to demonstrate the difference
// between %i and %d specifier
#include <stdio.h>
 
int main()
{
    int a, b, c;
 
    printf("Enter value of a in decimal format:");
    scanf("%d", &a;);
 
    printf("Enter value of b in octal format: ");
    scanf("%i", &b;);
 
    printf("Enter value of c in hexadecimal format: ");
    scanf("%i", &c;);
 
    printf("a = %i, b = %i, c = %i", a, b, c);
 
    return 0;
}


Output

Enter value of a in decimal format: 12
Enter value of b in octal format: 012
Enter value of c in hexadecimal format: 0x12
a = 12, b = 10, c = 18

Explanation

  • The decimal value of a as 12 is 12
  • The decimal value of b as 12(octal) is 10
  • The decimal value of c as 12(hexadecimal) is 18


Similar Reads

C++ final specifier
In Java, we can use final for a function to make sure that it cannot be overridden. We can also use final in Java to make sure that a class cannot be inherited. Similarly, the latest C++ standard C++ 11 added final. Use of final specifier in C++ 11: Sometimes you don’t want to allow derived class to override the base class’ virtual function. C++ 11
3 min read
_Noreturn function specifier in C
The _Noreturn keyword appears in a function declaration and specifies that the function does not return by executing the return statement or by reaching the end of the function body. If the function declared _Noreturn returns, the behavior is undefined. A compiler diagnostic is recommended if this can be detected. The _Noreturn specifier may appear
2 min read
Understanding constexpr Specifier in C++
constexpr is a feature added in C++ 11. The main idea is a performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized by the developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time (similar to template met
5 min read
Difference between AI File Format and EPS File Format
AI stands for Adobe Illustrator and this file format was built to control the file size of high graphic images. This file format also works on a Postscript base. It was designed by Adobe corporation to manage vector drawings or artwork in an easier and more efficient manner. It uses the .ai file extension to save Adobe illustrator files. EPS stands
2 min read
Difference Between Machine Language and Assembly Language
Machine language is the low level programming language. Machine language can only be represented by 0s and 1s. In earlier when we have to create a picture or show data on the screen of the computer then it is very difficult to draw using only binary digits(0s and 1s). For example: To write 120 in the computer system its representation is 1111000. S
2 min read
Difference Between Assembly Language And Machine Language
Machine language is the low-level programming language. It can only be represented by 0s and 1s. Earlier when we have to create pictures or show data on the screen of the computer then it is very difficult to draw using only binary digits(0s and 1s).For example: To write 120 in the computer system its representation is 1111000. So it is very diffic
2 min read
Difference between Hardware Description Language and Software Language
1. Hardware description language : It describes hardware in textual form. It describes hardware behavior and their structure. It is description of circuits that can be created at different levels of description according to language syntax and semantics or at level of abstraction from basic logic gates. It is stimulation of designs before fabricati
2 min read
Difference between assembly language and high level language
1. Assembly level language :It is a low-level language that allows users to write a program using alphanumeric mnemonic codes, instead of numeric code for a set of instructions examples of large assembly language programs from this time are IBM PC DOS.2. High-level language :It is a machine-independent language. It enables a user to write a program
1 min read
Difference Between C Language and LISP Language
C Language: C is the procedural Programming language. It was designed to be compiled using a compiler. The Language has small and fixed number of keywords like if/else, for, while,.. etc. We can use more than one assignment that may be used in one statement in this language. Functions are also used here, it can return values that can be ignored, wh
2 min read
Difference between Java and C language
Here are some of the differences between Java and C language. C is much faster than Java Java is slower than C due to overhead. C Java C was developed by Dennis M. Ritchie between 1969 and 1973.Java was developed by James Gosling in 1995.C is a Procedural Programming Language.Java is Object-Oriented language.C is more procedure-oriented.Java is mor
3 min read