Challenge - 5 Problems
Parsing Numeric Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that strtol stops parsing when it encounters a non-digit character.
✗ Incorrect
The function strtol parses the initial part of the string as a number until it finds a character that is not valid for the number base. Here, it parses "123" and stops at "a", so val is 123 and endptr points to "abc".
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check what happens if the string does not start with digits.
✗ Incorrect
strtol returns 0 and sets endptr to the original string if no digits are found at the start. The program checks if endptr equals str to detect this case and prints "No digits were found".
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Base 0 lets strtol detect the base from the string prefix.
✗ Incorrect
When base is 0, strtol detects the base automatically. The prefix "0x" means hexadecimal, so it parses "1A3f" as hex which is 6719 decimal. The entire string is parsed, so endptr points to the empty string.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
strtol stops parsing at the first character that is not valid for an integer.
✗ Incorrect
strtol parses the initial integer part "3" and stops at the dot "." which is not valid for integers. So val is 3 and endptr points to ".14".
🧠 Conceptual
expert2:00remaining
Behavior of strtol on overflow
What happens when
strtol parses a number larger than LONG_MAX in C?Attempts:
2 left
💡 Hint
Check the C standard behavior for overflow in strtol.
✗ Incorrect
When the parsed number is out of range for long, strtol returns LONG_MAX or LONG_MIN depending on sign and sets errno to ERANGE to indicate overflow.