![]() |
| | #1 |
| Guest
Posts: n/a
| Does atoi work like this: it scans an array element by element 0=49 1=50 2=51 converts these ascii values 49 50 and 51 into there digit equivalents and add each one to an identifier/varriable. Thanks learner(wanting to be a master). |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| No. It doesn't. x = atoi( "32" ); printf("%d", x ); /* this prints 32 */ atoi( ) takes a string of numbers and translatest it directly into the integer value of the string, as shown above. Quzah. |
| quzah is offline |
| | #3 |
| Registered User Join Date: Sep 2001
Posts: 752
| Okay, let's say this is the program... Code: #include <stdlib.h>
main ()
{
char a[4];
int i;
a = {49, 50, 51, 0};
// 49 = '1', 50 = '2', 51 = '3', 0 = '\0'
// So a is the string "123"
i = atoi (a)
// Now i is the integer value 123
return;
}
|
| QuestionC is offline |
| | #4 | |
| Guest
Posts: n/a
| Quote:
you try to compile this? probably not! any way this is not the meaning of atoi(ascii to integer), void main(void) { char *a; a = "49"; printf("%d",atoi(a)); } output: 49 press any key to continue... | |
| | #5 |
| Registered User Join Date: Sep 2001
Posts: 752
| Sorry, my post was intended to just get the spirit of atoi across as code, not as a program. Here's the code, fixed up to compile, and producting some output. Code: #include <stdlib.h>
main ()
{
char a[4] = {49, 50, 51, 0};
int i;
printf ("a represented as a string is... %s\n", a);
printf ("a represented as an int is... %d\n", a);
// 49 = '1', 50 = '2', 51 = '3', 0 = '\0'
// So a is the string "123"
i = atoi (a);
printf ("atoi(a) as an int is... %d\n", i);
printf ("atoi(a) as a string is... %s\n", i);
// Now i is the integer value 123
return;
}
Code: char *a = "49"; Code: a[3] = {52, 57, 0};
Code: char a[4] = {49, 50, 51, 0};
Code: char * a = "123" *Note to self... use atoi and pointer arithmatic to perform divisions in next ofuscated code contest* |
| QuestionC is offline |
| | #6 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| > return; Just to nit pick...you forgot to return a value! ![]() Quzah. |
| quzah is offline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Having trouble with atoi | NewbGuy | C Programming | 2 | 05-22-2009 11:55 PM |
| Redirect FAQ... | Queatrix | General Discussions | 21 | 05-21-2007 04:48 PM |
| If you are employed as a programmer, please look | Flood Fighter | A Brief History of Cprogramming.com | 10 | 09-28-2004 02:35 AM |
| FAQ Check/Lock | RoD | A Brief History of Cprogramming.com | 2 | 10-15-2002 11:21 AM |