Thread: Decimal, Octal & Hexadecimal

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Question Decimal, Octal & Hexadecimal

    Hi all.

    Can anyone help me clarify these statements.

    1. C support unary minus operator for hexadecimal and octal literals (eg. -0xFF, -002)
    2. Negative hexadecimal will count from FFFFFFFF for 'int' type.?
    3. Negative octal will count from 377777777777 for 'int' type. ?

    Am I right?

    I just try this out.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    	int x = -1;
    	printf("%i %o %x\n", x, x, x);
    
    	int y = -0x01;
    	printf("%i %o %x\n", y, y, y);
    
    	int z = -001;
    	printf("%i %o %x\n", z, z, z);
    
    	return 0;	
    }
    
    /*
    Output :
    1 377777777777 FFFFFFFF
    1 377777777777 FFFFFFFF
    1 377777777777 FFFFFFFF
    */

    Lastly, how to print hexadecimal and octal as '-0x01' and '-001' instead of 'FFFFFFFF' and '377777777777' using printf?



    Regards,

    mie.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You would need to do something like
    Code:
    if ( x < 0 ) {
      x = -x;
      printf("-");printf("%x",x);
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think it's 37777777777 (10 x '7', not 11 x '7').

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C - decimal to hexadecimal converter
    By emblem4ever in forum C Programming
    Replies: 20
    Last Post: 01-23-2010, 04:08 PM
  2. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 03:29 PM
  3. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  4. stacks to convert from decimal to hexadecimal
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 12:24 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM