通过整数限定符打印字符 [英] Printing char by integer qualifier

查看:61
本文介绍了通过整数限定符打印字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下程序。

I am trying to execute the below program.

#‎include‬ "stdio.h" 
#include "string.h" 

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

我得到的输出为 56 。但是对于 8 以外的任何数字,输出本身就是数字,但是对于 8 ,答案是 56

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.

有人可以解释吗?

推荐答案

\0 代表总数字,是以8为底​​的数字系统,并使用数字 0到7 。因此 \08 无效表示nofollow noreferrer>八进制数字,因为 8∉[0,7] ,因此您将获得实现定义的行为。

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

您的编译器可能识别出多字节字符 '\08''\0'一个字符和'8'作为另一个,并解释为'\08''\0' + '8'使其变为'8'。查看 ASCII 表后,您会注意到的十进制值8'是56。

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

感谢@ DarkDust,@ GrijeshChauhan和@EricPostpischil。

Thanks to @DarkDust, @GrijeshChauhan and @EricPostpischil.

这篇关于通过整数限定符打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆