Home>

About C language and programming.

Do not explain that the following sentence is grammatically correct. Also, explain how the program works, using the terms 0 and non-zero.

#include<stdio.h>
int main (void) {
int a, b;
scanf ("% d",&a);
if (b = a)
 printf ("A non-zero value has been assigned to b. \ N");
else else
 printf ("0 is assigned to b. \ N");
}


I thought it was a common truth-related problem, such as executing if (a = b) holds, and not executing if it does not hold ...
However, even if 0 is assigned in (a = b), (a = b) holds, so I started to wonder if it could be explained by the story that holds (true) and does not hold (false).

I haven't used anything other than if (conditional expression), so I can't explain it.
Could you tell me?

c if
  • Answer # 1

    In the if statement, the value of the expression described in the conditional expression is0Fake in the case of0It is judged as true in other cases.
    b = aThe formula isbToaAs the value of the expressionbReturns.

    // ・ ・ ・ Abbreviation
    if (b = a) printf ("a non-zero value has been assigned to b. \ N");
    else printf ("0 is assigned to b. \ N");


    Is the same as:

    // ・ ・ ・ Abbreviation
    b = a;
    if (b) printf ("A non-zero value has been assigned to b. \ N");
    else printf ("0 is assigned to b. \ N");

    b! = 0Is also an expression, and as a result of comparison, if it is false0, If true1Is the value of the expression.

    // ・ ・ ・ Abbreviation
    b = a;
    if (b! = 0) printf ("A non-zero value has been assigned to b. \ N");
    else printf ("0 is assigned to b. \ N");


    Can also be written as:

    int conditions = 0;
    // ・ ・ ・ Abbreviation
    b = a;
    conditions = (b! = 0);
    if (conditions) printf ("a non-zero value has been assigned to b. \ N");
    else printf ("0 is assigned to b. \ N");

  • Answer # 2

    Since the result of the substitution is returned,bToaAfter substitutingif (a)It means that.
    In other words0When substitutingif (0),Non-zero(Non-zero) Is substitutedif (non-zero),is.
    int c = (b = a);AsaWhen changing the value ofcPlease check what the value of is.

  • Answer # 3

    C language= =Does not mean "equal". The operator that compares whether the left and right values ​​are equal==is.

    b = aThe expression has only the meaning of "assigning the value of variable a to variable b and using the value of b as the value of the expression". What to do because the value of variable a and the value of variable b are equal is completely irrelevant.

    In the conditional expression of the if statement, you can write other than the expression using the comparison operator, and the operation is "If the value of the expression is other than 0, the condition is satisfied, and if it is 0, it is not satisfied".

    In other wordsb = aIf the value of the variable b after substitution is other than 0, the condition is satisfied, and if it is 0, the condition is not satisfied.

  • Answer # 4

    I'm guessing, but the problem given is

    Do you understand the difference between "=" (assignment operator) and "==" (comparison operator) in C language?

    Do you have a grammatical strict understanding of conditional branching in if statements?

    I think it is a question that asks.

    The following is an example.The evaluation value is "an evaluation of that value"is.

    [1] For assignment operators
    a = 5;// (1) Let's assume that a = 5.
    b = a // (2) Assignment operator
    if (b = a) // (3) The evaluation target of the if statement is b = a


    You can see (1). The evaluation value of "a" is "5".
    (2) means "substitute the value of a for b". The "evaluation value" of "b = a" is "5".
    In (3), the evaluation target of the if statement is "b = a". In the case of this example

    if (b = a)
    if (5)


    Will be the same.

    Next, the branch condition of the if statementaccuratelyDescribe.
    The if statement has the value in () that follows

    True if non-zero

    False if 0
    Judge.

    In the above example, the value in the if statement is "5", so it becomes "true".
    It means that "a non-zero value has been assigned to b".

    a = 0
    if (b = a)


    In the case of, the evaluation target of the if statement is "0", so it becomes "false".
    It means that "0 has been assigned to b".

    [2] For comparison operators

    Here is an example:

    a = 5;//
    b = 5;//
    a == b // (1)
    if (a == b) // (2)


    In (1), "==", that is, the comparison operator is used.
    In comparison operators

    If a and b are compared and they match, the evaluation value will be "1".

    Compare a and b, and if they do not match, the evaluation value will be "0".
    In the above example, the evaluation value of "a == b" is "1".
    In (2), the evaluation target of the if statement is "a == b". In the above example

    if (a == b)
    if (1)


    Is the same thing, so the if statement is "true" and the statement immediately following the if statement is executed.

    If a = 5, b = 3,

    if (a == b)
    if (0)


    Is the same, and the if statement is "false", so the statement immediately following the if statement is not executed.

    That's a long time, but I explained about the if statement.
    Summary,
    The if statement is true if the value to be evaluated is non-zero, and false if it is 0, so the program in question is correct.
    It means that.

    I would appreciate it if you could refer to it.