Page 87 - C-Language
P. 87

The = operator is used for assignment.


        The == operator is used for comparison.

        One should be careful not to mix the two. Sometimes one mistakenly writes


         /* assign y to x */
         if (x = y) {
              /* logic */
         }


        when what was really wanted is:


         /* compare if x is equal to y */
         if (x == y) {
             /* logic */
         }


        The former assigns value of y to x and checks if that value is non zero, instead of doing
        comparison, which is equivalent to:


         if ((x = y) != 0) {
             /* logic */
         }




        There are times when testing the result of an assignment is intended and is commonly used,
        because it avoids having to duplicate code and having to treat the first time specially. Compare


         while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
                 switch (c) {
                 ...
                 }
         }


        versus


         c = getopt_long(argc, argv, short_options, long_options, &option_index);
         while (c != -1) {
                 switch (c) {
                 ...
                 }
                 c = getopt_long(argc, argv, short_options, long_options, &option_index);
         }


        Modern compilers will recognise this pattern and do not warn when the assignment is inside
        parenthesis like above, but may warn for other usages. For example:


         if (x = y)         /* warning */

         if ((x = y))       /* no warning */
         if ((x = y) != 0)  /* no warning; explicit */




        https://riptutorial.com/                                                                               63
   82   83   84   85   86   87   88   89   90   91   92