一. 字符串相关
1. c中表示字符串的方式有以下几种:
char a[] = {'a','b','\0'};// \0在asicc表中为值为0, 是一个空字符,用数组方式表示字符串时,必须末尾加上\0,否则编译器当做字符数组处理 char a[] = "ab";// 该方式,编译器会在末尾自动加上\0
2. 打印字符串的方式:
printf("%s",a);//不会自动换行,但是是可变参数 puts(a);//该方式会自动换行
二. 指针相关
1. 指针表示字符串:
char *p = "ab";//指向常量的指针 char s = "ab"; char *p = &s;//指向变量的指针2. 利用指针便利字符串
char s = "abc"; char *p = &s; for(;*p!='\0';p++){ printf("%c=\n",*p) }
3. 指向函数的指针
int sum(int a,int b){ return a + b; } int main(){ int (*p)(int, int) = sum;//函数名代表函数的地址,函数指针用来当作参数被其他函数调用 //指针调用函数的2种方式 (*p)(3,4); //或者 p(3,4); return 0; }
1. #ifdef :
//为了防止一个源程序被重复include,采用宏定义来判断 #ifndef _FILENAME_H #define _FILENAME_H //your code... #endif
四. 关键字
1. static : 类似java的private,修饰的变量和函数仅仅自己能访问
2. static和extern能直接定义一个函数,但是只能声明变量
extern int a;//只是声明全局变量a static int b; int a;//这里是定义a,也可以是声明a
五. 结构体
1. 定义和使用
struct Student{ int age; char *name; float height; };//分号不能少 struct Student s = {12,"dance",1.8f}; printf("age=%d\n",s.age);
1. 定义和使用
enum Season{spring,summer,autumn,winter};//默认以整形来存储,值依次是0,1,2,3 enum Season s = winter;