Decimal, Octal & Hexadecimal

This is a discussion on Decimal, Octal & Hexadecimal within the C Programming forums, part of the General Programming Boards category; Hi all. Can anyone help me clarify these statements. 1. C support unary minus operator for hexadecimal and octal literals ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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, 03:08 PM
  2. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 02:29 PM
  3. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 02: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, 10:07 PM

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