C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 10:32 AM   #1
Registered User
 
Join Date: Feb 2008
Posts: 36
char and int

Hello everyone.Im having some problem understanding whats going on here ...

This code

Code:
unsigned char x;
x=-5;
printf("%d",x);
Gives an output of 5 .. no problemo

but

Code:
unsigned int x;
x=-5;
printf("%d",x);
prints out -5 ... why ?

As for my 2nd question,

Code:
long long x = 18232323232322;
printf("%d",x);
The compiler is generating a warning :
[Warning] integer constant is too large for "long" type and printing a value of 187060802.
Im on a 32-bit machine and using Dev C++ 4.9.9.2 , where the long long int is supported and is in the range –9,223,372,036,854,775,807 to 9,223,372,036,854,775,807.

Thank you.
BlaX is offline   Reply With Quote
Old 07-10-2009, 10:44 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by BlaX
Gives an output of 5 .. no problemo
hmm... are you sure you get 5 as the output? I am not sure how to explain 5, but I can explain 251, which I do get when running your sample code.

Quote:
Originally Posted by BlaX
The compiler is generating a warning :
[Warning] integer constant is too large for "long" type and printing a value of 187060802.
You should use the long long constant 18232323232322LL.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-10-2009, 10:49 AM   #3
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
I'm surprised the first one prints out -5. When you assign -5 to an unsigned char, it stores a positive value (in this case, if you have 8-bit chars, 251). In your call to printf(), x is promoted to int, which can store 251. Thus I would expect 251 to be printed.

The second case is, I believe, technically undefined behavior. %d expects an int, and you're passing an unsigned int. But what's probably happening is this: -5 gets converted to unsigned int, and if you have 32-bit ints, that means 4294967291. It so happens that -5 and 4294967291 look the same to the computer, and which you get depends on how you interpret it. %u says to interpret it as an unsigned int, %d as a signed int. So you're asking printf() to print out -5, essentially.

As for 18232323232322, you want to print out a long long with %lld, not %d. %d means int. The warning you're getting is just informational, and the initialization is correct (in C99); but you can silence it by using 18232323232322LL to explicitly tell what type of constant it is (LL meaning long long, obviously).
cas is offline   Reply With Quote
Old 07-10-2009, 11:01 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by cas View Post
...The warning you're getting is just informational...
I am not so sure.
Usually the compiler truncates the number and spits out a warning for you.
Be on the safe side - use the proper postfix to make the compiler understand.
__________________
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, 11:04 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Elysia
Usually the compiler truncates the number and spits out a warning for you.
I double checked, and apparently that is not true for a standard conforming compiler (other than the warning part).
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-10-2009, 11:05 AM   #6
Registered User
 
Join Date: Feb 2008
Posts: 36
Ohh .. sorry sorry my mistake.its printing 251.(I know the reason no need to explain.thank you)
But why is the unsigned int not printing 4294967291 and is printing -5 ?

Quote:
You should use the long long constant 18232323232322LL.
Why should i use the long long constant when im declaring x as a long long integer and assigning a value within its range.

Even trying

Code:
long long x = 18232323232322LL;
printf("%LLd",x);
Im getting 187060802 but without a warning.

Edit:

@cas:Sorry, i didnt see your post because i was writing a reply when u had already answered.

Quote:
It so happens that -5 and 4294967291 look the same to the computer, and which you get depends on how you interpret it
The same thing is for the 251 and -5 in the first part.But why is it printing 251 during char (the +ve value) and -5 for the int(the -ve value)

Quote:
you want to print out a long long with %lld, not %d
im getting the same result with %lld

Last edited by BlaX; 07-10-2009 at 11:11 AM.
BlaX is offline   Reply With Quote
Old 07-10-2009, 11:10 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by laserlight View Post
I double checked, and apparently that is not true for a standard conforming compiler (other than the warning part).
Hmm. Still, I would not rely on it, though.
And it does no harm in silencing a warning.

Quote:
Originally Posted by BlaX View Post
Why should i use the long long constant when im declaring x as a long long integer and assigning a value within its range.
Any number is treated as int by default, and thus it tries to assign an int to a long long, hence the warning.
Just as you can prefix 0x to make it hexadecimal, you can postfix numbers with with things to make it a different type. For example, .0f for float, .0 for double, etc.

Quote:
Even trying

Code:
long long x = 18232323232322LL;
printf("%LLd",x);
Im getting 187060802 but without a warning.
Use %lld. There is a difference between %LLd and %lld. C is case sensitive.
__________________
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, 11:16 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by BlaX
But why is the unsigned int not printing 4294967291 and is printing -5 ?
Implementation defined behaviour. cas has outlined the reasons.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-10-2009, 11:16 AM   #9
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
you should be using %lld not %LLd

Last edited by itCbitC; 07-10-2009 at 11:19 AM.
itCbitC is offline   Reply With Quote
Old 07-10-2009, 11:22 AM   #10
Registered User
 
Join Date: Feb 2008
Posts: 36
Quote:
It so happens that -5 and 4294967291 look the same to the computer, and which you get depends on how you interpret it
That's true.The same thing is for the 251 and -5.But why is it printing 251 during char (the +ve value of the variable) and -5 for the int(the -ve value of it) while using the %d format specifier.

Quote:
Any number is treated as int by default, and thus it tries to assign an int to a long long, hence the warning.
Where's the problem ? It is trying converting from a lower type to a higher type..

Quote:
Use %lld. There is a difference between %LLd and %lld. C is case sensitive.
Code:
long long x = 18232323232322ll;
printf("%lld",x);
Getting same answer for both(187060802) ...How the heck should i print out a long long variable ?
BlaX is offline   Reply With Quote
Old 07-10-2009, 11:22 AM   #11
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Perhaps this might help to understand what laserlight and cas mean by "implementation defined behavior".
Code:
unsigned int x;
x=-5;
printf("%d  %u", x, x);
itCbitC is offline   Reply With Quote
Old 07-10-2009, 11:25 AM   #12
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by BlaX View Post
Where's the problem ? It is trying converting from a lower type to a higher type..
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.
__________________
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.

Last edited by Elysia; 07-10-2009 at 11:27 AM.
Elysia is offline   Reply With Quote
Old 07-10-2009, 11:25 AM   #13
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by BlaX View Post
Getting same answer for both(187060802) ...How the heck should i print out a long long variable ?
Using MinGW (dev-c++/code::blocks on windows)? If so as I recall you have to use Microsoft's %I64d.
tabstop is offline   Reply With Quote
Old 07-10-2009, 11:27 AM   #14
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by BlaX
But why is it printing 251 during char (the +ve value of the variable) and -5 for the int(the -ve value of it)
Because 251 as a char can be represented as an int, but -5 as an unsigned int cannot be represented as an int, hence implementation defined behaviour is involved.

Quote:
Originally Posted by BlaX
How the heck should i print out a long long variable ?
Oh yeah, you're using MinGW, so I believe it should be:
Code:
long long x = 18232323232322ll;
printf("%I64d",x);
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way

Last edited by laserlight; 07-10-2009 at 11:38 AM. Reason: Interesting... how did that text get there?
laserlight is offline   Reply With Quote
Old 07-10-2009, 11:36 AM   #15
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by laserlight View Post
BlaX is online now Add Infraction for BlaX Report Post IP Edit/Delete Message
Now we know what godlike powers the moderators have! :P
tabstop 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 05:30 PM.


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