![]() |
| | #1 |
| Registered User Join Date: Feb 2008
Posts: 36
| char and int This code Code: unsigned char x;
x=-5;
printf("%d",x);
but Code: unsigned int x;
x=-5;
printf("%d",x);
As for my 2nd question, Code: long long x = 18232323232322;
printf("%d",x);
[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 | |
| | #2 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
__________________ 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 | |
| | #3 |
| 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 | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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:
| |
| Elysia is offline | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ 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 | |
| | #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:
Even trying Code: long long x = 18232323232322LL;
printf("%LLd",x);
Edit: @cas:Sorry, i didnt see your post because i was writing a reply when u had already answered. Quote:
Quote:
Last edited by BlaX; 07-10-2009 at 11:11 AM. | |||
| BlaX is offline | |
| | #7 | ||||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
And it does no harm in silencing a warning. Quote:
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:
__________________ 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 | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ 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 | |
| | #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 | |
| | #10 | |||
| Registered User Join Date: Feb 2008
Posts: 36
| Quote:
Quote:
Quote:
Code: long long x = 18232323232322ll;
printf("%lld",x);
| |||
| BlaX is offline | |
| | #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 | |
| | #12 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
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:
Last edited by Elysia; 07-10-2009 at 11:27 AM. | ||
| Elysia is offline | |
| | #13 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #14 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
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 | |
| | #15 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop 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 |