Question

I would like to compare argv[][] to a space character. However, it won't let me. When I tried with other characters such as 'a', 'b', 'c', etc. it works, but with a space, it's doesn't work.

if(argv[1][1] == ' ')
{
    printf("Space Detected\n");
}

I tried to use the space ASCII number, no chance. I tried to use other characters' ASCII number, it works.

I also tried '\ ', no chance.

Is there an alternative way to compare argv[][] to a space?

Restrictions: I'm not allow use use quotation marks, backslashes, on the command line.

This is code I'm working on:

int main(int argc, char *argv[]) 
{       
    printf("Program Running...\n\n");

    if((argc == 2) || ((argv[1][0] == '-') && (argv[1][1] == 'A')) )
    {
        printf("Option A.\n");
        printf("argv[1][2]: %c\n", argv[1][2]);

        printf("argv[1]: %s\n", argv[1]);

        if(argv[1][2] == ' ')
        {
            printf("Space Detected\n");
        }

    }

    return 0;
}
Was it helpful?

Solution

The parameters for the program are delimited by spaces. So if you want to actually enter one you will have to use "", ( at least in windows )

Console example;

This is only one parameter:

my.exe "a b c" 

Three parameters:

my.exe a b c
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top