about atoi

S

sam

Hi, whats the meaning of atoi function here.


int atoi(char __ch)
{
switch(__ch)
{
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
};
return -1;
}
 
D

Daniel Albuschat

sam said:
Hi, whats the meaning of atoi function here.

The meaning is the following:
int atoi(char __ch)
{
switch(__ch)
{
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
};
return -1;
}

(Can't you figure it out yourself?)

Regards,
Daniel
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi, whats the meaning of atoi function here.

atoi is short for ascii to integer, it converts a char-string like "345"
to the integer 345. This however is someones attempt to implement that
function on their own and this version can only handle one-digit number.
There are better ways to do that, something like this (haven't tried):

int ator(char ch)
{
if (ch < '0' || ch > '9')
return -1;
else
return ch - '0';
}
 
D

Default User

sam said:
Hi, whats the meaning of atoi function here.


int atoi(char __ch)

atoi() is a standard library function. It does not have the signature
given above. It should be:

int atoi(const char *nptr);

We'll assume it's some other function name and go from there.
{
switch(__ch)

Identifiers starting with __ are reserved for the implemenation.
{
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
};

This switch is rather pointless. You can do the same thing more simply
with:

if (ch < '0' || ch > '9')
return -1;
else
return ch - '0';



Brian
 
R

raxitsheth2000

Hi, whats the meaning of atoi function here.

int atoi(char __ch)
{
switch(__ch)
{
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
};
return -1;
YOUR /Above written atoi takes one input character and return
appropriate integer or -1 as error.

1. Give it Good Name so it will not conflict.
2. Dont Provide input string, it is asking for Input char.
3. I think __varname is platform reserved variable, but i am not sure.
4. Is there need of this function cant you use atoi (widely used) ?
5. Why I am answering to this question ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top