I’m writing a program that takes a single command line argument. This argument needs to be in the range [0, INT_MAX]. What is the best way to take argv[1] and convert it into an int, while ensuring that it will be a valid integer?
I’m writing a program that takes a single command line argument. This argument needs
Share
argv[1]is the first commandline arg and strtoul() converts to an unsigned integerAlso argc is the number of arguments, so check it is at least 2 (argc counts the program name) before calling
argv[1]Strictly strtoul() is c++ but most c compilers support it in their standard library, it takes a ‘c’ style char * string