![]() |
| | #16 | |
| Registered User Join Date: Sep 2007
Posts: 372
| Quote:
I don't know about your compiler, but on gcc 4.3.3, I get that warning when I'm not explicitly in C99 mode. The C89 standard did not have long long, and thus of course did not have long long constants. So my compiler is helpfully letting me know that what I'm doing might have issues. If I ask for C99 support (with -std=c99) the warning goes away. If you're unaware, C is an internationally standardized language with two major version: C89 and C99. C99 is newer, and contains things that C89 does not have, one of those being the long long type. So your compiler, even if it accepts long long, is probably not fully C99 conformant. As such, it's letting you know that your code, while being correct C99, is not necessarily portable to other implementations. As for 251 vs -5: when a negative integer is converted to an unsigned type, the resulting value is necessarily positive; and how it is converted is defined: -1 is the largest possible value in the unsigned type, -2 is the next largest, and so on. Assuming a typical desktop, an unsigned char is 8 bits. Thus the largest value it can take is 255. -1 converted to unsigned char is 255. An unsigned int is 32 bits. -1 converted to unsigned int is 4294967295. When you pass values to printf(), something called the "integer promotions" happens. C won't allow you to pass an integer smaller than an int to printf(). That means char (and short) need to get converted to a larger type. Assume our unsigned char and unsigned int above take the values -1 (which means their largest value). When the unsigned char is passed to printf(), it's converted to int. That means 255 is converted to int, which results in the value 255. So %d, which expects an int, received an int with the value 255, and printed that out. However, you're lying to the compiler when you pass the unsigned int and use %d. %d expects an int, not an unsigned int. This means that %d can not print out a value larger than the largest int. The unsigned int you passed is much larger than the largest int type (about twice as large). %d must then interpret the bit pattern as an int (that's its job: it has no choice in the matter). It so happens that what you passed (4294967295) looks like -1 when interpreted as an int, which is what %d does. 255 does not look like -1 when interpreted as an int, so that's why 255 printed in the first part. Perhaps this helps: Code: 255 / -1 as an unsigned char: 1111 1111 Converted to int: 0000 0000 0000 0000 0000 0000 1111 1111 The above is only -1 if the least significant 8 bits are used. As an int (signed or unsigned) it's clearly 255. 4294967295 / -1 as an unsigned int: 1111 1111 1111 1111 1111 1111 1111 1111 Converted to int: 1111 1111 1111 1111 1111 1111 1111 1111 This is ambiguous as an int: unsigned, it's 4294967295. Signed, it's -1. | |
| cas is offline | |
| | #17 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
C89 is an American standard.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #18 | ||
| Registered User Join Date: Feb 2008
Posts: 36
| Quote:
Code: long long x = 18232323232322; Quote:
@cas : ... thank you.im graceful for your explanation though i have some other questions 1) if all chars and shorts are changed the same way to ints ... then how would a signed char like Code: signed char x=-5;
printf("%d",x);
2) when we are saying unsigned int x = -5 and knowing its ranging is 0 to 4....... how come -5 is getting stored, it is not in the range ? ... can we say declaring it unsigned is useless while using the %d format specifier ? 3)And why is Code: unsigned int x;
x=-5u;
printf("%d",x);
Damn im confused .. Thank you. | ||
| BlaX is offline | |
| | #19 | |||
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Ditto. MinGW uses the Microsoft runtime, 'cause it's their operating system, and Microsoft hasn't gotten past 1989 in terms of C (when there was no such thing as "long long"). Quote:
Quote:
You are explicitly changing -5 to an unsigned value ... and then explicitly changing it right back again by printing with %d. | |||
| tabstop is offline | |
| | #20 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: signed char x=-5;
printf("%d",x);
Code: signed char x=128;
printf("%d",x);
Code: signed char x;
for(x=0;x<=255;x++)
printf("%d",x);
2) Same as 1. It'll take appropriate value from it's range which is 0-4. 3)Just change %d to %u and you'll get an appropriate value from the other side.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #21 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
You are declaring the variable x as long long. But the integer constant itself, you are declaring as an int (by omitting any type of postfix)! In later code, when you added LL after it, you got no warning, did you? You can also see it this way: How do we assign that number to the variable x (in machine code)? First, we must have some place to store it. mov eax, my_integer_constant; // <--- If your number can't fit into 32 bits, you will get a WARNING! lea ecx, x; // Put address of x into ecx mov [ecx], eax; // Store eax in *ecx. Does this make any sense? You have TWO parts of the expression, and both have different types! The first is the variable x which has type long long, and the second is the number which has type int (or long).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #22 | |
| Registered User Join Date: Sep 2007
Posts: 372
| Quote:
| |
| cas is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NEED HELP READING FILE and PRINTING | geoffr0 | C Programming | 4 | 04-16-2009 05:26 PM |
| C programing doubt | sivasankari | C Programming | 2 | 04-29-2008 09:19 AM |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Quack! It doesn't work! >.< | *Michelle* | C++ Programming | 8 | 03-02-2003 12:26 AM |
| Strings are V important... | NANO | C++ Programming | 15 | 04-14-2002 11:57 AM |