【发布时间】:2017-08-07 20:07:44
【问题描述】:
我在谷歌上搜索了很多文章以寻找以下问题,但仍然没有找到任何好的答案。 从我的第一眼看,我可以得到 int x:3 这里 3 是宽度,如果我们分配大于 3 的 x 值会打印一些负值,如果我们分配小于 3 的值,则分配给 x 的值会正确打印。 谁能解释一下这段代码的输出是怎么来的。
#include<stdio.h>
1 struct Point
2 {
3 int x:3, y:4;
4 };
5
6 int main()
7 {
8 struct Point p1 = {6,3};
9
10 printf ("x = %d, y = %d", p1.x, p1.y);
11
12 return 0;
13 }
The output comes as:
x = -2, y = 3
when i included stdio.h header file warning vanished but the output of x get chnaged first without header file the value of x is coming -4 now with stdio.h header file the value of x becomes -2.Why this is happening??
提前致谢!!
【问题讨论】:
-
请以可读的方式格式化您的代码。在当前状态下,您可能只会收到反对票
-
您没有足够的位来存储您的值。
-
您显示的警告之一与您显示的代码不匹配。请创建一个Minimal, Complete, and Verifiable Example(不带行号,使用 cmets 标记错误或警告所在的行)并显示该代码中的错误。
-
@Someprogrammerdude,我已经在 c 编译器上编译了这段代码,它只显示了这个警告,我自己没有。
-
不,您还没有编译您显示的代码。警告包含有关第 11 行分配的消息,但您显示的源在第 11 行没有这样的分配。此外,
printf调用不在第 12 行报告。请编辑您的问题以包含您使用的实际代码。