[C] declare a hexadecimal variable

____22 Posted messages 101 Status Member -  
stephbb75 Posted messages 620 Registration date   Status Member Last intervention   -
Hello,

how to declare a hexadecimal variable in C

int x = 0x10;

what should I put instead of int? byte?

does x equal 10 or 16 in this case?

thanks for the help
Configuration: Windows XP Internet Explorer 7.0

2 answers

  1. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
     
    Hello,
    Your variable will not have a specific base. In memory, the number is stored in binary.
    It’s at display time that you can request the output in a certain base (16 for example).
    When you do int x = 0x10; you are telling the compiler that the number you are entering is in base 16. It will store it in binary in memory. If you request a decimal display (printf("%d", x); you will get 16.
    If you use printf("%x", x); it will be 10 (hex display).
    --

    Google is your friend
    13
  2. stephbb75 Posted messages 620 Registration date   Status Member Last intervention   122
     
    Hello,

    You can't declare a Hex variable in C, you declare integers (int, long, ...) or floats, doubles, ...
    Then you can work with hex.

    In your case, int x = 0x10; you have the Hex value 10 inside, which corresponds to 16 in base 10, "10000" in binary ....

    Steph
    2