I know this works as expected if your command line arguments are strings.

#include <stdio.h>

int main( int argc, char *argv[] )  
{
   printf("Program name %sn", argv[0]);

if( argc == 2 )
{
   printf("The argument supplied is %sn", argv[1]);
}
   else if( argc > 2 )
{
   printf("Too many arguments supplied.n");
}
else
{
   printf("One argument expected.n");
}
}

Would you be able to do something similar to this if you have integers or would you need to use the first method then convert it with atoi?

#include <stdio.h>

int main( int argc, int *argv[] )  
{
   printf("Program name %sn", argv[0]);

if( argc == 2 )
{
   printf("The argument supplied is %sn", argv[1]);
}
   else if( argc > 2 )
{
   printf("Too many arguments supplied.n");
}
else
{
   printf("One argument expected.n");
}
}

Recommended Answers

All 5 Replies

Command line arguments are always strings. What those strings represent depends on your code and how you use them.

But yes, if an argument represents an integer and you want to store it in an integer type, you need to convert the string with something like strtol (atoi is not recommended...ever).

Whats wrong with atoi? I have used it in the past with no issues. Why is strtol better?

atoi invokes undefined behavior if the converted integer cannot be represented by the int type. There's no workaround other than explicitly validating the string before calling atoi, and since strtol does this already (as well as not invoking undefined behavior in the same case atoi does), there's no good reason to use atoi.

What about this part? How do you handle this?

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

http://www.cplusplus.com/reference/cstdlib/strtol/

What about this part? How do you handle this?

All of them are under the programmer's complete control. I handle it by not writing stupid code. ;)

No-throw guarantee: this function never throws exceptions.

All functions in C have a no-throw guarantee. Why? Because C doesn't support exceptions in the first place. ;)

If str does not point to a valid C-string

Yup. The same goes for strlen, or strcpy, or anything that expects a valid C-string by definition. The strings contained by argv are guaranteed to be either a valid C-string or NULL. For other strings, a C-string is defined as zero or more characters followed by '\0'. In this case it's a matter of defensive coding to ensure that you don't produce an invalid string.

And of course, NULL is a simple check if there's any possibility of receiving it.

or if endptr does not point to a valid pointer object, it causes undefined behavior.

Noting that NULL is a valid pointer object, this too is a matter of defensive coding. Make sure your pointers point to an actual object or set them to NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.