if statement with hex variable

This is a discussion on if statement with hex variable within the C Programming forums, part of the General Programming Boards category; It is possible to use hex values with the if statement e.g. insert Code: if (value == f) { ... ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    if statement with hex variable

    It is possible to use hex values with the if statement

    e.g.

    insert
    Code:
    if (value == f)
    {
     ...
    }
    where f is 15 in hex and not the ascii value. i know i could simply have value equal 15 but for readability i would prefer the hex to be displayed.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,857
    Yep. Use the 0x prefix.

    Code:
        if (value = 0x15)    /*   0x15 is 21 in decimal  */
    Also a prefix of 0 (no x) is octal - base 8. So 017 is actually the value 15 (decimal). It is a fairly common mistake for beginners to expect that 017 is in decimal, not octal.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-14-2010, 09:32 PM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  3. declare variable in case statement
    By Marksman in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 02:32 AM
  4. if statement reads char value as variable
    By yougene in forum C Programming
    Replies: 2
    Last Post: 07-29-2007, 11:16 PM
  5. embedding variable inside sql statement
    By WaterNut in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2004, 02:43 PM

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