0

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?

oadams
  • 2,929
  • 5
  • 26
  • 48

1 Answers1

4

argv[1] is the first commandline arg and strtoul() converts to an unsigned integer

Also 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

Martin Beckett
  • 90,457
  • 25
  • 178
  • 252