How is it possible to convert a letter in char to int
Printable View
How is it possible to convert a letter in char to int
cast it - and you get the ascii decimal value for the character:
Code:#include <stdio.h>
int main ()
{
int letter = 'a';
printf ("The ascii value of %c is %d\n", letter, (int)letter);
getchar ();
return 0;
}
thanks but the thing i want to do is to take a charactor out of a char and convert it to int so i can multply it eg.
char test[20] = "12345678";
now i want to multiply 8 with 5 and place it into a variable called
int lastnumber
ah, different story, you were on about arrays... there are ways to
convert between ascii numeral values and actual ints. your compiler may have a function called atoi - i cant honestly
remember if it is standard code or not, but here's a link about it:
http://cboard.cprogramming.com/showthread.php?t=3506
The header <stdlib.h> declares functions for number conversion, storage allocation, and similar tasks.
double atof(const char *s)
atof converts s to double; it is equivalent to strtod(s, (char**)NULL).
int atoi(const char *s)
converts s to int; it is equivalent to (int)strtol(s, (char**)NULL, 10).
long atol(const char *s)
converts s to long; it is equivalent to strtol(s, (char**)NULL, 10).
niceee.. it worked very well.. thank u very much.. u guys are the best
If your goal is to just convert a single digit, then an easier more portable method would be to subtract '0' from it, for example:
Code:char test[20] = "12345678"
int lastdigit = test[7]-'0';
int firstdigit = test[0]-'0';
..etc
in my case i need to convert an input string to a #..
for example input a month..
ive try dis 1 but its an error..Code:printf("input month");
scanf("%s",month");
if (month==january)
month=1;
hope you can help me..
thanks
Use strcmp to compare strings. Visit the FAQ. Reread your reference on basic syntax.