Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to execute the below program.

#?include? "stdio.h" 
#include "string.h" 

void main()
{ 
    char c='8'; 
    printf("%d",c); 
} 

I'm getting the output as 56 . But for any numbers other than 8 , the output is the number itself , but for 8 the answer is 56.

Can somebody explain ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
370 views
Welcome To Ask or Share your Answers For Others

1 Answer

A characters that begins with represents Octal number, is the base-8 number system, and uses the digits 0 to 7. So 8 is invalid representation of octal number because 8 ? [0, 7], hence you're getting implementation-defined behavior.

Probably your compiler recognize a Multibyte Character '8' as '' one character and '8' as another and interprets as '8' as '' + '8' which makes it '8'. After looking at the ASCII table, you'll note that the decimal value of '8' is 56.


Thanks to @DarkDust, @GrijeshChauhan and @EricPostpischil.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...