简体   繁体   中英

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

Output: 0 8 8

Why is the value of a 0? Can any one explain this?

Platform --- GCC ubuntu 10.04

scanf with a "%d" format requires an int* argument. You're giving it a short int* argument, thus your program's behavior is undefined.

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 . You could use "%hd" with printf , but it's not necessary. (There is no similar promotion of short int* arguments to 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. 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 . 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. The second "%d" does the same thing with &b ; it stores half of the 32-bit representation of 8 in b , and the other half somewhere else.

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" . 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.

So the result is that you've stored 0 in a and 8 in b , which explains the output you're getting.

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/

you need to specify a modify if your variable is a "short" int and not a regular int

In VC++ 6.0, The value of c is 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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