1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
// 保留关键字以及以_开头的标识符和标准函数名(如:printf)不能定义为变量名 auto enum restrict unsigned break extern return void case float short volatile char for signed while const goto sizeof _Bool continue if static _Complex default inline struct _Imaginary do int switch double long typedef else register union // 非打印字符 \a \b \f \n \r \t \v \\ \' \" \? \0oo \xhh // 导入标准io库(头文件) #include导入 <>系统本身有的 stdio.h标准io库 .h头文件(类似于php中的interface) #include <stdio.h> // 导入int类型库(头文件) 可以使用 uint32_t int16_t等 #include <inttypes.h> // 导入整数限制库(头文件) #include <limits.h> // 导入浮点数限制库(头文件) #include <float.h> // 导入字符串库(头文件) 可以使用strlen等函数 #include <string.h> // 导入自定义函数库(头文件) ""自定义的 #include "max.h" // 定义常量(不要用=赋值 不要用;) 可以是整数、浮点、字符、字符串等 #define PI 3.14 #define STR "length test" // 声明自定义函数 void log(void); // 入口函数可以不接收参数 int main(void) { // 可以多个声明 int a, b; // 字符串声明 其实只能放19个字符,因为最后一个是不可见的空格符\0用于让计算机知道字符串在哪来中断 char c[20]; // 可以声明并赋值 int d = 2; // 另一种常量声明方法,e为只读的3.14 const float e = 3.14; // 自定义函数如果要在main下方实现,需要在mian上方申明 log(); // 自定义函数也可以从外部导入 a = max(PI, d); // 接受用户输入一个整型 scanf("%d", &b); // 引用型输入不需要&符合 scanf("%s", c); // 输出用户输入的整型 printf("%d %s\n", b, c); // 获取int型占多少字节 4 sizeof(int); // 获取d这个整型占多少字节 4 sizeof(d); // 获取c这个字符串占多少字节 20 sizeof(c); // 获取c这个字符串的长度 比如输入test则为4 strlen(c); // 获取STR常量占多少字节(后面有个不可见的空格符\0) 12 sizeof(STR); // 获取STR常量的长度 11 strlen(STR); // 用户输入任意键后继续执行,scanf时用户可能输入回车,所以可以用2个getchar()扛住 getchar(); getchar(); return 0; } // 自定义函数实现 void log(void) { printf("this is a log.."); } |