newbie question: how to compare char?

P

Paula

Hi,

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...

Thx
Paula
 
R

Ravi

Paula said:
...
char file_iso[30];
...

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

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

HTH,
Ravi.
 
R

Régis Troadec

Paula said:
...
char file_iso[30];
...

if (file_iso[11] == "a") alfafe=0.5; else alfafe = 0.0;
^^^ (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;

Regis
 
E

Eric Sosman

Paula said:
Hi,

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...

Thx
Paula

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

http://www.eskimo.com/~scs/C-faq/top.html
 
M

Martin Ambuhl

Paula said:
Hi,

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;
^^^ '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;
 
E

Eric Sosman

Martin said:
Hi,

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;
^^^ '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;

*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.)
 
K

Kenneth Brody

Eric said:
Martin Ambuhl wrote: [...]
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;

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

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

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. :)
 
E

Eric Sosman

Kenneth said:
Eric said:
Martin Ambuhl wrote: [...]
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;

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

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

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. :)

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.
 
A

August Derleth

Paula said:
...
char file_iso[30];
...

if (file_iso[11] == "a") alfafe=0.5; else alfafe = 0.0;
^^^ (it's a constant of type string,
not a constant of type character)

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
functions.
if (file_iso[11] == 'a') alfafe=0.5; else alfafe = 0.0;

Regis
but it doesn't work...

Thx
Paula
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top