【秋暝的C语言练习代码】11~20

 a11 if省略{}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a = 0;
    printf("%d",a);
    if (a == 0)a=1;
    printf("%d",a);
    system("pause");
    return 0;
}

 a12 if(判断){...}else if(判断){...}else{...}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a = 0;
    printf("%d\n", a);
    if (a == 0){
        a = 1;
    }
    else if (a == 1){
        a = 2;
    }
    else{
        a = 3;
    }
    printf("%d\n", a);
    system("pause");
    return 0;
}

 a13 三目运算 (赋值)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //三目运算
    int a = 0;
    a=a==1?1:2;
    printf("%d\n", a);
    system("pause");
    return 0;
}

a14 三目运算2 (赋值)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //三目运算2
    int a = 0;
    printf("%d\n", a==1?1:2);
    system("pause");
    return 0;
}

a15 三目运算3 (复合)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //三目运算3
    int a = 0;
    printf("%d\n", a);
    a=a==0?1:a==1?2:3;
    // if (a == 0) a = 1;
    // else if (a == 1)a = 2;
    // else a = 3;

    printf("%d\n", a);
    system("pause");
    return 0;
}

a16 三目运算4 (输出)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //三目运算4
    int a = 0;
    printf(a==0?"A\n":"B\n");
    system("pause");
    return 0;
}

a17 三目运算5 (语句)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    //三目运算5
    int a = 0;
    a==0?printf("A\n"):printf("B\n");
    system("pause");
    return 0;
}

a18 switch关键字

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a = 0;
    switch (a)
    {
    case 0:
        printf("A\n");
        break;
    case 1:
        printf("B\n");
        break;
    
    default:
        printf("C\n");
        break;
    }
    
    system("pause");
    return 0;
}

a19 常量

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10 //常量
int main()
{
    int a = 0;//变量
    const int b = 1;//常量,本质常量
    system("pause");
    return 0;
}

a20 枚举

#include <stdio.h>
#include <stdlib.h>
int main()
{
    enum Sex//枚举常量
	{
	MALE, 
	FEMALE,
	SECRET
	};
    
	enum Sex s = FEMALE;  //利用枚举常量给变量赋值
	printf("%d\n", MALE);//0
	printf("%d\n", FEMALE);//1
	printf("%d\n",SECRET);//2
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只秋暝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值