简体   繁体   中英

why does printf auto convert %x %o %d to hex oct and dec

Some how I thought this should have worked.

#include<stdio.h>
void main(void)
{   
     short i;
     scanf("0x%hi",&i);
     printf("0x%x\n",i);
     printf("0%o\n",i);
     printf("%d\n",i);
}

Wonder why ?Any answers , why this doesnt work. I thought this should auto do the conversion into hex oct and atleast it should print the decimal correctly

  32768
  0x26
  046
  38

Is what I get for 32768 input .

short is probably 16-bit on your platform, in which case 32768 doesn't fit. Try using a bigger type instead.

Demo: http://ideone.com/D0hzP .

sorry , I did a silly mistake. But it really helps and gives courage and comfort when u ask a question.

      scanf("%hi",&i);

Removing 0x fixed this. Thanks for the help all of you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM