I'm having trouble comparing two characters. I wrote a very basic C problem to try command line arguments.
Here is my code:
#include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { char ch; char *type = ""; char *gender = ""; int baby = 0; int count = 0; /* Options: * -t = type of pet * -g = gender * -b = baby or adult */ while ((ch = getopt(argc, argv, "t:g:b")) != EOF) switch (ch) { case 't': type = optarg; break; case 'g': gender = optarg; break; case 'b': baby = 1; break; default: fprintf(stderr, "Invalid option.\n"); return 1; } argc -= optind; argv += optind; printf("You have chosen a %s.\n", type); if (gender == 'f') puts("It a girl"); if (gender == 'b') puts("It a boy."); // The main command line arguments should be about the traits of the pet printf("%s", "Traits: "); for (count = 0; count < argc; count++) printf("%s ", argv[count]); return 0; }
So, if I enter it into the terminal:
$ ./pet_shop -t dog -gf cute small
I get this as output:
You have chosen a dog: Traits: cute small
At the exit, he does not have information about sex, it must be a girl since I entered f. But I tried to check printf ("% i", gender), which gave the value 0. Is g == 'f' used to compare two characters?
tenkii
source share