-
Character to Ascii
Hello there, I'm stuck trying to convert any scanned character to ascii. I know that printf("%d", a); will print the value of the letter a (that is 74 or 75), but i can't get to save it.
This is where I'm at
Code:
int x;
char letter;
print("enter a letter\n");
scanf("&c", letter);
x = (int)letter;
but that doesn't seem to work.
any input is highly appreciated,
thanks.!
-
Change scanf("&c", letter); to
scanf("&c", &letter);
-
are you sure the ASCII value of a is 74 or 75? this code prints 97.
Code:
char letter;
printf("enter a letter\n");
scanf("%c", &letter);
printf("%d", letter);
-
He may be talking hex, but I think that's in and around the 60's ...
-
I believe a is 0x61 in hex.
-
yeah sorry. it's 97. I think 74 was for 'A'. but yeah I did have that code..
I just can't figure out how to save what's being printed as an integer x or (in the case of scanning a, x should be set to 97).
thanks for the quick replies too.
-
The character has an integer value.
Code:
#include <stdio.h>
int main(void)
{
char letter = 'a';
printf("letter = '%c' (using %%c)\n", letter);
printf("letter = %d (using %%d)\n", letter);
printf("letter = %#x (using %%x)\n", letter);
return 0;
}
/* my output
letter = 'a' (using %c)
letter = 97 (using %d)
letter = 0x61 (using %x)
*/