tenkii
tenkii

Reputation: 449

C - Comparing two characters

I am having trouble comparing two characters. I've written a very basic C problem to try out command line arguments.

Here is my code so far:

#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's a girl");
    if (gender == 'b')
        puts("It's 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 type this into the terminal:

  $ ./pet_shop -t dog -g f cute small

I get this as output:

  You have chosen a dog:
  Traits: cute small

The output it missing information about the gender, it should be a girl since I entered f. But I tried checking by printf("%i", gender) which gave the value 0. Is g == 'f' the incorrect way of comparing two characters?

Upvotes: 8

Views: 91946

Answers (4)

abelenky
abelenky

Reputation: 64730

You first declared gender as a string:

char *gender = "";

Then you later treat is as a single character:

if(gender == 'f')
   [...]
if(gender == 'b')

You need to clarify in your own mind what gender is, before you try and code it.
Pick one definition, and stick with it.

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363817

gender is a char*, i.e. a pointer to a string's first charadcter. When you compare that to a single char, both the char and the pointer are converted to integers and an integer comparison is done.

To compare strings, use strcmp from <string.h>:

if (strcmp(gender, "f") == 0)
    // it's a girl

Note the double quote (") which signifies a string, rather than a single character.

Upvotes: 16

Shahbaz
Shahbaz

Reputation: 47593

You have:

char *gender = "";

So gender is a string, not a character. To compare strings, use strcmp.

Upvotes: 3

slugonamission
slugonamission

Reputation: 9642

The problem is that you're comparing a string (or rather, a char*) to a char. This comparison (i.e. if(gender == 'f')) will compare the raw pointer value to the character instead of comparing the contents of the string to the character. Instead, you need to dereference the pointer and then compare that value, or index into the string, i.e. if(gender[0] == 'f').

Of course, it would also be a good idea to check that the string actually contains something before attempting that in order to avoid a segfault.

Upvotes: 3

Related Questions