Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use logical operators in C language

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today Xiaobian to share with you how to use the C language logic operators related knowledge points, detailed content, logic clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you read this article after some gains, let's learn about it together.

I,&&& II Analysis

What are the values of i, j and k at the end of the following program?

#include int main(){ int i = 0; int j = 0; int k = 0; ++i || ++j && ++k; printf("i = %d\n", i); printf("j = %d\n", j); printf("k = %d\n", k); return 0;}

The output is as follows:

Why i = 1, j = 0, k = 0? See the analysis below.

Short circuit in program

|| Calculate from left to right:

Stop evaluating when a condition is true, the entire expression is true

An expression is false only if all conditions are false.

&& Calculate from left to right:

Stop evaluating when a condition is false, the entire expression is false

An expression is true only if all conditions are true

In logical expressions,&& is more than|| has a higher priority

In &&& and|| When mixed, the entire expression is treated as|| Expressions, the compiler starts evaluating && expressions from left to right, stops evaluating when one && expression is true, and the entire expression is true.

The above code can be explained. Since i = 0;++i is 1, true && ++i is 1, according to the short-circuit rule,|| It starts from left to right and stops when it meets a condition that is true, and the entire expression is true. So ++j and ++k are not running, so they are 0.

Here's another short-circuit rule in a program:

#include int g = 0; int f(){ printf("In f()...\ n"); return g++;} int main(){ if( g || f() && f() ) { printf("In if statement: %d\n", g); } printf("In main(): %d\n", g); return 0;}

Understand the short-circuit law and|| Mixing the algorithm with &&, the following output is easy to understand:

Two! analysis

Logical NOT in C! "I only know 0. I only know that when I see 0, I return to 1. So when it encounters a value other than 0, the result is 0. (Note that not only 1 means true, other non-zero values mean true)

Here's a snippet of code used by the logical NOT operator:

#include int main(){ printf("%d\n", ! 0); printf("%d\n", ! 1); printf("%d\n", ! 100); printf("%d\n", !- 1000); return 0;}

The output is as follows:

The above is "C language logic operators how to use" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report