简体   繁体   English

c中的short和int

[英]short and int in c

short int a,b,c;
scanf("%d%d",&a,&b);
c = a + b;
printf("%d %d %d",a,b,c);

Input: 5 8 输入: 5 8

Output: 0 8 8 输出: 0 8 8

Why is the value of a 0? 为什么值a 0? Can any one explain this? 任何人都能解释一下吗?

Platform --- GCC ubuntu 10.04 平台--- GCC ubuntu 10.04

scanf with a "%d" format requires an int* argument. 具有"%d"格式的scanf需要int*参数。 You're giving it a short int* argument, thus your program's behavior is undefined. 你给它一个short int*参数,因此你的程序的行为是未定义的。

If you actually want to know why you're getting the results you're getting, I can speculate on that, but it's much easier just to correct your code: 如果你真的想知道为什么你得到你得到的结果,我可以推测,但更正你的代码更容易:

scanf("%hd%hd", &a, &b);

You can continue to use "%d" for printf , since the short int arguments are promoted to int . 您可以继续对printf使用"%d" ,因为short int参数被提升为int You could use "%hd" with printf , but it's not necessary. 您可以在printf使用"%hd" ,但这不是必需的。 (There is no similar promotion of short int* arguments to int* .) (对于int*没有类似的short int*参数的提升。)


You can safely stop reading here. 你可以放心地在这里停止阅读。


The following is some speculation about what's probably happening in your incorrect code. 以下是关于您的错误代码中可能发生的事情的一些猜测。 This is not a solution; 这不是解决方案; the solution is to correct your code so it does what you want it to do. 解决方案是纠正您的代码,使其完成您希望它执行的操作。 But might be instructive to see just how incorrect code can misbehave. 但是,如果错误的代码行为不当,可能会有所帮助。

Assume short is 16 bits and int is 32 bits, which is typical. 假设short是16位, int是32位,这是典型的。 The first "%d" in the format string tells scanf to read a value (you gave it 5 ) and store it into a 32-bit int pointed to by the second argument, &a . 格式字符串中的第一个"%d"告诉scanf读取一个值(您给它5 )并将其存储到第二个参数&a指向的32位int中。 Since a is only 16 buts, it will store half the 32-bit value in a and the other half in some adjacent chunk of memory. 由于a只有16个buts,它会将一半的32位值存储在a ,另一半存储在一些相邻的内存块中。 The second "%d" does the same thing with &b ; 第二个"%d"&b做同样的事情; it stores half of the 32-bit representation of 8 in b , and the other half somewhere else. 它存储的32位表示的一半8b ,而另一半在别处。

Based on your output, it appears that the second "%d" caused scanf to store the low-order 16 bits of the value 8 in b , and the high-order 16 bits (with value 0) in a , overwriting the value stored by the first "%d" . 根据您的输出,似乎第二个"%d"导致scanf将值8的低16位存储在b ,而高阶16位(值为0)存储在a ,覆盖存储的值由第一个"%d" Note that the high-order 16 bits from the first "%d" were probably stored somewhere else, perhaps clobbering some other variable or perhaps writing to some otherwise unused memory. 请注意,第一个"%d"中的高16位可能存储在其他位置,可能会破坏其他一些变量,或者可能写入一些未使用的内存。

So the result is that you've stored 0 in a and 8 in b , which explains the output you're getting. 所以结果是,你已经存储在第0 a和8 b ,这说明你得到的输出。

All this is very speculative, and many many other results are possible. 所有这些都是非常具有推测性的,许多其他结果都是可能的。 This kind of analysis is useful only for tracking down the behavior of incorrect code, with the goal of correcting it. 这种分析用于跟踪错误代码的行为,目的是纠正它。 Writing code that deliberately takes advantage of this kind of thing is an extraordinarily bad idea. 编写故意利用这种东西的代码是一个非常糟糕的主意。 The language says absolutely nothing about what incorrect code like this will do; 该语言完全没有说明像这样的错误代码会做什么; its behavior can vary wildly on different systems, with different compiler settings, or even depending on the phase of the moon. 它的行为可能在不同的系统上有很大的不同,有不同的编译器设置,甚至取决于月亮的相位。

According to this: 根据这个:

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

you need to specify a modify if your variable is a "short" int and not a regular int 如果您的变量是“short”int而不是常规int,则需要指定修改

In VC++ 6.0, The value of c is 13. 在VC ++ 6.0中, c值为13。

#include <stdio.h>

int main()
{

short int a,b,c;
 scanf("%d%d",&a,&b);
 c = a + b;
 printf("%d + %d = %d\n",a,b,c);


return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM