zoukankan      html  css  js  c++  java
  • C语言 三目运算

    C语言 三目运算

    功能:为真后可根据条件选择成立两个不同的表达式

    • 如果表达式1值为真选择表达式2
    • 如果表达式1值为假选择表达式3

    案例

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main(void)
    {
        int a = 10;
        int b = 20;
        int c = 30;
        
        // 表达式1?表达式2:表达式3
        // 如果表达式1值为真选择表达式2
        // 如果表达式1值为假选择表达式3
        
        // 比较两个数输出最大值
        printf("最大值为:%d
    ", a > b ? a : b);
    
        // 比较三个数输出最大值
        printf("最大值为:%d
    ", a > b ? (a > c ? a : c) : (b > c ? b : c));
        
        return 0;
    }
    三目运算 使用案例
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    // 宏定义表达式:MAX传递值、通过三目运算做比较
    #define MAX(a,b) (a)>(b)?(a):(b)
    
    int main(void)
    {
        int a = 10;
        int b = 20;
        printf("%d
    ", MAX(a, b));
        return 0;
    }
    三目运算 使用案例:宏定义三目运算
  • 相关阅读:
    计算几何
    差三角
    约瑟夫
    字符编码
    河南省赛之Substring
    移动字母
    抽屉原理
    不要为了完成代码而写代码
    分布式文件系统优化
    降低代码的复杂度
  • 原文地址:https://www.cnblogs.com/xiangsikai/p/12373077.html
Copyright © 2011-2022 走看看