0
strings variable conditions in c
I am a beginner... can someone help me in writting a code with an if statement having strings as the variable example scanf("%s%s%d", & marital_status, & gender, & age) ; if((gender==male) && marital_status == married & && age>37))...this is not working. help me with the Syntax of solving such a problem
3 Réponses
+ 2
scanf("%d", &var), but scanf("%s", var) (without &). if(marital_status == "married") (in quotes), same for gender. && instead of &&&
+ 2
In c you can't compare string content with ==
You must use strcmp(s1, s2);
It returns 0 if both strings are equal.
Ej.
scanf("%s %s", marital_status, gender);
if(
(strcmp(marital_status, "married") == 0) &&
(strcmp(gender, "male")==0)
) {
.....
.....
}
#include <string.h> to use string finctions such as strcmp()
0
Thanks guys