C Programming

How to Use %i and %d to Print Integer in C Using printf()

In C programming language, the format specifiers play a crucial role as they help you in displaying a value at the output. These format specifiers are used with the printf() function to specify the type of data that needs to be output. One of the widely used format specifiers in the C programming language are %i and %d which are used to print the integers.

If you don’t know about the %i and %d format specifiers, follow this article’s guidelines.

%i Format Specifier

In C programming language, the %i is used to print out a signed integer when used with the printf() function. The signed integers are the ones that can either be positive or negative. In C programming when using of %i the argument value that is passing must be an expression or an integer type which automatically evaluates in an integer datatype. You can also use the %i specifier to read a character value that comes within the range of a signed integer.

%d Format Specifier

The %d format specifier on the other hand is used to print out the unsigned integer in the C programming language. Here unsigned integers are positive integers. In C language when you use %d then the value of the arguments type int.

Differences

Both %i and %d format specifiers are frequently used in the C programming language for printing integers. They may look similar in terms of use as well as behave the same when using the printf() function.

Let’s follow up with a sample code where both format specifiers are used.

#include <stdio.h>

int main() {

  int digit;

printf("Please Enter any digit: ");

scanf("%i", &digit);

printf("\n The digit is: %i\n " , digit);

printf("The digit is: %d\n ", digit);

  return 0;

}

In this code we firstly initialize a variable and take value from the user using scanf(). Then we show output using %d and %i.

Output:

Both provide a similar output when used with the printf() function. It doesn’t matter whether you enter the positive value or negative, the result will always be the same.

However, if we look at the scanf function, we can differentiate them correctly. In the above code, if we input a decimal value like “012”. If we use “%d” instead of %i, you will get the output 12. The reason is that %d ignores the 0 zero value from the user input.

#include <stdio.h>

int main() {

  int digit;

printf("Please Enter any digit: ");

scanf("%d", &digit);

printf("\n The digit is: %i\n " , digit);

printf("The digit is: %d\n ", digit);

  return 0;

}

Output

If the same value is entered using %i as in scanf() function, the output will be 10 in this case. The reason is %i prints the decimal value of 012 (octal representation).

#include <stdio.h>

int main() {

  int digit;

printf("Please Enter any digit: ");

scanf("%i", &digit);

printf("\n The digit is: %i\n " , digit);//shoes output

printf("The digit is: %d\n ", digit);//shoes output

  return 0;

}

Conclusion

Format specifier is a pattern that initially starts with the % sign and it usually tells us what kind of data is placed in input and what data is to be shown. %d and %i are two specifiers widely used in C programming. They are similar when used with the printf() function but they output different results when used with the scanf() function.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.