Integers and charecters

• 02-09-2006
anilk
Integers and charecters
Hi ,

is there any way by which i can check for a Charecter values from an integer

wht i mean to say is

int m ;

scanf("%d",&m);
now i compare this m with a charecter "y" is that possible

regards
Anil
• 02-09-2006
NickESP
Characters and Ints are both used to store numbers. The difference is in how those numbers are used by the compiler. For an 'int', the number is used. For a 'char', the number relates to one from the ASCII character set. The compiler uses the number stored in the 'char' to find which ASCII character to use instead.

Code:

```int a; a = 64; char b; b = 64; cout << a << "\n" << b;```
This will print the integer stored in 'a', and then whatever ASCII character has that number. In this case, it will print the '@' character.

Remember, chars are just 'small' 'ints'. They store numbers, but the number is used to interpret other information.

Here is an ASCII lookup table:
http://www.lookuptables.com/
• 02-09-2006
major_small
...I'm guessing that you wanted a C++ method, since this is the C++ board...
Code:

```#include <iostream> int main() {         char ch='A';         int i=static_cast<int>(ch);                 std::cout<<static_cast<char>(i)<<std::endl;         return 0; }```
or you could create your own cast (don't do this):
Code:

```#include <iostream> union trunCast {         char ch;         int i[sizeof(int)-sizeof(char)+1]; }; int main() {         trunCast inch;         inch.ch=0x0;         inch.i[0]=65;                 std::cout<<inch.ch<<std::endl;         return 0; }```
• 02-09-2006
NickESP
If you want to compare an 'int' to a 'char', go ahead and do it. They are both integers.
Code:

```#include <iostream> using namespace std; int main(int argc, char *argv[]) {     int a;     a = 64;     char b;     b = 64;     if(a == b){         cout << "The numbers are equal.\n";     }     return 0; }```