Home > Database > Mysql Tutorial > body text

C数据类型(枚举enum)和switch语句

WBOY
Release: 2016-06-07 16:01:20
Original
1370 people have browsed it

// // main.m // LessonCondition // // Created by lanouhn on 14-7-16. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import Foundation/Foundation.h //枚举类型是一个构造类型,它使一组整型常量罗列出了有的可能性 //定义

//

// main.m

// LessonCondition

//

// Created by lanouhn on 14-7-16.

// Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.

//

#import

//枚举类型是一个构造类型,它使一组整型常量罗列出了有的可能性

//定义枚举,使用关键字 enum +枚举类型的名字{枚举值,多个枚举值之间用逗号隔开};最后的分号不能少

//枚举类型将人能够识别的识别符和计算机能够识别的数字对应起来

enum season {

spring, //春天

summer = 100, //夏天

autumn, //秋天

winter //冬天

};

enum company {

teaching = 801,//教学部分机号801

market = 802,//市场部分机号802

person = 803,//人事部分机号803

consult = 804 //咨询部分机号804

};

int main(int argc, const char * argv[])

{

@autoreleasepool {

int a = 20, b =5;

BOOL isTrue = NO;

printf("%d\n", isTrue);

//关系运算符

isTrue = a > b;

printf("%d\n", isTrue);

//逻辑运算符 逻辑与(&&) 逻辑或(||) 逻辑非(!)

// isTrue = a || b;

isTrue = !a;

printf("%d\n", isTrue);

//if语句

// use of undeclared identifier 'a'变量 a 没有定义, 解决方案,定义变量a

// expression result unused 表达式的结果没有使用, 解决方案, 定义变量, 存储表达式的结果

// unused variable 'b' 没有使用变量b

// if 语句的第一种结构 if(条件表达式) {语句}当条件表达式的条件成立时(为真), 执行大括号内的语句,否则执行if之后的语句

if (a > 10) {

printf("%d\n", a);

}

//练习1

char sex = 0;

// scanf("%c", &sex);

if (sex == 'm') {

printf("男\n");

//枚举 enum + season是枚举类型的名字

enum season c =winter;

printf("%d\n", c);

enum company lanou =teaching;

printf("请输入分机号:\n");

scanf("%d", &lanou);

//switch 根据括号内的表达式的值与switch中对应的case后的常量进行匹配,一旦发现相同,就执行case分支的语句,如果没有匹配到对应的case,则执行default分支的语句.

//default语句的执行顺序与位置无关,只要未匹配到对应的分支就会执行default语句.

switch (lanou) {

case teaching:

{

int d = 5;

printf("接通教学部\n");

break;//结束当前的分支,跳出switch,执行switch之后的语句

}//如果在case分支中定义变量,必须要在case分支中加上{}

case market:

printf("接通市场部\n");

break;

case person:

printf("接通人事部\n");

break;

case consult:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!