Reputation: 175
I am trying to learn C, and I'm trying to cast a char
into an int
to extract the ASCII value. However, when I try to compile this in GCC, I get this warning:
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
ascii = (int) letter;
I'm trying to do a simple program that asks for a single character, scans for it, then casts it as an int to get the ASCII value, printing that value. I've initialized the letter
variable as char*
and ascii
as int
. I've tried using the placeholders%s
, %c
, and %1s
for the letter
variable, but it isn't work.
Here is the code:
#include <stdio.h>
char* letter;
int ascii;
int main(){
printf("Please input a charcter:");
scanf("%s", letter);
ascii = (int) letter;
printf("\n The ASCII value of %s is %d.\n", letter, ascii);
}
What I'd expect to happen is that "Please input a character:"
would print, then a character, such as a
, would be typed, and then it would print, for example, `"The ASCII value of a is 97."
Instead, when I type something, it prints "The ASCII value of (null) is zero."
During compilation, it prints out the error listed above. This is not what's supposed to happen.
How can I fix this problem?
Upvotes: 0
Views: 264
Reputation:
Just use %hhd
format specifier
#include <stdio.h>
int main(void)
{
char letter;
fputs(stdout, "Please input a charcter:");
letter = getchar();
printf("\nThe ASCII value of %c is %hhd.\n", letter, letter);
return 0;
}
Upvotes: 0
Reputation: 1685
You aren't casting a char to an int, you are casting a pointer to a char to an int.
int main(){
// define as an actual char, not a char*
char letter;
int ascii;
printf("Please input a character:");
// scan a character, not a string. Pass in the address of the char
scanf("%c", &letter);
ascii = (int)letter;
printf("\n The ASCII value of %c is %d.\n", letter, ascii);
}
Upvotes: 1
Reputation: 685
#include <stdio.h>
//char* letter; //moving to stack from data segment
int ascii;
int main(){
char letter[10] = {0};//allocing 10 bytes here, all initialized null chars
printf("Please input a charcter:");
scanf("%c", &letter); //not checking return value here, bad form
ascii = (int) letter[0]; //casting only the first char
printf("\n The ASCII value of %s is %d.\n", &letter, ascii);
}
Results in the output that you wanted:
Please input a charcter:
The ASCII value of a is 97.
This is bad code, and ive commented on some lines to help add some clarity.
#include <stdio.h>
//char* letter; //moving to stack from data segment
int ascii;
int main(){
char letter[10] = {0};//allocing 10 bytes here, all initialized null chars
printf("Please input a charcter:");
scanf("%s", &letter); //not checking return value here, bad form
ascii = (int) letter[0]; //casting only the first char
printf("\n The ASCII value of %s is %d.\n", &letter, ascii);
printf("\n The ASCII value of %s is %d.\n", &letter[1], (int)letter[1]);
}
With input ab, changing %c to %s gets both charaters (which seems like what you wanted in the first place?)
Please input a charcter:
The ASCII value of ab is 97.
The ASCII value of b is 98
.
Upvotes: 0