C语言学习笔记(六)宏定义,typedef,static和extern
2014-06-07 17:49
429 查看
一、宏定义
二、条件编译
#define A 10
#if (A == 10)
printf("a的值是10\n");
#elif (A == 5)
printf("a的值是5");
#else
printf("a的值");
#endif
条件编译和文件包含相结合,防止重复拷备。
#ifndef ABC_H
#define ABC_H
#endif
三、typedef定义一种新类型
typedef int MyInt;
指向函数指针类型
typedef (*MyPoint)(int,int);
int sum(int a,int b)
{
return a + b;
}
MyPoint p = sum;
指向结构体指针类型
typedef struct Person{
int age;
int whight;
} *PersonPoint;
struct Person P = {23,45};
PersonPoint p2 =&p;
四、static 和extern对函数的作用
五、static 和extern 对变量的作用
1.。。对全局变量
全局变量分为:外部变量和内部变量
六、static 对局部变量的作用
/* 1.所有的预处理指令都是以#开头 2.预处理指令分3种 1> 宏定义 2> 条件编译 3> 文件包含 3.预处理指令在代码翻译成0和1之前执行 4.预处理的位置是随便写的 5.预处理指令的作用域:从编写指令的那一行开始,一直到文件结尾,可以用#undef取消宏定义的作用 6.宏名一般用大写或者以k开头,变量名一般用小写 */ #include <stdio.h> //#define kCount 4 int main() { char *name = "COUNT"; printf("%s\n", name); #define COUNT 4 int ages[COUNT] = {1, 2, 67, 89}; for ( int i = 0; i<COUNT; i++) { printf("%d\n", ages[i]); } // 从这行开始,COUNT这个宏就失效 #undef COUNT int a = COUNT; return 0; } void test() { }
二、条件编译
#define A 10
#if (A == 10)
printf("a的值是10\n");
#elif (A == 5)
printf("a的值是5");
#else
printf("a的值");
#endif
条件编译和文件包含相结合,防止重复拷备。
#ifndef ABC_H
#define ABC_H
#endif
三、typedef定义一种新类型
typedef int MyInt;
指向函数指针类型
typedef (*MyPoint)(int,int);
int sum(int a,int b)
{
return a + b;
}
MyPoint p = sum;
指向结构体指针类型
typedef struct Person{
int age;
int whight;
} *PersonPoint;
struct Person P = {23,45};
PersonPoint p2 =&p;
四、static 和extern对函数的作用
/* 外部函数:定义的函数能被本文件和其他文件访问 1> 默认情况下所有函数都是外部函数 2> 不允许有同名的外部函数 内部函数:定义的函数只能被本文件访问,其他文件不能访问 1> 允许不同文件中有同名的内部函数 static对函数的作用: 1> 定义一个内部函数 2> 声明一个内部函数 extern对函数的作用: 1> 完整地定义一个外部函数 2> 完整地声明一个外部函数 (extern可以省略,默认情况下声明和定义的函数都是外部函数) */ // 声明一个test函数 // 完整地声明一个外部函数 // extern可以省略 //extern void test(); void test(); //void test2(); int main() { test(); //test2(); return 0; } //void test() //{ // //} static void test2() { }
#include <stdio.h> // 声明一个内部函数 static void test2(); // 完整地定义一个外部函数 /* extern void test() { printf("调用了test函数\n"); }*/ // 默认情况下,所有的函数都是外部函数,所以可以省略extern void test() { printf("调用了test函数\n"); test2(); } // 定义一个内部函数 static void test2() { printf("调用了test2函数\n"); }
五、static 和extern 对变量的作用
1.。。对全局变量
全局变量分为:外部变量和内部变量
/* 全局变量分2种: 外部变量:定义的变量能被本文件和其他文件访问 1> 默认情况下,所有的全局变量都是外部变量 1> 不同文件中的同名外部变量,都代表着同一个变量 内部变量:定义的变量只能被本文件访问,不能被其他文件访问 1> 不同文件中的同名内部变量,互不影响 static对变量的作用: 定义一个内部变量 extern对变量的作用: 声明一个外部变量 static对函数的作用: 定义和声明一个内部函数 extern对函数的作用: 定义和声明一个外部函数(可以省略) */ #include <stdio.h> void test(); // 定义一个外部变量 //int a; 这么多个a都是代表同一个a //int a; //int a; //int a; //int a; // 定义一个内部变量 static int b; // 声明一个外部变量 //extern int a; int main() { //b = 10; //test(); extern int a; a = 10; /* a = 10; test(); printf("a的值是%d\n", a);*/ return 0; } int a;
六、static 对局部变量的作用
#include <stdio.h> /* static修饰局部变量的使用场合: 1.如果某个函数的调用频率特别高 2.这个函数内部的某个变量值是固定不变的 */ void test() { static double pi = 3.14; double zc = 2 * pi * 10; int a = 0; a++; printf("a的值是%d\n", a); // 1 /* static修饰局部变量: 1> 延长局部变量的生命周期:程序结束的时候,局部变量才会被销毁 2> 并没有改变局部变量的作用域 3> 所有的test函数都共享着一个变量b */ static int b = 0; b++; printf("b的值是%d\n", b); // 3 } int main() { for (int i = 0; i<100; i++) { test(); } test(); test(); test(); return 0; }
相关文章推荐
- 【C语言学习笔记】关键字:typedef、static和extern
- 黑 马 程 序 员_视频学习总结<C语言>----07 static和extern关键字、typedef
- 黑马程序员------C语言-----宏定义、typedef、static和extern、递归
- C语言回顾之结构体、枚举、宏定义、typedef、extern和static的使用
- 【学习笔记】【C语言】static和extern对变量的作用
- 【学习笔记】【C语言】static和extern对函数的作用
- C语言数据类型总结,预处理指令,typedef,static&extern,递归笔记
- 【黑马程序员】iOS学习之路——C语言之宏定义、条件编译和条件包含及typedef
- C++学习笔记:extern &amp; static
- 【学习笔记】【C语言】宏定义
- 黑马程序员——C语言------typedef、static和extern
- C语言学习入门 (七) 变量与函数,static和extern关键字
- 黑马程序员--c语言:结构体、枚举、预处理指令、typedef、static与extern、递归思想
- C语言易混淆关键词详解-const,static,extern,typedef,声明
- C语言易混淆关键词详解-const, static, extern, typedef, 声明
- 黑马程序员 C语言 - 09 预处理指令、typedef、extern、static
- 【黑马程序员】C语言学习笔记之typedef(十五)
- 黑马程序员————C语言(预处理指令、static与extern、typedef)
- C语言易混淆关键词详解-const, static, extern, typedef, 声明
- C语言易混淆关键词详解-const, static, extern, typedef, 声明