Go Down

Topic: basic c question (Read 3112 times) previous topic - next topic

AWOL

Quote
it is not showing any error message.

Just because something compile, it doesn't mean that it works properly.
Check what the comma operator does.

BijendraSingh

Quote
if (strcmp(name, "bruce"), age != 1 && age != 40)

here strcmp(name, "bruce ") is compared and output could be 0 or 1,
so if() is like 
Quote
if(0, age != 1 && age != 40)

comma operator execute left to right so rightmost value is assigned to if statement , so instruction is actually converted into
Quote
if(age != 1 && age != 40)

so that it is not showing any error.
Bijendra

jonwhite

thank you for helping me ive read up on the commer

     if (strcmp(name, "bruce") && age >= 13 && age <= 19)
        printf("your name is bruce, you are the chosen one\n")

im still pulling my hair out trying to get this to work im thinking its something to do with the first %% the way i understand this works is if both strings compair result is 1 (boloon expression) and age is betweem 13 and 19, ive taken out the ! expression to make it a bit easer to understand

AWOL

You might want to double check what "strcmp" returns - it may be counter-intuitive.

mistergreen

yup,
you have to use strcmp correctly

http://www.cplusplus.com/reference/cstring/strcmp/

this site is the place to be where you have to code C.

Go Up