C command line parameters

Status
Not open for further replies.

H-Hour

Ars Scholae Palatinae
606
im new to passing parameters through the command line
im trying do do this in C

Code:
int main(int argc, char *argv[])
{
	
	blockSizeInWords = (int)argv[2];
	numberOfSets = (int)argv[3];
	blocksPerSet = (int)argv[4];

passing ./a.out test.mc 4 2 1

i want
blockSizeInWords == 4
numberOfSets == 2
blocksPerSet == 1

and argv[0] == "test.mc"

but i have now clue what im going wrong
 

Tychom

Ars Tribunus Militum
2,391
Subscriptor
Firstly, you will find the file named test.mc at argv[1], argv[0] is the name of the program.

Secondly everything passed into your program is passed as a string, you cannot simply cast a string to an integer as you're trying to do. You will need to use one of the conversion functions such as atoi. also check that the correct number of arguments have been passed in as its good practise (even if in this case you don't particularly need to)
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
With all respect to Zeus60, I'd suggest using strtol()/strtod() to do the conversions. The problem with using the sscanf() method is that it won't reject bad inputs like 12qw3; it will successfully convert and assign 12, so your code thinks it got a valid input when it really didn't.

Here's an example of what I mean:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, char **argv)
{
    int val;
    char *chk;

    if (sscanf (argv[1], "%d", &val) == 1)
    {
        printf ("sscanf() successfully read %d from %s\n",
           val, argv[1]);
    }
    else
    {
        printf ("sscanf() was not able to convert %s\n", argv[1]);
    }
    
    val = (int) strtol (argv[1], &chk, 10);
    if (isspace (*chk) || *chk == 0)
    {
        printf ("strtol() successfully read %d from %s\n",
            val, argv[1]);
    }
    else
    {
        printf ("strtol() encountered non-numeric character"
                " %c in %s\n", *chk, argv[1]);
    }

    return 0;
}

And here are some sample runs:

Code:
./args 123
sscanf() successfully read 123 from 123
strtol() successfully read 123 from 123

./args 12f
sscanf() successfully read 12 from 12f
strtol() encountered non-numeric character f in 12f

./args f23
sscanf() was not able to convert f23
strtol() encountered non-numeric character f in f23
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
You're right, the version posted will erroneously validate inputs with embedded whitespace (like "12 3" or "123 look I'm a number", but note that unquoted arguments with embedded whitespace are treated as separate arguments anyway; "12 3" is one argument, 12 3 are separate arguments). However, arguments like " 123" and "123 " should be considered valid. strtol() and strtod() ignore leading whitespace anyway, and the isspace() check is necessary to handle trailing whitespace. An additional check is necessary to handle embedded whitespace.

The moral of the story is that input validation in C is rarely as straightforward as we'd like it to be.
 
Status
Not open for further replies.