Thread: Need some Help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Question Need some Help

    Well this is my first time to the forum and im trying to finish a project for my c programming class --- anyway my question is this.

    how can i assign the ascii value of a character to a variable and vice versa?

    This is what I really wanna do--

    a user inputs "A", then the program finds the ascii of "A" (which i think is 75) and assigns it to a variable and outputs it.

    I know this might be easy for some of you folks so please take a hot second and help me out!

    thanks a million.

  2. #2
    Unregistered
    Guest
    Code:
    char c; int i;
    
    c = getchar();
    if( c > 'A' && c < 'Z' || c > 'a' && c < 'z' ){
        i = atoi(c);
        printf( "%d", i );
    }
    Of course, this assumes that your input can only be letters.

    -Prelude

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    Thumbs up GRACIAS!!

    THANK YOU!!!!

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    WAIT?

    Is there some kind of include that i have to put so that the atoi() will work?

  5. #5
    Unregistered
    Guest
    Sorry, I way overcomplicated that. That's what happens when I don't code C for a while :P This works better:
    Code:
    #include <stdio.h>
    
    int main(void){
    	char c; int i;
    
    	c = getchar();
    	i = c;
    	printf( "%d", i );
            /*Alternative*
            c = getchar();
            printf( "%d", c );
            */
    
    	return 0;
    }
    -Prelude

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    Thanks again

Popular pages Recent additions subscribe to a feed