Sorry, I'm a rookie in C. What I am trying to do is just to print something if --help parameter is entered to the terminal like ./program --help
. So the code is this:
char *HELP = "--help";
char *argv1 = argv[1];
if (argv1 == HELP) {
printf("argv[1] result isaa %s\n", argv[1]);
}
So even if I use --help parameter it does not pass through the if condition. So what could be the reason behind that?
That's not how you compare strings in C. Use strcmp
or strncmp
:
if (strcmp(argv1, HELP) == 0)
Include string.h
to get access to those.
That is comparing the addresses, not the content. Use strcmp()
:
if (0 == strcmp(HELP, argv1))
{
printf("argv[1] result isaa %s\n", argv[1]);
}
Be sure and check that argc > 1
before accessing argv[1]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With