January 24th, 2004, 08:03 AM
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.
January 25th, 2004, 08:40 AM
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:
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.
int i = 23;
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.
var = (short*)malloc(sizeof(short));
All the buzzt
January 25th, 2004, 11:17 AM
So Athlon64s would convert shorts to 64-bit integers?
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.
January 25th, 2004, 12:16 PM
Not sure about the 64-bit processors, only about 16- and 32-bit processors.
floats get converted to double? Are you sure?
All the buzzt
January 25th, 2004, 07:54 PM
Absolutely, 100% positive. It's part of the C language standard.
January 26th, 2004, 09:07 AM
January 26th, 2004, 10:03 AM
Don't just take my word for it...
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.
January 26th, 2004, 10:08 AM
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.
All the buzzt
January 26th, 2004, 11:36 AM
If you're that trusting, then mail me $1000 and I will psychically command all bugs to leave your programs.
Seriously, do the research! Read the fourth paragraph of the following page:
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.
January 26th, 2004, 11:52 AM
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...
All the buzzt
January 26th, 2004, 12:02 PM
Thank you for the kind words!
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.