-9

This is the code-

  int j;
   printf("Enter the number:");
   scanf("%c",&j);

   printf("You have enter the decimal number %i \n",j);
   printf("You have enter the octal number %o\n",j);
   printf("You have enter the hexadecimal number %x\n",j);
   printf("You have enter the character %c\n",j);
   

This is my output:

Enter the number:r
You have enter the decimal number 4201074 
You have enter the octal number 20015162
You have enter the hexadecimal number 401a72
You have enter the character r
dbush
  • 205,898
  • 23
  • 218
  • 273

1 Answers1

1

The %c format specifier to scanf expects a char * as a parameter. You're instead passing in an int *.

Using the wrong format specifier triggers undefined behavior which is why you're seeing strange results. What's probably happening behind the scenes is that only the first of the (presumably) 4 bytes of the int variable j are written to, leaving the rest uninitialized and containing whatever garbage was there previously.

Change the type of j to char and you should get the results you expect.

dbush
  • 205,898
  • 23
  • 218
  • 273