September 20th, 2003, 09:45 PM
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
September 20th, 2003, 10:06 PM
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'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.
September 20th, 2003, 10:11 PM
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
month = January
September 20th, 2003, 10:19 PM
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.
September 20th, 2003, 10:56 PM
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.