C | 学习笔记
C99标准
1. Hello World
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
2. 注释
int main()
{
return 0;
}
3. 变量与常量
3.1. C基本类型
数字
- char
- int
- short
- long
- long long
浮点数
3.2. 变量声明
变量名要求
必须是小写字母、大写字母、数字和下划线(_),第一个字母必须是字母或下划线。
变量声明
int main()
{
int a = 1;
int b = 2, c = 3;
int d, e = 4;
}
静态变量
#include <stdio.h>
void tryStatic(void);
int main()
{
int count;
for (count = 1; count <= 3; count++)
{
tryStatic();
}
return 0;
}
void tryStatic()
{
int fade = 1;
static int stay = 1;
printf("fade = %d and stay = %d\n", fade++, stay++);
}
Output
fade = 1 and stay = 1
fade = 1 and stay = 2
fade = 1 and stay = 3
3.3. 常量
3.3.1. define常量
#include <stdio.h>
#define MAX_AGE 200
int main()
{
printf("%d\n", MAX_AGE);
}
3.3.2. const
#include <stdio.h>
int main()
{
const int MAX_AGE = 200;
printf("%d\n", MAX_AGE);
return 0;
}
3.4. 枚举类型
后续更新…
4. 输入与输出
4.1. 输出
#include <stdio.h>
int main()
{
int a = 16;
printf("八进制: %o 十进制: %d 十六进制: %x", a, a, a);
char c = 'a';
printf("%c", c);
}
4.2. 输入
#include <stdio.h>
int main()
{
char c;
printf("Please enter a character.\n");
scanf("%c", &c);
printf("Input content: %c", c);
return 0;
}
Output
Please enter a character.
1
Input content: 1
5. 循环
5.1. for
#include <stdio.h>
int main() {
for (int i = 1; i < 5; i++) {
printf("i=%d\n", i);
}
}
Output
i=1
i=2
i=3
i=4
5.2. do while
无论条件是否满足都会执行一次。
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d", i);
} while (i < 5);
}
Output
10
6. 分支跳转
注意:可以使用break和continue这里不在演示
6.1. if
#include <stdio.h>
int main()
{
int score = 10;
if (score < 60)
{
printf("不及格");
} else if (score < 70)
{
printf("及格");
} else if (score < 90)
{
printf("良");
} else
{
printf("优");
}
}
Output
不及格
6.2. 三元运算符
#include <stdio.h>
int main()
{
int num1 = 10;
int num2 = 11;
int max = num1 > num2 ? num1 : num2;
printf("%d\n", max);
}
6.3. switch
#include <stdio.h>
int main()
{
int floor;
printf("Input floor:");
scanf("%d", &floor);
switch (floor)
{
case 1:
printf("去一楼");
break;
case 2:
printf("去二楼");
break;
case 3:
printf("去三楼");
break;
default:
printf("未知楼层");
}
}
Output
Input floor:1
去一楼
6.4. go to
#include <stdio.h>
int main()
{
int num = 10;
if (num >= 10)
{
goto a;
}
// 跳过此处
num++;
a:
printf("%d\n", num);
}
Output
10
7. 函数
7.1. 值传递
#include <stdio.h>
int sum(int a, int b);
int main()
{
int r = sum(1, 2);
printf("%d", r);
}
int sum(int a, int b)
{
return a + b;
}
7.2. 地址传递
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int a = 1;
int b = 2;
swap(&a, &b);
printf("a=%d b=%d\n", a, b);
}
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
7.3. 保护参数
#include <stdio.h>
int sum(const int arr[], int n)
{
int i;
int total = 0;
// arr[0] = 1; // Output: error: read-only variable is not assignable
for (i = 0; i < n; i++)
{
total += arr[i];
}
return total;
}
int main()
{
int arr[3] = {1, 2, 3};
int result = sum(arr, 3);
printf("%d\n", result); // Output: 7
}
7.4. 可变参数
#include <stdio.h>
#include <stdarg.h>
int sum(int count, ...)
{
int result = 0;
va_list vaList;
va_start(vaList, count);
int i;
for (i = 0; i < count; i++)
{
result += va_arg(vaList, int);
}
va_end(vaList);
return result;
}
int main()
{
printf("%d\n", sum(2, 3, 4));
}
8. 数组
注意
8.1. 数组声明
int arr[5];
int arr2[] = {1, 2, 3};
8.2. 遍历
#include <stdio.h>
int main()
{
int arr2[] = {1, 2, 3};
int len = sizeof(arr2) / sizeof(arr2[0]);
int i;
for (i = 0; i < len; i++)
{
printf("%d\n", arr2[i]);
}
}
9. 指针
9.1. 地址引用示例
#include <stdio.h>
int main()
{
int i = 1;
int *p1 = &i;
*p1 = 2;
printf("%d", i);
}
9.2. 指针运算
这里只是演示指针加运算,还可以减运算。
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3};
int *p1 = arr;
int *p2 = &arr[1];
int *p3 = &arr[2];
printf("%ld\n", p3 - p1);
printf("%d\n", *(p1 + 1));
printf("%d\n", *(p2 - 1));
printf("%d", p1 == p2);
}
9.3. 指针运算和二组数组
#include <stdio.h>
int main()
{
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("%lu\n", sizeof(arr[0][0]));
printf("arr[0]=%p arr[0][0]=%p\n", arr[0], &arr[0][0]);
printf("arr=%p arr+1=%p\n", arr, arr + 1);
}
10. 内存管理
#include <stdlib.h>
int main()
{
double *temp = (double *) malloc(10 * sizeof(double));
free(temp);
double *temp2 = (double *) calloc(10, sizeof(double));
free(temp2);
return 0;
}
11. 限定词
#include <stdlib.h>
int main()
{
const int i = 1;
volatile int loc1;
int *restrict temp = (int *) malloc(10 * sizeof(int));
free(temp);
return 0;
}
12. 结构体
#include <stdio.h>
#include <string.h>
struct book
{
char title[40];
char author[40];
float price;
};
int main()
{
struct book b1 = {
.title = "Example",
.author = "yimt",
.price = 10.5f
};
printf("%s %s %f", b1.title, b1.author, b1.price);
struct book b2;
strcpy(b2.title, "Example");
strcpy(b2.author, "yimt");
b2.price = 10.5f;
printf("%s %s %f", b1.title, b1.author, b1.price);
return 0;
}
13. 预处理
13.1. define
#include <stdio.h>
#define MAX 10
#define SQUARE(x) x*x
#define PSQR(name) printf("Hello %s\n", name)
#define XNAME(n) x ##n
#define PRINT_XN(n) printf("%d\n", x ##n)
#define PR(...) printf(__VA_ARGS__)
int main()
{
printf("%d\n", SQUARE(2));
PSQR("yimt");
int XNAME(1) = 10;
int XNAME(2) = 11;
printf("%d %d\n", x1, x2);
PRINT_XN(1);
PRINT_XN(2);
PR("yimt\n");
PR("hello %s", "yimt\n");
return 0;
}
13.2. include
#include <stdio.h>
#include "stdlib.h"
14. 注意
14.1. 浮点数精度变量
#include <stdio.h>
int main()
{
float f = 0.12345678;
printf("%f", f);
return 0;
}
15. 参考
|