Hi!
The following program gives the integer value of char string as output.Suppose if we give "1998",it will give integer 1998.In

i=i<<3+i<<1+(*string-'0')

I understood that it will multiply i by 10,but in *string-'0' I don't know what is going there..?.Here what refers string is it index in 1998 or whole 1998.
How this program works..?.What we are incrementing at string++ is it index.Please explain me I am a newbie...!
Any help would be highly appreciated.Thanxx

#include<stdio.h>

int myatoi(char *string);

int main(int argc, char* argv[])
{
printf("n%dn\n", myatoi("1998"));

return(0);
}

int myatoi(char *string)
{
int i;
i=0;
while(*string)
{
i=(i<<3) + (i<<1) + (*string-'0');
printf("%d\n",i);
string++;

// Dont increment i!

}
return(i);
}

Dani AI

Generated

@Ancallarao and covered the core idea: the routine walks the character string one byte at a time and builds an integer from the digit characters. The remaining useful details are about pointer semantics, safety, and robust alternatives.

The parameter char *string is a local pointer that initially points at the first character of the string passed in. Doing string++ advances that local pointer to the next character (it moves the pointer by sizeof(char)). That change does not alter the caller’s pointer — the function has its own copy of the pointer value. The loop typically stops when it reaches the NUL terminator ('\0'), so the code is processing one character per iteration.

For real code, prefer these practices:

  • Mark the input as read-only: const char * if the function does not modify the data.
  • Use strtol (or strtoll) for robust conversion with error and overflow reporting instead of rolling your own parser. See the strtol reference: strtol — cppreference.

When checking characters with ctype functions use a safe cast: isdigit((unsigned char)*p). Passing a plain char that can be negative is undefined behavior for those functions; cppreference documents this: isdigit — cppreference.

Finally, beware of overflow. Multiplying and adding can overflow a signed int (undefined behavior). Either check ranges before multiplying, use a wider type, or let strtol detect range errors; see the language overflow notes: .

Example of a short strtol idiom (no full copy shown here): set errno=0, call strtol, inspect endptr to detect no-conversion, and check errno==ERANGE for range errors.

Recommended Answers

All 5 Replies

I much perfer this instead of all those shifts. The code below could fail for integer overflow.

int myatoi(const char *string){
   int i = 0;
    // skip leading white space
   while( isspace(*string))
      ++string;
   while(*string && isdigit(*string))
   {
       i = (i * 10) + *string - '0';
       ++string;
   }
   return i;
}

I much perfer this instead of all those shifts. The code below could fail for integer overflow.

int myatoi(const char *string){
   int i = 0;
    // skip leading white space
   while( isspace(*string))
      ++string;
   while(*string && isdigit(*string))
   {
       i = (i * 10) + *string - '0';
       ++string;
   }
   return i;
}

In any way,what I haven't understood is

*string-'0'

.What string indicates whole string or index..
By the by what is "Post all questions in one of the boards." I didn't get you.I am from India now the time is 10:27 AM.

The asterisk before the variable means to reference only the first character in the string that the variable points to. So if string = "1234", then *string is looking at '1'. Another way to say the same thing is *string it the same as string[0].

Consequently, *string - '0' is the same as '1' - '0', which if you look at any ascii chart '1' = 49, and '0' = 48. So it boils down to 49 - 48 = 1.

The asterisk before the variable means to reference only the first character in the string that the variable points to. So if string = "1234", then *string is looking at '1'. Another way to say the same thing is *string it the same as string[0].

Consequently, *string - '0' is the same as '1' - '0', which if you look at any ascii chart '1' = 49, and '0' = 48. So it boils down to 49 - 48 = 1.

Very thanks...
I got this now...!
But some confusion in pointers .In many function we may not point and call them.What is the significance in this problem...
Thanks..

Sorry, I don't understand your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.