I've an extremely n00bish doubt regarding 'return' statement. If I comment out second last code in attoi function; the output is only '6'; instead of 123456 (I figured out the formatting portion to print 123456). Why does 'return' chooses to print only last element from the character array? Why does'nt it is returning a value for every iteration?
I hope I sound coherent?
Code:
#include <stdio.h>
#include <stdlib.h>
int attoi(char inp[]);
int main(void) {
int l;
char p[5] = {49,50,51,52,53,54};
l = attoi (p);
printf ("%d",l);
}
/* attoi: convert s to integer */
int attoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = /*10 * n + */(s[i] - '0');
return n;
}