thanx a lot guys. anyway, scanf("%hd",(short*)a) works pretty fine, and I aggre that it's a matter of scanf NOT printf(). printf displays the content of a even when using %d.
Printable View
thanx a lot guys. anyway, scanf("%hd",(short*)a) works pretty fine, and I aggre that it's a matter of scanf NOT printf(). printf displays the content of a even when using %d.
The compiler upgrades all arguments to multiples of the machine word size (32 bits on the usual 32-bit PC), which means that you're quite lucky it works this way. Because the compiler doesn't know that you want an int. E.g. if you do this:Quote:
Thinking about it, the original printf("%d") statement is entirely correct. There is no need for %hd in there nor for a conversion to int, since the compiler will upgrade the dereferenced value of var to an int automatically. So it's just the scanf that's wrong.
you're in trouble. i64 is an MS-specific sign for a 64-bit integer, but you only provide 32 bits.Code:int i = 23;
printf("%i64", i);
A pointer as passed to scanf is already 32 bits, that's why no enlargening is done.
On a side note, this line in the original code:
doesn't need the short* cast. malloc returns a void* and var is a void*, everyone's satisfied. The cast has no runtime effect anyway, it's only a sign for the compiler.Code:var = (short*)malloc(sizeof(short));
So Athlon64s would convert shorts to 64-bit integers?Quote:
Originally posted by CornedBee
The compiler upgrades all arguments to multiples of the machine word size (32 bits on the usual 32-bit PC), which means that you're quite lucky it works this way.
And why do floats get converted to doubles then? According to your argument, there should be no need to promote a double.
Honestly, if this was the intended behavior, then code that ran on a Pentium would have completely different behavior than that compiled for an Athlon64 -- even though they run the same OS.
The reason the short is promoted to an int is because of the rules of the C language, not because of the OS. Usually, int's contain a single machine word -- but not always. The previous version of National Instruments' C compiler assumed an int was 16 bits, not 32 bits -- even though it compiled code for 32-bit Windows. For this compiler, shorts didn't need to be promoted, because a short was equivalent to an int -- despite the fact that the machine word size was 32-bits.
Not sure about the 64-bit processors, only about 16- and 32-bit processors.
floats get converted to double? Are you sure?
Absolutely, 100% positive. It's part of the C language standard.
Ok then.
I've seen many people be "100% positive" about a subject before and be 100% wrong. Don't just take my word for it. Do some research yourself. Do a search on Google. Pick up a copy of "The C Programming Language 2nd edition" and look for promotions in variable argument length functions. If I remember correctly, there is a section on printf() and they may cover such things there. Read some other books on C. Or just go get a copy of the standard and read it. Or even write a program to test my statements.
Anyway, my point is: do the reasearch -- don't just take the word of one person.
- Kevin
I don't have a copy of the C standard (know any legal way of getting a cheap or free one?), but if you say it's there, I trust you.
If you're that trusting, then mail me $1000 and I will psychically command all bugs to leave your programs. :D
Seriously, do the research! Read the fourth paragraph of the following page:
http://216.239.41.104/search?q=cache...hl=en&ie=UTF-8
It says that variable argument lists convert arguments to int, unsigned int, pointer, or double. Of coarse, I am a little skeptical about this page too, since it seems to imply that long integers cannot be passed as a variable-length argument.
I trust other's knowledge if they before have shown it (and I've seen you answer other questions), I don't trust other's benevolence, or magical abilities for that matter ;)
What I need is the C standard, but I don't really want to buy it...
Thank you for the kind words! :)
Darn! ;) :D
and,
I understand!