0
0
Cprogramming~20 mins

Parsing numeric arguments - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parsing Numeric Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of parsing integer with strtol
What is the output of this C program when parsing the string "123abc" using strtol?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "123abc";
    char *endptr;
    long val = strtol(str, &endptr, 10);
    printf("Value: %ld\n", val);
    printf("Remaining string: %s\n", endptr);
    return 0;
}
A
Value: 123
Remaining string: abc
B
Value: 0
Remaining string: 123abc
C
Value: 0
Remaining string: 
D
Value: 123
Remaining string: 
Attempts:
2 left
💡 Hint
Remember that strtol stops parsing when it encounters a non-digit character.
Predict Output
intermediate
2:00remaining
Output when parsing invalid number with strtol
What is the output of this C program when parsing the string "abc123" using strtol?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "abc123";
    char *endptr;
    long val = strtol(str, &endptr, 10);
    if (endptr == str) {
        printf("No digits were found\n");
    } else {
        printf("Value: %ld\n", val);
    }
    return 0;
}
A
No digits were found
B
Value: 0
C
Value: -1
D
Value: 123
Attempts:
2 left
💡 Hint
Check what happens if the string does not start with digits.
Predict Output
advanced
2:00remaining
Output of parsing hexadecimal number with strtol
What is the output of this C program when parsing the string "0x1A3f" using strtol with base 0?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "0x1A3f";
    char *endptr;
    long val = strtol(str, &endptr, 0);
    printf("Value: %ld\n", val);
    printf("Remaining string: %s\n", endptr);
    return 0;
}
A
Value: 0
Remaining string: 0x1A3f
B
Value: 6719
Remaining string: 
C
Value: 1
Remaining string: A3f
D
Value: 6719
Remaining string: 0x1A3f
Attempts:
2 left
💡 Hint
Base 0 lets strtol detect the base from the string prefix.
Predict Output
advanced
2:00remaining
Output when parsing float with strtol
What is the output of this C program when parsing the string "3.14" using strtol?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *str = "3.14";
    char *endptr;
    long val = strtol(str, &endptr, 10);
    printf("Value: %ld\n", val);
    printf("Remaining string: %s\n", endptr);
    return 0;
}
A
Value: 3
Remaining string: 14
B
Value: 0
Remaining string: 3.14
C
Value: 3
Remaining string: .14
D
Value: 314
Remaining string: 
Attempts:
2 left
💡 Hint
strtol stops parsing at the first character that is not valid for an integer.
🧠 Conceptual
expert
2:00remaining
Behavior of strtol on overflow
What happens when strtol parses a number larger than LONG_MAX in C?
AIt causes a runtime crash due to overflow.
BIt returns 0 and sets errno to EINVAL.
CIt returns the number modulo LONG_MAX without error.
DIt returns LONG_MAX and sets errno to ERANGE.
Attempts:
2 left
💡 Hint
Check the C standard behavior for overflow in strtol.