概述
C 语言是一个中等级语言, 在机器语言和汇编语言之上, 在其他高级语言之下
大部分语言的编译器的内核都是用 C 构建的 (例: python, java, C#, C++, js)
- C 语言不是面向对象的编程语言
- C++是 C 语言的扩展
C 语言对初学者还挺难的(¬‿¬)
开始
stdio.h 包含一些关于输入输出的函数
| 12
 3
 4
 5
 6
 7
 
 | #include <stdio.h>
 
 
 int main(){
 return 0;
 }
 
 | 
在命令行中使用编译器
注释
变量
- signed有符号数据 (默认数据)
- unsigned指代无符号数据
- short短数据
- long长数据 (默认数据)
- long long长长数据
要使用变量类型 bool 需要引入 stdbool.h 头文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | int x = 123;int y = 321;
 
 int age = 22;
 float gpa = 2.05;
 char grade = 'C';
 char name[] = "Bro";
 
 
 double d = 3.141592653589793;
 bool b = true;
 
 unsigned char g = 255;
 
 short int h = 32767;
 unsigned short int i = 65535;
 
 long int l;
 long long int ll = 4656454654654654654654546U;
 
 | 
格式化输出:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | 
 
 
 
 % <标志> <宽度> <精度> <格式化类型>
 
 
 
 float f = 3.1415;
 long long int ll;
 
 
 printf("float: %f", f);
 printf("long long data: %lld", ll);
 
 | 
| Type | 格式化 | 
| unsigned | %u | 
| long | %l | 
| int | %d | 
| float | %f | 
| double | %lf | 
| char | %c | 
| char[] | %s | 
| % | %% | 
常量, 常量在程序中不可改变
| 12
 
 | const float PI = 3.14159;
 
 | 
用户输入
注: & 为取地址符
| 12
 3
 4
 5
 6
 7
 8
 
 | 
 scanf("%d", &age);
 
 
 
 
 fgets(name, 25, stdin);
 
 | 
| 12
 3
 4
 
 | #include <string.h>
 
 strlen()
 
 | 
数学运算
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | #include <math.h>
 sqrt(9);
 pow(2, 4);
 
 round(3.14);
 ceil(3.14);
 floor(3.14);
 fabs(-100);
 
 log(3);
 
 
 sin(45);
 cos(45);
 tan(45);
 
 | 
流程控制
if 语句好像没什么说的…
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | if (true){
 }
 else if(false) {
 
 }
 else{
 
 }
 
 | 
switch 语句
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | int choice;
 switch (choice){
 case 1:
 
 break;
 case 2:
 
 break;
 default:
 
 }
 
 
 | 
逻辑运算符
三元运算符
| 12
 3
 4
 5
 
 | int min;int max;
 int result;
 
 result = (max > min) ? max : min;
 
 | 
函数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | void myFunction(){
 printf("I'm function!");
 }
 
 int main(){
 
 myFunction();
 
 return 0;
 }
 
 | 
参数:
| 12
 3
 4
 
 | void myFunction(int num){
 printf("I'm function! and my argument is %d", num);
 }
 
 | 
原型函数, 是一个函数声明, 在主函数之前的没有函数体的函数
该函数的主要作用是确保函数的参数正确(让编译器不忽略错误的传参)
字符串函数
要使用字符串函数, 需包含 string.h 头文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | #include<string.h>
 char string1[] = "Hi";
 char string2[] = "my name is voilone";
 
 
 strlwr();
 strupr();
 
 strcat(sring1, string2);
 
 strcopy(string1, string2);
 strncopy(string1, string2, n);
 
 strset(string1, '?');
 strnset(string1, '?', 1)
 strrev(string1);
 
 | 
| 12
 3
 4
 5
 6
 7
 
 | strlen(); 
 strcmp();
 strncmp();
 
 strcmpi();
 strnicmp()
 
 | 
循环
- continue跳过一个循环
- break跳出整个循环
for 循环
| 12
 3
 
 | for (int i; i < 10; i++){
 }
 
 | 
while 循环
do while 循环
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | #include<stdbool.h>
 bool judge = false;
 
 do{
 judge = true;
 }while (judeg){
 
 }
 
 | 
数组
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | double prices[3] = {2.2, 5.5, 6.6};
 
 
 int nums[2];
 nums[0] = 1;
 nums[1] = 2;
 
 
 prices[0];
 
 | 
| 12
 3
 4
 
 | int array[] = {1, 2, 3}
 
 sizeof(array)
 
 | 
二维数组
| 12
 3
 4
 5
 
 | int numbers[2][3] = {
 {1, 2, 3},
 {2, 3, 3}
 };
 
 | 
字符串数组
| 12
 3
 4
 5
 6
 
 | #include <string.h>
 char chars[][20] = {"hello", "This char array"};
 
 
 strcopy(chars[0], "New string~");
 
 | 
struct 数据集合
struct 结构体
struct 是一些数据的集合, 每个单一的数据被称为: “成员(member)”
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | struct Player{
 char name[12];
 int score;
 }
 
 struct Player player1;
 struct Player player2;
 
 
 strcopy(player1.name, "voilone");
 player1.score = 55;
 
 | 
struct 数组
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | struct Student{char name[12];
 float gpa;
 }
 
 struct Student student1 = {"voilone", 3.0};
 struct Student student2 = {"sam", 2.0};
 struct Student student3 = {"xv", 4.0};
 
 struct Student students[] = {student1, student2, student3};
 
 | 
typedef
typedef 可以给关键字一个别称
| 12
 3
 4
 
 | typedef char user[25]
 
 user user1[25] = "voilone";
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | struct User{
 char name[25];
 char password[12];
 int id;
 }
 
 struct User user1 = {"voione", "654321", 666};
 
 
 
 
 typedef struct {
 char name[25];
 char password[12];
 int id;
 } User;
 
 User user2 = {"voione", "654321", 666};
 
 | 
enmu 枚举类型
一种用户自定义命名的整数数据, 使其代码更有可读性
| 12
 3
 4
 5
 6
 
 | enum Day{Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7};
 enum Day today = Sun;
 
 
 printf("Today is %d", today)
 
 | 
位运算符
内存地址
- memory一个字节数组
- memory block一个单元的内存(byte), 用于储存数据
- memory address内存块的地址
& 取地址符
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | char a = 'X';char b = 'Y';
 char c = 'Y';
 
 
 printf("%d bytes", sizeof(a));
 
 
 
 printf("%p", &a)
 
 | 
指针
pointer 指针保存一个变量的内存地址
* 为解引用操作符
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | int age = 21;
 
 
 int *pAge = &age;
 
 printf("address: %p", &age);
 prinft("pAge value: %p", pAge);
 
 printf("value: %d", age);
 printf("pAge point to value %d", *pAge)
 
 
 int *pAge2 = NULL;
 pAge2 = &age;
 
 | 
函数与指针
| 12
 3
 
 | void printAge(int *pAge){printf("you are %d olds", *pAge);
 };
 
 | 
其他
随机数
要生成随机数, 需要包含 stdlib.h 头文件
| 12
 3
 4
 5
 6
 7
 8
 
 | #include <stdlib.h>
 
 
 srand(time(0));
 
 
 int number1 = (rand() % 6) + 1;
 
 | 
文件 cz
fopen(path, mode) 模式:
检测是否存在文件:
| 12
 3
 
 | FILE *pF = fopen("test.txt", "r");
 if (pF == NULL) printf("No such file!");
 
 | 
写入文件
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | 
 FILE *pF = fopen("test.txt", "w");
 
 
 fprintf(pF, "some fromat text");
 
 
 fclose(pF);
 
 | 
文件删除:
读取文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | FILE *pF = fopen("test.txt"), "r";
 
 char buffer[255];
 
 
 
 fget(buffer, 255, pF);
 
 
 
 while (fgets(buffer, 255, pF) != NULL){
 printf("%s", buffer)
 }
 
 
 
 |