c - 为什么以及如何在 C 语言中从 -52 开始未初始化的字符?

标签 c visual-studio-2012

正如标题所说。我的印象是 char 与 short short unsigned int 相同,允许 0 到 255 之间的任何值。根据 Visual Studio 上的调试器,每当我声明一个 char,它从 -52 'Í' 开始。为什么会这样,这怎么可能?

最佳答案

未指定 char 在您的情况下是有符号的还是无符号的,它是有符号的,这在 C99 标准草案部分 6.2.5 Types 中有所介绍,它说:

The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.35)

未初始化的自动变量具有不确定的值,因此它们的初始值是不可预测的。

由于我们谈论的是 Visual Studio,它会根据维基百科文章 Magic number (programming) 出现它使用 CC 来标记未初始化的堆栈内存:

Used by Microsoft's C++ debugging runtime library and many DOS environments to mark uninitialized stack memory. CC resembles the opcode of the INT 3 debug breakpoint interrupt on x86 processors.

这可以解释您看到的 -52 但这不应该被认为是可靠的并且不可移植。应该注意的是,使用不确定的值会调用未定义的行为。

关于c - 为什么以及如何在 C 语言中从 -52 开始未初始化的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269426/

相关文章:

c++ - 从字符串到函数的映射中存在语法错误

c# - 在调试中忽略方法的属性 -

c - while 循环在我的 C 程序中不起作用

c - 为什么 _Generic 语句被视为表达式而不是宏?

c - 使用指针到指针的方法读取和打印矩阵

c - 数组和指针链

visual-studio-2012 - Visual Studio 是如何对 App_Code 文件夹进行特殊处理的?

visual-studio - Visual Studio 2012 CPU 使用率高且无法调试

c# - 无法应用项目 X 的​​发布属性

c - 解析类函数的宏和参数