Bird
0
0
DSA Cprogramming~20 mins

String to Integer atoi in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Atoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of atoi with leading spaces and signs
What is the output of the following C code that uses atoi to convert a string with spaces and signs?
DSA C
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "   -1234abc";
    int val = atoi(str);
    printf("%d\n", val);
    return 0;
}
A0
B-1234
C1234
DSyntax error
Attempts:
2 left
💡 Hint
atoi ignores leading spaces and stops at first non-digit after optional sign.
Predict Output
intermediate
2:00remaining
Result of atoi on string with no digits
What will be printed by this C program using atoi on a string with no digits?
DSA C
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "abc";
    int val = atoi(str);
    printf("%d\n", val);
    return 0;
}
A0
BGarbage value
Cabc
DRuntime error
Attempts:
2 left
💡 Hint
atoi returns 0 if no valid conversion is possible.
Predict Output
advanced
2:00remaining
Output of atoi with multiple signs
What is the output of this C code using atoi on a string with multiple signs?
DSA C
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "--123";
    int val = atoi(str);
    printf("%d\n", val);
    return 0;
}
AUndefined behavior
B-123
C123
D0
Attempts:
2 left
💡 Hint
atoi stops conversion if invalid sign sequence is found.
Predict Output
advanced
2:00remaining
Value returned by atoi on integer overflow
What is the output of this C program when atoi converts a string representing a number larger than INT_MAX?
DSA C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
    char str[] = "999999999999999999999";
    int val = atoi(str);
    printf("%d\n", val);
    return 0;
}
AImplementation-defined value (overflow)
B2147483647
C0
D-1
Attempts:
2 left
💡 Hint
atoi does not handle overflow safely; result is undefined.
🧠 Conceptual
expert
2:00remaining
Behavior of atoi with embedded null character
Consider the string literal "123\0abc" passed to atoi. What integer value does atoi return?
A1230
B0
C123
DRuntime error
Attempts:
2 left
💡 Hint
Strings in C end at the first null character.