一、局部变量和全局变量
1.局部变量:在还是内部定义的变量
*作用域:从定义变量开始,到代码块结束
*生命周期:定义开始到代码块结束,就会被回收
*函数的形参也是局部变量
*无固定初值
2.全局变量:函数外面定义的变量
*作用域:定义开始到文件结尾(能被后面所有函数共享)
*生命周期:程序一开始就分配内存,到程序退出才销毁
*默认的初值为0
#include<stdio.h>
int age;
void test(){
int age;
age=10;
}
int main(){
printf("%d\n",age);//0int age=20;
printf("%d\n",age);//20
test();//直接调用函数
printf("%d\n",age);//20
return 0;
}
二、结构体
1、构造类型:数组、结构体
#include<stdio.h>
int main(){
//1>.定义结构体类型,例
struct Person{//结构体里面的3个变量称为结构体的成员或者属性
int age;//年龄
double height;//身高
char *name;//姓名
};
struct Person p={ 20,1055,"jack"};
p.age=30;
p.name="rose";
printf("age=%d,name=%s,height=%f\n",p.age,p.name,p.height);
/*错误写法
struct Person p1;
p1={30,1.69,"jack"};
*/
struct Person p2={.age=30,.height=1.69,.name="jack"};
return 0;
}
#include<stdio.h>
int main(){
void test(){//补齐算法(对齐算法),结构体所占内存空间必须是最大成
员字节数的倍数,所以本结构体占16个字节的内存空间
struct Student{
int age;//4个字节
char name;//8个字节
};
}
void test1(){
//1.定义结构体类型
struct Date{
int year;
int mouth;
int day;
};
//2.定义结构体变量(真正分配内存空间)
struct Date d1={ 2011,10,3};
struct Date d1={ 2012,5,12};
d2=d1;//会将d1所有成员的值对应的赋值给d2的所有成员的值
d2.year=2010;
printf("%d-%d-%d\n"d1.year,d1.mouth,d1.day);
printf("%d-%d-%d\n"d2.year,d2.mouth,d2.day);
}
return 0;
}
2、定义变量的三种方式#include<stdio.h>
int main(){
//第三种方式
struct {
int age;
double height;
char *name;
}stu;
//第二种方式:定义类型的同时,定义了变量
struct Student{
int age;
double height;
char *name;
}stu;
/*
//第一种方式
struct Student{
int age;
double height;
char *name;
};//变量
struct Student stu={30,1.69,"jack"};
*/
return 0;
}
注:结构体类型不能重复定义,结构体类型也有作用域,从定义开始到全部代码块结束(跟全局变量一样),符合就近原则
3、结构体与数组
#include<stdio.h>
int main (){
struct RankRecord{
int numb;
int score;
char *name;
};
/*
struct RankRecord r1={1,"jack",5000};
struct RankRecord r2={2,"jaok",3000};
struct RankRecord r3={3,"jark",1000};
*/
struct RankRecord rec[3]{
{1,"jack",5000},
{2,"jaok",3000},
{3,"jark",1000}
};
for(int i=0;i<3;i++){
printf("%d\t%s\t%d\n",rec[i].num,rec[i].name,rec[i].score);
}
return 0;
}
4、指向结构体的指针#include<stdio.h>
int main (){
struct Student{
int numb;
int age;
};
struct Student stu={1,20};
//指针p将来指向struct Student类型的数据
struct Student *p;
//指针变量p指向stu变量
p=&stu;
//第一种方式
printf("age=%d,numb=%d\n",stu.age,stu.numb);
//第二种方式
printf("age=%d,numb=%d\n",(*p).age,(*p).numb);
//第三种方式
printf("age=%d,numb=%d\n",p->age,p->numb);
return 0;
}
5、结构体的嵌套<pre name="code" class="cpp">#include<stdio.h>
int main (){
struct Date{
int year;
int mouth;
int day;
};
struct Student{
int numb;//学号
struct Date birthday;//生日
struct Date ruxueday;//入学日期
};
struct Student stu={1,{2000,9,10},{2012,5,8}};
printf("year=%d,mouth=%d,day=%d\n",stu.birthday.year,
stu.birthday.mouth,stu.birthday.day);
return 0;
}
三、枚举类型#include<stdio.h>
int main (){
enum Season{//定义枚举类型
spring,summer,autumn,winter
};
enum Season s=summer;
return 0;
}
数据类型总结1、基本数据类型
1.int
1>.long int、long 8字节 %ld
2>.short int、short 2字节 %i
3>.unsigned int、unsigned 4字节 %zd
4>.signed int、signed 、int 4字节 %i
2.float\double
1>.float 4字节 %f
2>.doublet 8字节 %f
3.char
1>.1字节 %c %d
2>.char类型保存在内存中的是它的ASCII值
2、构造类型
1.数组
1>.只能由同一种数据组成
2>.定义:数据类型 数组名[元素个数];
2.结构体
1>.可以由多种类型数据组成
2>.先定义类型 在利用类型定义变量
3、指针类型
1>.变量的定义
int *p;
2>.间接操作变量的值
int a=10;
p=&a;
*p=20;
4、枚举类型
使用场合:当一个变量的取值,只允许有几个固定的取值时
四、预处理指令
1、不带参数的宏#define COUNT 5//把代码里所有不带双引号的COUNT都替
代为5
1>.所有的预处理指令都是以#开头
2>.预处理指令分三种:宏定义、条件编译、文件包含
3>.预处理指令在代码编译成0和1之前执行
4>.预处理指令的位置是随便写的
5>.预处理指令的作用域是从编写指令哪一行开始到文件结束
6>.#undef+宏名是宏定义失效//#undef COUNT
7>.宏定义的宏名一般大写
2、带参数的宏定义
带参数的宏定义效率比函数高
#define sum(a,b) ((a)+(b))//宏定义两个数的和,sum(,)可直
接调用,注意参数和运算结果一定要加上小括号
3、条件编译
1>.用法
#include<stdio.h>
#define A 10
int main(){
int a=10;
if(a==10){
printf("a是10\n");
}
else if(a==5){printf("a是5\n");
else{printf("a是其他值\n");
}
#if(A==10)
printf("A是10\n");
#elif(A==5)
printf("A是5\n");
#else
printf("A是其他值\n");
#endif//必须写#endif,条件结束
return 0;
}
2>.3种形式#if defined A
#endif
#ifdef A
#endif
#ifndef A
#endif
4、文件包含
1.<>表示系统自带的文件,""表示自定义的文件
2.不允许循环包含
3.编译包含自定义头文件的时候必须一起编译该自定义文件
4.为了使文件只包含一次
#ifndef A
#defined A
#include<stdio.h>
#endif
5.宏名必须唯一,一般以文件名为宏名
五、typedef关键字的基本使用
1>.定义(整型)变量时
typedef (int) MyInt
typedef MyInt MyInt1 {
MyInt c=10;//int 类型
MyInt1 b=11;//int类型
}
2>.还可以给指针类型起别名
typedef char * String//第一种
typedef struct Student {//第二种
}*MyStu;
3>.还可以给结构体/枚举起别名
typedef struct Student MyStu
这样也可以起别名MyStu
typedef struct Student {
}MyStu;
4>.对函数也可以
typedef int (*MyPoiont)(int,int);
int minus(int a,int b){
return a-b;
}
int sum(int a,int b){
return a+b;
}
int main{
MyPoiont p=sum;
MyPoiont p2=minus;
return 0;
}
5>.使用注意:区别开#define Integer char*和typedef char* Integer;六、static和extern(外部)关键字对函数的作用
1、外部函数:定义的函数能被本文件和其他文件访问//默认情况
extern void test(){//完整定义外部函数
printf("调用了test函数\n");
}
extern void test();//完整的声明外部函数
2、内部函数:定义的函数只能被本文件直接访问,其他文件不能直接访问
static void test(){//完整定义内部函数,不允许其他函数直接调用
}
static void test();完整的声明内部函数
注意:允许不同文件中有同名的内部函数,但是不允许有同名的外部函数