------
Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
预处理指令
- 所有的预处理指令都是以#开头
- 预处理指令分3种:1.宏定义 2.条件编译3.文件包含
- 预处理指令在代码翻译成0和1之前执行
- 预处理的位置是随便写的
- 预处理指令的作用域:从编写指令的那一行开始,一直到文件结尾,可以用#udef取消宏定义的内容
- 宏名一般用大写或者以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()
{
}
带参数的宏
带参数的宏定义效率比函数高
#include <stdio.h>
#define sum(v1, v2) ((v1)+(v2))
#define pingfang(a) ((a)*(a))
int main()
{
// pingfang(5+5) (10*10)
// pingfang(5+5)
// pingfang(5+5) (35)
// pingfang(5+5)/pingfang(2)
int c = pingfang(5+5)/pingfang(2);
printf("c is %d\n", c);
/*
int c = sum(2, 3) * sum(6, 4);
printf("c is %d\n", c);*/
/*
int a = 10;
int b = 20;
int c = sum(a, b);
printf("c is %d\n", c);
//int c = sum(a, b);*/
return 0;
}
条件编译
只要写了#if,在最后面必须加上#endif
//#define A 5
int main()
{
#ifndef A
//#ifdef A
//#if !defined(A)
printf("哈哈\n");
#endif
//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
*/
return 0;
}
条件编译在文件包含中的使用,可以提高编译效率
宏名用文件名大写加下划线
#ifndef LISI_H
#define LISI_H
int sum(int a, int b);
#endif
typedef关键字
作用:给已经存在的类型起一个新的名称
使用场合:1.基本数据类型 2.指针 3.结构体 4.枚举 5.指向函数的指针
#include <stdio.h>
typedef int MyInt;
typedef MyInt MyInt2;
// 给指针类型char *起一个新的类型名称String
typedef char * String;
/*
struct Student
{
int age;
};
typedef struct Student MyStu;
*/
/*
typedef struct Student
{
int age;
} MyStu;
*/
typedef struct
{
int age;
} MyStu;
/*
enum Sex {Man, Woman};
typedef enum Sex MySex;
*/
typedef enum {
Man,
Woman
} MySex;
typedef int (*MyPoint)(int, int);
int minus(int a, int b)
{
return a - b;
}
int sum(int a, int b)
{
return a + b;
}
/*
struct Person
{
int age;
};
typedef struct Person * PersonPoint;
*/
typedef struct Person
{
int age;
} * PersonPoint;
int main()
{
// 定义结构体变量
struct Person p = {20};
PersonPoint p2 = &p;
//struct Person *p2 = &p;
//MyPoint p = sum;
//MyPoint p2 = minus;
//int (*p)(int, int) = sum;
//int (*p2)(int, int) = minus;
//p(10, 11);
//MySex s = Man;
//enum Sex s = Man;
//enum Sex s2 = Woman;
// struct Student stu3;
//MyStu stu = {20};
//MyStu stu2= {21};
return 0;
}
void test2()
{
String name = "jack";
printf("%s\n", name);
}
void test()
{
int a;
MyInt i = 10;
MyInt2 c = 20;
MyInt b1, b2;
printf("c is %d\n", c);
}
static和extern对函数的作用
外部函数:定义的函数能背本文件和其他文件访问
- 默认情况下所有函数都是外部函数
- 不允许有同名的外部函数
内部函数:定义的函数只能被本文件访问,其他文件不能访问
- 允许不同文件中有同名的内部函数
static对函数的作用:
- 定义一个内部函数
- 声明一个内部函数
extern对函数的作用(extern可以省略,默认情况下声明和定义的函数都是外部函数)
- 完整的定义一个外部函数
- 完整的声明一个外部函数
全局变量分两种:
外部变量:定义的变量能被本文件和其它文件访问
- 默认情况下,所有的去啊句变量都是外部变量
- 不同文件中的同名外部变量,都代表着同一个变量
内部变量:定义的变量只能被本文件访问,不能被其他文件访问
- 不同文件中的同名内部变量,互不影响
static对变量的作用:定义一个内部变量
extern对变量的作用:声明一个外部变量
代码笔记:
main.c
#include <stdio.h>
void test();
// 定义一个外部变量
//int a; 这么多个a都是代表同一个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;
one.c
#include <stdio.h>
int a;
static int b;
void test()
{
printf("b的值是%d\n", b);
/*
printf("a的值是%d\n", a);
a = 20;*/
}
static修饰局部变量的使用场合:
- 如果这个函数的调用频率很高
- 这个函数内部的某个变量值是固定的
static修饰局部变量的作用:
- 延长局部变量的生命周期:程序结束的时候,局部变量才会被销毁
- 并没有改变局部变量的作用域
- 所有的函数都共享着一个变量
代码笔记:
#include <stdio.h>
void test()
{
static double pi = 3.14;
double zc = 2 * pi * 10;
int a = 0;
a++;
printf("a的值是%d\n", a); // 1
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;
}#include <stdio.h>
void test()
{
static double pi = 3.14;
double zc = 2 * pi * 10;
int a = 0;
a++;
printf("a的值是%d\n", a); // 1
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;
}
递归
递归的两个条件:
- 函数自己调用自己
- 必须有个明确的返回值
代码笔记:
#include <stdio.h>
int pow2(int b, int n);
int main()
{
int c = pow2(3, 2);
printf("%d\n", c);
return 0;
}
/*
pow2(b, 0) == 1
pow2(b, 1) == b == pow2(b, 0) * b
pow2(b, 2) == b*b == pow2(b, 1) * b
pow2(b, 3) == b*b*b == pow2(b, 2) * b
1> n为0,结果肯定是1
2> n>0,pow2(b, n) == pow2(b, n-1) * b
*/
int pow2(int b, int n)
{
if (n <= 0) return 1;
return pow2(b, n-1) * b;
}
/*
int pow2(int b, int n)
{
// 用来保存计算结果
int result = 1;
//result *= b;
//result *= b;
//result *= b;
//result *= b;
//....
//n次
for (int i = 0; i<n; i++)
{
result *= b;
}
return result;
}*/