C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 01:18 PM   #16
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
Quote:
Where's the problem ? It is trying converting from a lower type to a higher type..
There's no problem as far as C (C99, specifically) is concerned. Integer constants in your program are by default int; but if an integer constant can't fit into an int, then it's a long; if it can't fit into a long int, then it's long long. So since your 18232323232322 doesn't fit into an int or a long on your system, it has the type long long int. That's fine and dandy, but your compiler is (perhaps helpfully) letting you know that it's too large to be a long.

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   Reply With Quote
Old 07-10-2009, 02:09 PM   #17
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by cas View Post
If you're unaware, C is an internationally standardized language with two major version: C89 and C99.
There is no C89 international standard. There is only C90 and C99.
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-10-2009, 07:06 PM   #18
Registered User
 
Join Date: Feb 2008
Posts: 36
Quote:
Originally Posted by Elysia View Post
That's not it.
Your constant 18232323232322 is too large for an integer. It cannot be stored in 32 bits. Thus, it is not an integer type; it cannot be, for it is too large to be held in an integer.
It's like saying to the compiler: "Hi, this is my number and its type is an integer." And the compiler replies: "But your number is too large to be of type integer, because it will not fit."
This is before it is assigned to the long long type, note.
"It cannot be stored in 32 bits" , but im already declaring it as a long long integer and .. why should it post a warning ?

Code:
long long x = 18232323232322;
Quote:
Originally Posted by laserlight View Post
Oh yeah, you're using MinGW, so I believe it should be:
Code:
long long x = 18232323232322ll;
printf("%I64d",x);
why %I64d .. why isn't %lld working ?

@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);
cause a negative to print out ?

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);
printing -5 ... im explicitly changing -5 to an unsigned value ...

Damn im confused ..
Thank you.
BlaX is offline   Reply With Quote
Old 07-10-2009, 07:40 PM   #19
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by BlaX View Post
"It cannot be stored in 32 bits" , but im already declaring it as a long long integer and .. why should it post a warning ?
Because for, ooh, about two-thirds of its lifetime the C language didn't have anything longer than 32 bits. (Also hint: 1823232323232322 is not explicitly typed -- you most certainly have not declared it as a long long integer.)

Quote:
Originally Posted by BlaX View Post

why %I64d .. why isn't %lld working ?
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:
Originally Posted by BlaX View Post

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);
cause a negative to print out ?
... Because it's changed to an int? If it wasn't converted to an int, you'd get the equivalent of 0x000000FB, also known as 251. It is converted to an int, that is we get an int with the same value, not with the same bits.
Quote:
Originally Posted by BlaX View Post


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 ?
What?
Quote:
Originally Posted by BlaX View Post

3)And why is

Code:
unsigned int  x;
x=-5u;
printf("%d",x);
printing -5 ... im explicitly changing -5 to an unsigned value ...

Damn im confused ..
Thank you.
You are explicitly changing -5 to an unsigned value ... and then explicitly changing it right back again by printing with %d.
tabstop is offline   Reply With Quote
Old 07-10-2009, 10:31 PM   #20
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by BlaX View Post
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);
cause a negative to print out ?

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);
printing -5 ... im explicitly changing -5 to an unsigned value ...

Damn im confused ..
Thank you.
1).
Code:
signed char x=-5;
printf("%d",x);
Considering the char to be of 8bits, The range of signed char is -128 to 127. Now if you give a signed char any value in this range, it'll print it as it is. But if you give the signed char a value which is not in it's range for eg. 255, then it'll print any appropriate number from the other side. In your case you are printing -5 which is in the range of -128 to 127 that's why it prints -5, but try your hands on any number not in this range then you'll get to know what's happening. Let me give you another example.
Code:
signed char x=128;
printf("%d",x);
As I said here x is not in range of -128 to 127, now what happens is that it takes the appropriate number from the other side of the range which is -128, if x would have been 129, output then would be -127 and so on. Try doing this
Code:
signed char x;
for(x=0;x<=255;x++)
printf("%d",x);
It'll result in an indefinite loop because of the same reason that x is never going above 127. As it increments to 129 it goes to the negative side and thus process gets repeated.

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   Reply With Quote
Old 07-11-2009, 07:40 AM   #21
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by BlaX View Post
"It cannot be stored in 32 bits" , but im already declaring it as a long long integer and .. why should it post a warning ?
No, you are not declaring it as long long!
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2009, 12:24 PM   #22
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
Quote:
The first is the variable x which has type long long, and the second is the number which has type int (or long).
If the compiler follows C99 at all, then the constant is not an int or a long, but a long long, assuming it doesn't fit into an int or a long. There's nothing wrong with explicitly making it long long with LL, especially if your compiler is, as the OP's appears to be, in C89 mode with extensions. But the type of the constant is not int or long.
cas is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22