Doubt with the %i

Asked

Viewed 43 times

0

I know that with %i you can read numbers in decimal, hexadecimal, and octal, but when I type 0x15, the number 21 appears, being that it was fifteen that was appear or I am mistaken ?

Example below

#include <stdio.h>

int main(int argc, char** argv)
{
   int n = 0x15;
   printf("%d\n",n);
  return 0;
}

1 answer

1


0x15 in hexa = 21 decimal.

The specifiers %i and %d are interpreted in the same way by the functions of the fprintf() family, but are interpreted differently by the fscanf function family().

Using %d implies that the user entered value will be interpreted as a decimal integer.

Using %i allows the user to enter values on the basis of 8, 10 and 16.

When you type the printf you use %d, so the result was decimal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.