Course thumb

The C programming language made simple

How to parse and validate a number in C

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { char str[100] = "13333333333333"; char* endPtr; long long x = strtoll(str, &endPtr, 10); if (str == endPtr) { printf("Number could not be parsed!\n"); return 1; } if (errno == ERANGE) { printf("Number is too big to store in the variable!\n"); return 1; } printf("%lld\n", x); return 0; }