newbie question: how to compare char?

Discussion in 'C Programming' started by Paula, Apr 8, 2004.

  1. Paula

    Paula Guest


    i guess this is a stupid question, but i'm used to pascal and i'm
    having problems in understanding char and strings in c...

    How can I compare the char value at a given position in a string? I
    mean, I'm trying:

    char file_iso[30];

    if (file_iso[11] == "a") alfafe=0.5; else alfafe = 0.0;

    but it doesn't work...

    Paula, Apr 8, 2004
    1. Advertisements

  2. Paula

    Ravi Guest

    if (file_iso[11] == 'a') alfafe=0.5; else alfafe = 0.0;

    Ravi, Apr 8, 2004
    1. Advertisements

  3. ^^^ (it's a constant of type string,
    not a constant of type character)

    if (file_iso[11] == 'a') alfafe=0.5; else alfafe = 0.0;

    Régis Troadec, Apr 8, 2004
  4. Paula

    Eric Sosman Guest

    if (file_iso[11] == 'a') ...

    Single and double quotes have entirely different
    meanings in C source. You would do well to review
    Question 8.1 -- in fact, all of Section 8 -- in the
    comp.lang.c Frequently Asked Questions (FAQ) list
    Eric Sosman, Apr 8, 2004
  5. ^^^ 'a'
    'a' is a character, "a" is a string.

    BTW, I like, although there are people posting to the newsgroup who
    think it should be banned,
    alfafe = (file_iso[11] == 'a') ? 0.5 : 0.0;
    Martin Ambuhl, Apr 8, 2004
  6. Paula

    Eric Sosman Guest

    *That's* not ugly; *this* is ugly:

    alfafe = (file_iso[11] == 'a') * 0.5;

    (With apologies to C. Dundee, a name perhaps more
    apropos than what we done wot of.)
    Eric Sosman, Apr 8, 2004
  7. [...]

    Does the standard guarantee that the value of a boolean expression
    be 1 for true? I ask because many years ago, one of the systems I
    used had a compiler that used -1 for true, so I only go on the "non-
    zero" assumption. Not that I would ever want to do anything as ugly
    as Eric's code, anyway. :)
    Kenneth Brody, Apr 9, 2004
  8. Paula

    Eric Sosman Guest

    The comparison operators `==' and `!=', the relational
    operators `<', `<=', `>=', and `>', the logical operators `&&'
    and `||', and the negation operator `!' all produce zero for
    false and unity for true.

    The "testing constructs" (the `?:' operator, the `&&' and
    `||' operators, the `if' statement, the `for' statement, the
    `while' statement, and the `while' clause of the `do' statement)
    treat zero as false and any non-zero value as true.

    Note, though, that library functions -- notably the isxxx()
    family from <ctype.h> -- are not always so rigid. isspace(),
    for example, returns zero for '9' but any arbitrary non-zero
    value, not necessarily unity, for '\t'.

    There's a familiar trick for "normalizing" a truth value
    from zero/nonzero to zero/unity: `!!value'. In practice, though,
    I almost never find this necessary.
    Eric Sosman, Apr 9, 2004
  9. It's not, either. It's a constant of type array of char, which is
    guaranteed to be null-terminated.

    (In memory, "a" looks like {'a', '\0'}. The compiler is guaranteed to do
    the termination for you in this case. However, arrays of this type can
    never be modified, which is what `constant' means.)

    C lacks a string type unless you (or the one writing libraries you use)
    create one. C hobbles along by defining the characteristics an array of
    char must adhere to if it is to be used as a string by the standard C
    August Derleth, Apr 11, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.