结构体
#include <stdio.h>
#include <string.h>
struct Student{
char name[ 30 ] ;
int age;
char address[ 100 ] ;
} st3, st4;
struct Student st1 = { "lisi" , 11 , "China Shanghai" } , str2= { "wangwu" , 22 , "Chian Guangzhou" } ;
struct Student str5, student[ 10 ] = {
{ "lisi" , 11 , "China shanghai" } ,
{ "wangwu" , 22 , "China shanghai" }
} ;
struct Student * pst;
pst = & student[ 2 ] ;
strcpy ( st5. name, "zhangsan" ) ;
st5. age = 20 ;
strcpy ( st5. address, "China Beijing" ) ;
printf ( "%s,%d,%s\n" , st5. name, st5. age, st5. address) ;
printf ( "%s,%d,%s\n" , st1. name, st1. age, st1. address) ;
printf ( "%s,%d,%s\n" , st2. name, st2. age, st2. address) ;
printf ( "%s,%d,%s\n" , student[ 0 ] . name, student[ 0 ] . age, student[ 0 ] . address) ;
printf ( "%s,%d,%s\n" , student[ 1 ] . name, student[ 1 ] . age, student[ 1 ] . address) ;
printf ( "%s,%d,%s\n" , student[ 2 ] . name, student[ 2 ] . age, student[ 2 ] . address) ;
printf ( "请输入学生姓名,年龄,家庭地址:" ) ;
scanf ( "%s" , student[ 2 ] . name) ;
scanf ( "%d" , & student[ 2 ] . age) ;
scanf ( "%s" , student[ 2 ] . address) ;
printf ( "%s,%d,%s\n" , student[ 2 ] . name, student[ 2 ] . age, student[ 2 ] . address) ;
printf ( "%s,%d,%s\n" , pst-> name, pst-> age, pst-> address) ;
printf ( "%s,%d,%s\n" , ( * pst) . name, ( * pst) . age, ( * pst) . address) ;
使用结构体变量的指针作为函数参数
#include <stdio.h>
struct Student{
char name[ 20 ] ;
float score[ 3 ] ;
} student1= { "zhangsan" , 99 , 95 , 98.5 } ;
student2 = { "lisi" , 10 , 20 , 59.5 } ;
void display ( struct Student * stu) {
printf ( "-------学生信息--------\n" ) ;
printf ( "姓名:%s\n" , stu-> name) ;
printf ( "英语成绩:%.1\n" , stu-> score[ 0 ] ) ;
printf ( "数学成绩:%.1\n" , stu-> score[ 1 ] ) ;
printf ( "语文成绩:%.1\n" , stu-> score[ 2 ] ) ;
}
int main ( ) {
struct Student * p1, * p2;
p1 = & student1;
p2 = & student2;
display ( & student1) ;
display ( & student2) ;
display ( p1) ;
display ( p2) ;
student2. score[ 0 ] = 99.9 ;
display ( p2) ;
p2-> score[ 1 ] = 88.8 ;
display ( p2) ;
return 0 ;
}
包括结构体的结构体
#include <stdio.h>
struct date{
int year;
int month;
int day;
} ;
struct student{
char name[ 20 ] ;
char sex;
char number[ 10 ] ;
struct date birthday;
} st = { "wangwu" , 'M' , "2020000001" , { 1999 , 10 , 1 } } ;
int main ( ) {
printf ( "------学生信息------\n" ) ;
printf ( "姓名:%s\n" , st. name) ;
printf ( "姓名:%c\n" , st. sex) ;
printf ( "性别:%s\n" , st. sex == 'M' ? "男" :"女" ) ;
printf ( "学号:%s\n" , st. number) ;
printf ( "出生日期:%d年%d月%d日\n" , st. birthday. year, st. birthday. month, st. birthday. day) ;
return 0 ;
}
共用体
union 共用体名{
成员列表;
} ;
#include <stdio.h>
union DataUnion {
int a;
char c;
}
int main ( ) {
union DataUnion u1;
printf ( "%lu\n" , sizeof u1) ;
u1. a = 97 ;
printf ( "%d\n" , u1. a) ;
printf ( "%d\n" , u1. c) ;
u1. c= 'd' ;
printf ( "%d\n" , u1. a) ;
printf ( "%c\n" , u1. c) ;
u1. a = 256 + 97 ;
printf ( "%d\n" , u1. a) ;
printf ( "%c\n" , u1. c) ;
return 0 ;
}
枚举类型
#include <stdio.h>
enum Colors{
red = 1 , green, blue, black
} ;
int main ( ) {
int i;
printf ( "请输入一个整数(1~4)选择颜色:" ) ;
scanf ( "%d" , & i) ;
switch ( i) {
case red:
printf ( "你选择了红色" ) ;
break ;
case green:
printf ( "你选择了绿色" ) ;
break ;
case blue:
printf ( "你选择了蓝色" ) ;
break ;
case black:
printf ( "你选择了黑色" ) ;
break ;
default :
printf ( "输入有误!!" ) ;
}
printf ( "\n" ) ;
return 0 ;
}
预处理-宏定义
#define PI 3.14159
#define SIDE 40
#define PERIMTER 4*SIDE
#define AREA SIDE*SIDE
#define WELCOME "Welcome to China!!\n"
#include <stdio.h>
int main ( ) {
printf ( "%d\n" , PERIMTER) ;
printf ( WELCOME) ;
printf ( "%f\n" , PI) ;
#undef PI
return 0 ;
}
带参数的宏
#include <stdio.h>
#define MIX(a,b,c) a+b*c
#define MIX2(a,b,c) (a)+(b)*(c)
int getMix ( int a, int b, int c) {
return a+ b* c;
}
int main ( ) {
printf ( "%d\n" , MIX ( 1 , 2 , 3 ) ) ;
printf ( "%d\n" , MIX ( 3 , 5 , 7 ) ) ;
printf ( "%d\n" , MIX ( 1 + 2 , 2 + 3 , 3 + 4 ) ) ;
printf ( "%d\n" , getMix ( 1 , 2 , 3 ) ) ;
printf ( "%d\n" , getMix ( 3 , 5 , 7 ) ) ;
printf ( "%d\n" , getMix ( 1 + 2 , 2 + 3 , 3 + 4 ) ) ;
printf ( "%d\n" , MIX2 ( 1 + 2 , 2 + 3 , 3 + 4 ) ) ;
return 0 ;
}
包含文件include “”
#include <stido.h>
#include "07.h"
int main ( ) {
int a;
P ( "请输入一个整数:" ) ;
S ( D, & a) ;
P ( "输入的整数是:" ) ;
P ( D, a) ;
P ( "\n" ) ;
P ( C, a) ;
P ( "\n" ) ;
return 0 ;
}
#define P printf
#define S scanf
#define D "%d"
#define C "%c"