Bird
0
0
DSA Cprogramming~10 mins

String to Integer atoi in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - String to Integer atoi
Step 1: Skip spaces
i=0,1 → ' ' skipped → i=2
Step 2: Read sign
s[2]='-' → sign=-1 → i=3
Step 3: Accumulate digits
s[3]='4' → result=4
Step 4: Apply sign + clamp
-1 × 42 = -42
C's atoi processes the string in four strict sequential steps: skip whitespace, detect optional sign, accumulate digit characters left to right, then apply sign and clamp to INT_MAX/INT_MIN. The critical C-specific concern is overflow detection before multiplying — signed integer overflow is undefined behavior in C.
Execution Sample
DSA C
#include <ctype.h>
#include <limits.h>

int myAtoi(char *s) {
    int i = 0;
    /* Step 1: skip whitespace */
    while (s[i] == ' ') i++;
    /* Step 2: read sign */
    int sign = 1;
    if (s[i] == '+' || s[i] == '-') {
        sign = (s[i] == '-') ? -1 : 1;
        i++;
    }
    /* Step 3: accumulate digits with overflow check */
    int result = 0;
    while (isdigit((unsigned char)s[i])) {
        int digit = s[i] - '0';
        if (result > (INT_MAX - digit) / 10) {
            return sign == 1 ? INT_MAX : INT_MIN;
        }
        result = result * 10 + digit;
        i++;
    }
    return sign * result;
}
Trace on ' -42abc': i advances past 2 spaces (i=2), reads '-' setting sign=-1 (i=3), reads '4' → result=4 (i=4), reads '2' → result=42 (i=5), hits 'a' which fails isdigit so loop exits. Returns -1 × 42 = -42.
Execution Table
is[i]actionsignresultoverflow?
0' 'skip space10no
1' 'skip space10no
2'-'sign=-1, i++-10no
3'4'result=0*10+4=4-14no
4'2'result=4*10+2=42-142no
5'a'!isdigit: STOP-142no
--return -1*42-1-42no
💡 Loop exits at i=5 because isdigit('a') returns 0. Final value: sign * result = -1 * 42 = -42.
Variable Tracker
stepisignresultdigitcheck: result>(INT_MAX-d)/10
init010--
skip spaces210--
read sign3-10--
read '4'4-1440>(2147483643)/10=214748364? NO
read '2'5-14224>(2147483645)/10=214748364? NO
stop 'a'5-142--
return--1-42--
Key Moments - 4 Insights
Why cast to (unsigned char) inside isdigit((unsigned char)s[i])?
isdigit takes an int but expects a value in the range [0, 255] or EOF. If char is signed and s[i] is a negative value (extended ASCII > 127), passing it directly is undefined behavior. Casting to unsigned char ensures a valid non-negative value.
Why check result > (INT_MAX - digit) / 10 instead of result * 10 + digit > INT_MAX?
result * 10 itself can overflow before adding digit — signed integer overflow is undefined behavior in C. The rearranged check avoids the multiplication entirely, testing only with safe operations.
What does C's standard atoi() return for overflow — and why should you not use it?
Standard atoi() has undefined behavior on overflow. It should never be used in production or competitive programming. strtol() is the correct alternative — it sets errno and clamps to LONG_MIN/LONG_MAX on overflow.
What does the algorithm return for input '+-12'?
Reads '+', sets sign=1, advances i. Next character '-' is not a digit so the digit loop never runs. Returns 1 * 0 = 0.
Visual Quiz - 4 Questions
Test your understanding
For input ' +0 123', what does myAtoi return?
A123
B0
C0123
D-1
💡 Hint
The digit loop stops at the first non-digit after accumulating '0' — the space after '0' terminates digit reading.
For input '2147483648' (INT_MAX + 1), what does myAtoi return?
A-2147483648
B2147483648
C2147483647
D0
💡 Hint
The overflow check fires before result would exceed INT_MAX and clamps to INT_MAX = 2147483647.
Why is isdigit((unsigned char)s[i]) safer than s[i] >= '0' && s[i] <= '9' in C?
Aisdigit is faster
Bisdigit handles multi-byte characters
Cisdigit with unsigned char cast avoids UB on signed char values > 127
DThey are exactly equivalent in all cases
💡 Hint
isdigit expects [0,255] or EOF — passing a signed negative char value directly is UB in C.
What does myAtoi return for '-2147483649' (below INT_MIN)?
A-2147483649
B2147483647
C0
D-2147483648
💡 Hint
Negative overflow clamps to INT_MIN = -2147483648 because sign == -1 in the overflow branch.
Concept Snapshot
C atoi = 4 steps: skip spaces → read sign → accumulate digits (stop on first non-digit) → clamp to [INT_MIN, INT_MAX]. Key C rules: (1) signed overflow is UB — check before multiplying, (2) cast to unsigned char before isdigit, (3) never use stdlib atoi() — it has UB on overflow.
Full Transcript
Let's trace String to Integer atoi in C on ' -42abc'. i starts at 0. s[0] and s[1] are spaces — the while loop advances i to 2. At i=2 we see '-'. We set sign to -1 and advance i to 3. Now the digit loop starts. At i=3, s[3]='4'. We cast to unsigned char before calling isdigit — this is essential in C to avoid undefined behavior. digit = '4' - '0' = 4. Overflow check: is 0 > (INT_MAX - 4) / 10? That's 0 > 214748364 — false. result = 0*10+4 = 4. Advance to i=4. At i=4, s[4]='2'. digit=2. Overflow check: is 4 > 214748364? No. result = 42. Advance to i=5. At i=5, s[5]='a'. isdigit returns 0. Loop exits. Return sign * result = -1 * 42 = -42. The two C-specific rules to remember: always cast to unsigned char before isdigit, and always check overflow before the multiplication to avoid undefined behavior.