The Wayback Machine - https://web.archive.org/web/20241113064622/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
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 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 Assembly Language And Machine Language
Assembly language and machine language are both low-level languages that are intended for direct manipulation of the hardware but assembly language is slightly more abstract and much easier to use than machine language. Machine language is a series of instructions written in binary that the CPU can understand and execute directly, making it the low
4 min read
Difference between assembly language and high level language
Programming Language is categorized into assembly language and high-level language. Assembly-level language is a low-level language that is understandable by machines whereas High-level language is human-understandable language. What is Assembly Language?It is a low-level language that allows users to write a program using alphanumeric mnemonic cod
2 min read
Difference Between Hardware Description Language and Software Language
Hardware and software are fundamental components of modern computing systems. While software languages are widely known for programming applications, hardware description languages (HDLs) play a crucial role in designing and describing electronic circuits. Ever wondered how Hardware Systems are designed, planned, and expressed in what form or langu
6 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
Difference between Python and Lua Programming Language
Python Python is one of the most popular and powerful scripting languages that works nowadays. It is a high-level interpreted programming language. It is a very simple scripting language and very easy to learn as compared to other languages. Python programming language is best for both scripting applications and as standalone programs along with th
3 min read
Difference between Procedural and Non-Procedural language
Procedural Language: In procedural languages, the program code is written as a sequence of instructions. User has to specify "what to do" and also "how to do" (step by step procedure). These instructions are executed in the sequential order. These instructions are written to solve specific problems. Examples of Procedural languages: FORTRAN, COBOL,
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Difference between Compiled and Interpreted Language
Prerequisite - Compiler vs Interpreter What is Compiled Language? A compiled language is a programming language that is generally compiled and not interpreted. It is one where the program, once compiled, is expressed in the instructions of the target machine; this machine code is undecipherable by humans. Types of compiled language - C, C++, C#, CL
2 min read
Difference between TypeScript and Dart language
TypeScript: It is an open-source pure object-oriented programming and compiled language which is developed and maintained by Microsoft. It has been influenced by JavaScript, Java, C#. It is C-style syntax and it can optionally trans-compile into JavaScript and it used to develop web and mobile applications. First release of TypeScript was in Octobe
2 min read
Difference between while(1) and while(0) in C language
Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
3 min read
Difference Between Matlab and C Language
Matlab and C are both powerful programming languages but they serve different purposes and have distinct features. In this article, we will learn the fundamental differences between Matlab and the C language. MATLABMATLAB stands for Matrix Laboratory and is a high-level programming language primarily used for numerical computing such as matrix mani
3 min read
Difference between Text Mining and Natural Language Processing
Text Mining and Natural Language Processing (NLP) are both fields within the broader domain of computational linguistics, but they serve distinct purposes and employ different methodologies: Text MiningText Mining goal is to extract significant numeric indices from the text. Thus, make the facts contained in the textual content available to a range
3 min read
Similarities and Differences between Ruby and C language
Similarities between Ruby and C There are many similarities between C and Ruby, and some of them are: Like C, in Ruby alsoâ€Ķ A programmer is able to program procedurally if they like to do. But still, behind the scenes, it will be object-oriented.Both the languages have the same operators, for example, compound assignment and bitwise operators. But
3 min read
Differences Between MATLAB and R Programming Language
MATLAB: MATLAB stands for Matrix Laboratory. MATLAB is a high performance language, initially intended to provide access to the matrix software designed by the projects LINPACK (Linear System Package) and EISPACK (Eigen System Package). It incorporates the environment for computation, visualization and programming. MATLAB provides a modern language
4 min read
Format String Vulnerability and Prevention with Example
A format string is an ASCII string that contains text and format parameters. Example: // A statement with format stringprintf("my name is : %s\n", "Akash");// Output// My name is : Akash There are several format strings that specify output in C and many other programming languages but our focus is on C. Format string vulnerabilities are a class of
3 min read
lvalue and rvalue in C language
lvalue:- lvalue simply means an object that has an identifiable location in memory (i.e. having an address). In any assignment statement "lvalue" must have the capability to store the data.lvalue cannot be a function, expression (like a+b) or a constant (like 3 , 4 , etc.). L-value: "l-value" refers to memory location which identifies an object. l-
4 min read
fgets() and gets() in C language
For reading a string value with spaces, we can use either gets() or fgets() in C programming language. Here, we will see what is the difference between gets() and fgets(). fgets()The fgets() reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character i
3 min read
C program to print characters without using format specifiers
As we know that there are various format specifiers in C like %d, %f, %c etc, to help us print characters or other data types. We normally use these specifiers along with the printf() function to print any variables. But there is also a way to print characters specifically without the use of %c format specifier. This can be obtained by using the be
1 min read
Encode an ASCII string into Base-64 Format
Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss. Base64 is used commonly in a number of applications including email via MIME, and storing complex data in XML. Problem with sending normal binary data to a network is th
15+ min read
JPG or PNG - Which Image Format Offers Better Quality?
In this digital world, People store their images/photos on mobile/computer/drives, etc. But the question arises of which format to choose, there are two image formats available. Which one is better? These questions come to your mind and to answer we are writing this article. This article includes the introduction of both the format jpg and jpeg, as
5 min read
Format Specifiers in C
The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc. The C language provides a number of format specifiers that are associated with the different
6 min read
isxdigit() function in C Language
isxdigit() function in C programming language checks that whether the given character is hexadecimal or not. isxdigit() function is defined in ctype.h header file. Hexadecimal equivalent of Decimal Numbers: Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Syntax: char isxdigit( char x); Examples: Input : A
2 min read
isupper() function in C Language
isupper() function in C programming checks whether the given character is upper case or not. isupper() function is defined in ctype.h header file. Syntax : int isupper ( int x ); Examples: Input: A Output: Entered character is uppercase character Input: a Output: Entered character is not uppercase character Input: 1 Output: Entered character is not
2 min read
isalnum() function in C Language
isalnum() function in C programming language checks whether the given character is alphanumeric or not. isalnum() function defined in ctype.h header file. Alphanumeric: A character that is either a letter or a number. Syntax: int isalnum(int x); Examples: Input : 1 Output : Entered character is alphanumeric Input : A Output : Entered character is a
2 min read
three90RightbarBannerImg