char and int

This is a discussion on char and int within the C Programming forums, part of the General Programming Boards category; Hello everyone.Im having some problem understanding whats going on here ... This code Code: unsigned char x; x=-5; printf("%d",x); Gives ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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
    Version Control System: Bazaar

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

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    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).

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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
    Version Control System: Bazaar

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

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    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 ?

    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.

    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)

    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.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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
    Version Control System: Bazaar

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

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    you should be using %lld not %LLd
    Last edited by itCbitC; 07-10-2009 at 11:19 AM.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    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.

    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..

    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 ?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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);

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Last edited by Elysia; 07-10-2009 at 11:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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);
    Last edited by laserlight; 07-10-2009 at 11:38 AM. Reason: Interesting... how did that text get there?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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