Thread: Integers and charecters

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    14

    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

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    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/
    Last edited by NickESP; 02-09-2006 at 04:34 AM.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ...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;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    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;
    }
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

Popular pages Recent additions subscribe to a feed