oadams
oadams

Reputation: 3087

Receiving an Integer command line argument

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?

Upvotes: 0

Views: 938

Answers (1)

Martin Beckett
Martin Beckett

Reputation: 96119

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

Upvotes: 4

Related Questions