The logical value returned by the value assignment expression in C.

Source: Internet
Author: User

We know that in C, we often use judgment statements such as if (expression) {} And while (expression) {}. When the expression is true, we execute the function body in, but how can I determine whether the expression is true or false when the expression is a value expression? Or how can I use a comma expression?

Remember one principle: the value represented by the C language value assignment expression is the value that is finally assigned to the assigned variable.

The following test code proves the above points:

View code

 1 #include <stdio.h> 2  3 int main(int argc, const char *argv[]) 4 { 5     int i; 6     printf("the value is %d\n", i = -1); 7     printf("the value is %d\n", i =123); 8     printf("the value is %d\n", i = 0); 9     return 0;10 }

The result of the following code is easy to understand!

1 # include <stdio. h> 2 3 int main (INT argc, const char * argv []) 4 {5 Int I; 6 // when the value is 0, the value of the value expression indicates that the logical value is false, therefore, the following statement does not execute 7 if (I = 0) {8 printf ("I = 0 \ n "); 9} 10 // when the value is not 0, the value assignment expression indicates that the logical value is true, so the following statement will execute 11 if (I = 1) {12 printf ("I = 1 \ n"); 13} 14 // when the value is not 0, the value assignment expression indicates that the logical value is true, therefore, the following statement will execute 15 if (I =-1) {16 printf ("I =-1 \ n "); 17} 18 // The logical value represented by a comma expression is the logical value of the last expression, so the following statement does not execute 19 if (I = 1, I = 0) {20 printf ("I = 1, I = 0 \ n"); 21} 22 // The logical value represented by a comma expression is the logical value of the last expression, therefore, the following statement runs 23 if (I = 0, I = 1) {24 printf ("I = 0, I = 1 \ n "); 25} 26 27 28 29 return 0; 30}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.