fredhead3968 fredhead3968
  • 21-12-2017
  • Computers and Technology
contestada

Write a program to test the difference between %d and %i conversion

Respuesta :

tonb
tonb tonb
  • 21-12-2017
void test(char *s)
{
  int i, d;
  sscanf(s, "%i", &i);
  printf("%s converts to %i using %%i\n", s, i);
  sscanf(s, "%d", &d);
  printf("%s converts to %d using %%d\n", s, d);
}

int main()
{
  test("123");
  test("0x123");
  return 0;
}

outputs:
123 converts to 123 using %i
123 converts to 123 using %d
0x123 converts to 291 using %i
0x123 converts to 0 using %d

As you can see, %i is capable of parsing hexadecimal, whereas %d is not. For printf they're the same.
Answer Link

Otras preguntas

analogies, allow:permit::find:
If a yield sign is a regular polygon what is the measure of each angle
Sylvan withdrew 2/5 of the amount in his savings account, and spent 7/10 of that money.  What fraction of his total savings does he still have?
which expression stands for 32 more than a number d
A rectangle is 5 units long. The rectangle is also 3 units wide. What is its perimeter?
I have this sentence that I have to rewrite using a possessive nounThe swimming pool at the athletic center. Please help
If a yield sign is a regular polygon what is the measure of each angle
Why do the oceans play such a large part in the water cycle
choose the plural noun that's spelled correctly?A Fathers-in-lawD Editor in chiefs
What modern day country is located where Mesopotamia was located