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

Example Analysis of conditional operators and conditional expressions in C language

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of conditional operators and conditional expressions in C language, which is very detailed and has certain reference value. Friends who are interested must read it!

Conditional operators and conditional expressions

The conditional operator is the only ternary operator in the C language. The expression composed of conditional operators is called conditional expression, and its general form is as follows:

Expression 1? Expression 2: expression 3

The process of evaluating a conditional expression is as follows: first, the value of expression 1 is evaluated; if the value is true (not 0), the value of expression 2 is evaluated as the value of the conditional expression; otherwise, the value of expression 3 is evaluated as the value of the conditional expression.

Give an example

Topic description: use conditional operation to input two integers from the keyboard and output their larger values.

Examples of answers:

1#include

2int main ()

3 {

4 int max, a, b

5 printf ("Input two integers:")

6 scanf ("% d% d", & a

7 max = a > b? A: b

8 printf ("max=%d\ n", max)

9 return 0

10}

Results:

Matters needing attention

(1) conditional operators take precedence over assignment operators and are lower than all other operators.

For example: min=a > b?b*b:a*a

(2) the conditional operator is left associative.

For example: flag=a > 0?1:a==0?0:1

(3) the types of three expressions in conditional expressions can be different, and the higher type of expression 2 and expression 3 determines the type of conditional expression.

For example: max=a > baked 3.14purl 100

The above is all the content of the article "sample Analysis of conditional operators and conditional expressions in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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

Internet Technology

Wechat

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

12
Report