Hello, readers welcome to new post. Today we will discuss Difference between %d and %i format specifiers in C language. In C language of programming %d and %i are called formate specifiers. %d was used to specify the variable type as decimal and %i used for integer specification.
Note: %d is used to specify signed decimal integers and %i specifies integer
In a simple way there is no difference for print() function output but printing in numbers through use of %d or %i but for use of scanf function differences exist. scanf() has the ability to detect base through %i but suppose base ten for %d. In this tutorial, we will learn some points that will help use to find the basic difference between %d and %i. So let’s get started
What is Format specifiers in C
- The format specifier are used in C programming language to define the input and outputs. Formate specifiers help the compiler to know the type of data in a variable when get input through use of scanf() function and printing using print() function
- Commonly used Format specifiers are listed here
-
Format Specifier Type %c Character %d it is an integer sign %e or %E it used as a scientific notation for float %f It used for Float values %g or %G It is like the %e or %E %hi Signed integer (short) %hu Unsigned Integer (short) %i Unsigned integer %l or %ld or %li Long %lf Double %Lf Long double %lu Unsigned int or unsigned long %lli or %lld Long long %llu Unsigned long long %o Octal representation %p Pointer %s String %u Unsigned int %x or %X Hexadecimal representation %n Prints nothing %% Prints % character
Difference between %d and %i format specifier in C language
- We will get the difference according to printf and scanf. First for printf
- For printf there is no difference between %i and %d format specifier. Let’s solve an example
#include <stdio.h>
int
main()
{
int
num = 15;
// print value for %d
printf
(
"num ans for %%d is = %d\n"
, num);
// print value using %i
printf
(
"num ans for %%i is = %i\n"
, num);
return
0;
}
- Output:
num ans for %d is = 15
num ans for %i is = 15
Now find the difference on the basis of scanf - In the case of scanf function %d suppose the base as 10 but %i detects the base. So these two specifiers work in different fashions during input specifier configuration. So in simple form 015 will be 10 for %d and 15 for %i
- Solve example
-
include <stdio.h> int main() { int X=0; int y=0; printf("Put value x: "); scanf("%d",&x); printf("Put value y: "); scanf("%i",&y); printf("x=%d, y=%d\n",x,y); return 0; } put value of x:20 put vaue of x:0x30 x=20, y=30
That is all about the Difference between %d and %i format specifier in C language all details has explained.If you have any question ask here