#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    12

    Question question


    i got a int with a value and want to make it hold a char value is there a way to this this with the same variable i got (the int)

    i want to make theint if its 1 = a
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Actually, int and char can be used pretty much interchangeably. In fact, a number of the standard C library functions for handling a character declares it as an int. The only difference is that a char is a single byte and an int is multiple bytes.

    If you use an arithmetic operator on a char, then C will use its ASCII value as its numeric value. If you use int as a character then C will use its value as the ASCII code for the character in question (eg, int i = 65; printf("%c",i); // will print out an 'A' ).

    Similarly, you can get the ASCII code for a numeric digit thus:
    ascii_code = num + '0';
    In that line, the value of the int num is added to the ASCII code for a zero ('0'), which is 0x30 (note the hex notation). If num was 5, then ascii_code will be 0x35, the ASCII code for '5'.
    (NOTE: sorry about throwing the hex notation at you here, but it re-enforces the thing about octal and hex in the other thread. Plus the ASCII codes I have memorized are in hex, not decimal, because I normally only deal with them when reading hex dumps. Hex is sometimes much easier to work with than decimal, especially when you need to read individual bits within the number.)

    Does this answer your question?


    i want to make theint if its 1 = a
    I'm sorry, but I do not at all understand what that means. Although I know a few human languages, I still cannot understand a statement that is supposed to be in English unless it is expressed in English.
    Last edited by dwise1_aol; September 20th, 2003 at 10:13 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    12
    reason i asked that was that my case i done was incorrect it was telling me that i wasn't allowed to use a character... weird problem here's a sample

    Code:
    witch (month)
    	case '1':
    	month = January
    	break;
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by Angelo
    reason i asked that was that my case i done was incorrect it was telling me that i wasn't allowed to use a character... weird problem here's a sample

    Code:
    witch (month)
    	case '1':
    	month = January
    	break;
    OK. What is the data type of month? What is its numeric value?

    If it is an int and its numeric value is 1, then it is quite true that (1 != '1'). The numeric value of '1' is its ASCII code which is 0x31 (49 decimal).

    Just as a programming practice, your case values should be of the same data type as the variable being tested. Though if you were getting a compile error, then your compiler is doing much stricter checking than most I've used.
    Last edited by dwise1_aol; September 21st, 2003 at 11:40 AM.
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Your subject title is unacceptable. You can refer to the sticky thread at to top of this forum for more information on how to post a question.

    In the future, please use more informative subject titles.

IMN logo majestic logo threadwatch logo seochat tools logo