printing char in hexa decimal

This is a discussion on printing char in hexa decimal within the C Programming forums, part of the General Programming Boards category; where char is 1 byte in 32 bit system weather it is signed or unsigned. from this bellow program why ...

  1. #1
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56

    printing char in hexa decimal

    where char is 1 byte in 32 bit system weather it is signed or unsigned.
    from this bellow program why it is printing it as 32 bit number when it is signed ? I'm running this program on 32 it w7.

    Code:
    #include<stdio.h>
    
    
    int main()
    {
     unsigned char a;
    
      a= 15;
      printf("%X",a);
      a= 255;
      printf("\n%X",a);
    
        return 0;
    }
    out put
    F
    FF



    Code:
    #include<stdio.h>
    
    
    int main()
    {
     char a;
    
      a= 15;
      printf("%X",a);
      a= 255;
      printf("\n%X",a);
    
        return 0;
    }
    F
    FFFFFFFF

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    With printf, like all functions with variable argument lists, chars (and shorts) are widened to ints before the function is called. (And floats are widened to doubles, which is why the format %f works for either floats or doubles.)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Because 255 doesn't fit in a signed char without making the result look exactly like -1.

    So when the type promotion of a signed char to a signed int takes place (when you call printf), you end up with FFFFFFFF (which is -1 in an int).

    A good compiler would have warned you on the assignment - did yours?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by Salem View Post
    Because 255 doesn't fit in a signed char without making the result look exactly like -1.

    So when the type promotion of a signed char to a signed int takes place (when you call printf), you end up with FFFFFFFF (which is -1 in an int).

    A good compiler would have warned you on the assignment - did yours?
    I'm using (MinGw) gcc compiler but it didn't warned me.
    but gcc is a good compiler.
    Last edited by sagar474; 03-21-2012 at 01:05 PM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    The option -Wall for example is very useful because it highlights this and other issues. See Warning Options - Using the GNU Compiler Collection (GCC)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    gcc will warn you, but you need additional flags.

    In the version of MinGW that comes with code blocks, I used
    -Wall -Wextra -pedantic

    ...\test\main.c||In function 'main':|
    ...\test\main.c|10|warning: overflow in implicit constant conversion|
    ||=== Build finished: 0 errors, 1 warnings ===|
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print to hyperterminal in Hexa (Or Decimal)
    By pedropbr in forum C Programming
    Replies: 2
    Last Post: 11-03-2010, 10:03 PM
  2. Conversion of char for hexa!?!?
    By darkducke in forum C Programming
    Replies: 2
    Last Post: 05-29-2008, 09:40 PM
  3. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 07:39 AM
  4. Char Hexa Conversion for socket raw!HELP ME PLEASE!
    By gls2ro in forum C Programming
    Replies: 1
    Last Post: 05-04-2004, 06:41 AM
  5. about hexa-decimal numbers
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2002, 12:36 PM

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