How to create C program that can use parameters?

Asked

Viewed 95 times

4

I’m studying C, and now I’m trying to pass parameters. Well, although I think I did, it’s making a mistake when I try to use that variable...

I explain, the char "argument_value" that step on the command line, can be printed on the screen without the slightest problem, but if I use it inside an IF, it is as if it did not exist...

I need to compare the char "argument_value" to make decisions in the program!

What could I be doing wrong?

int main (int argument_count, char *argument_value[]) {

            printf("\n");
            printf("\nargument_value[0]  : %s", argument_value[0]);
            printf("\nargument_value[1]  :   %s", argument_value[1]);
            printf("\nargument_value[2]  :   %s", argument_value[2]);

            printf("\n");
            if      (argument_value[1] == "detailed"){printf("\n detalllll");}
            else if (argument_value[1] == "hexa")    {printf("\n hexaaaaaa");}
            else if (argument_value[1] == "bits")    {printf("\n bitsssss");}
            else                                     {printf("\n none...");}



}

This code always returns the parameters you pass on the command line, and "None..."

  • 2

    To compare strings in C, use the function strcmp. Example here

1 answer

4


Comfort indicated in this answer from Sopt:

To compare strings in C, use the function strcmp.

Example:

if (strcmp(argument_value[1], "detailed") == 0) {printf("\n detalllll");}
  • 3

    thanks for the objectivity! and examples! Agent you’re doing Knowing well how important it is to have it! Rigadão!

  • 2

    You’re welcome @Robervalsena 山 本! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.