Walter does an if statement between () have to be an arithmetic
statement?
The part in the () in an 'if' statement must be an expression.
Every expression has a value (by definition.) The value that
results from the expression will be compared to 0, and the
first statement will be chosen if it is -not- 0 and the 'else'
statement (if any) will be chosen if the expression -is- 0.
If you code the expression with one of the logical operators,
such as if (x > 3) then it is fairly obvious what the
logical result of the expression is -- it will be either true or false.
The logical operators all return 1 to mean true and 0 to mean false.
If you code the expression with an arithmetic value but no logical
operator, then remember the implicit comparison to 0. For example
if you code if (x) then it is the same as if (x != 0).
This is the case whether x is some kind of integer or some kind
of floating point value -- it is either 0 or it isn't.
The above two paragraphs might sound like two different rules, but
they are really the same rule: does the result of the expression
equal 0 or does it not? The fact that a logical operator
happens to produce 1 for true does not matter to C: what is important
to C's 'if' statements is that the result is not 0.
What is tested in the 'if' statement can also be a pointer of
some kind. This involves a special case: when x is a pointer,
if (x) is considered true if the pointer is not NULL, and
considered false if the pointer -is- NULL. In many systems,
NULL -is- 0 internally, but that is not true in all implementations;
whatever internal value or values that the implementation uses
to represent NULL, the implementation is responsible for testing
against those internal values for the if (x) case where x is a pointer,
which is the same situation as if if (x != NULL) had been coded.
I am thinking I am trying to test if the value of the char type
pass is "ded" or not. A string. So I am thinking strcmp() but if a string is
"ded I want to branch to success. If the value of pass isn't "ded" I want to
branch to something else. A failure.
strcmp(pass, "ded") will return an arithmetic value that will be
non-zero if the strings are not the same, and will be 0 if the
strings -are- the same. Because 0 is returned if they -are- the
same, the code if (strcmp(pass,"ded") == 0) will end up
comparing 0 to 0 if the strings -are- the same, and since 0 == 0
the test would be true and the first statement after the 'if' would
be executed. Therefore your originally posted code
if (strcmp(pass,pass2) == 0)
was exactly the right kind of test to make to check to see if
the two strings were the same. The original code was marred by
the strcpy() that you had above the test, which had the effect
of forcing the two strings to be the same, so because of the
strcpy(), the two would always compare equal in strcmp(), so
0 would be returned by strcmp, that 0 would be tested with == 0
because you coded that, and that would be true, so only the first
statement after the 'if' could be reached. If you did not have
the strcpy() in there, that problem would not have been present --
but you would have still had the problem that you were fgets'ing
into your initialized variable rather than into your other variable.