How is it possible to convert a letter in char to int
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; }
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
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
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
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
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, 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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*