What is the meaning of '==' in C?

前端 未结 5 887
暗喜
暗喜 2021-01-18 11:30

What is the meaning of == and how does it differ from =?

How do I know which one to use?

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 11:37

    Now that you know the difference between '==' and '=", let me put you some words of caution. Although '==' is used as a standard test of equality between comparable variables and '=' used as an internally type-casted assignment, the following programming error is quiet common.

    In the below example and similar codes, '=' is know as "Always true" conditional operator.

    #include
    int main()
    {
        int i = 10, j = 20;
        if ( i = j )
            printf("Equal\n");
        else
            printf("NOT Equal\n");
        return 0;
    }
    

    So, the word of caution is "Never use '=' in if statements, unless you have something evil in your mind."

提交回复
热议问题