Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c++ - Printing char by integer qualifier

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...