1. Hello WorldC99标准
#include2. 注释// 包含另一个文件 int main() // 入口函数 { printf("Hello, World!n"); // Output: Hello, World! return 0; }
int main()
{
// 注释示例1
return 0;
}
3. 变量与常量
3.1. C基本类型
数字
- char
- int
- short
- long
- long long
浮点数
- float
- double
- long double
变量名要求
必须是小写字母、大写字母、数字和下划线(_),第一个字母必须是字母或下划线。
变量声明
int main()
{
int a = 1; // 示例1
int b = 2, c = 3; // 示例2
int d, e = 4; // 示例3
}
静态变量
#includevoid 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 = %dn", fade++, stay++); }
Output
fade = 1 and stay = 1 fade = 1 and stay = 2 fade = 1 and stay = 33.3. 常量 3.3.1. define常量
#include3.3.2. const#define MAX_AGE 200 int main() { printf("%dn", MAX_AGE); // Output: 200 }
#include3.4. 枚举类型int main() { const int MAX_AGE = 200; printf("%dn", MAX_AGE); // Output: 200 return 0; }
后续更新…
4. 输入与输出 4.1. 输出#include4.2. 输入int main() { int a = 16; printf("八进制: %o 十进制: %d 十六进制: %x", a, a, a); // Output: 八进制: 20 十进制: 16 十六进制: 10 char c = 'a'; printf("%c", c); // Output: a }
#includeint 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: 15. 循环 5.1. for
#includeint main() { for (int i = 1; i < 5; i++) { printf("i=%dn", i); } }
Output
i=1 i=2 i=3 i=45.2. do while
无论条件是否满足都会执行一次。
#includeint main() { int i = 10; do { printf("%d", i); } while (i < 5); }
Output
106. 分支跳转
6.1. if注意:可以使用break和continue这里不在演示
#includeint main() { int score = 10; if (score < 60) { printf("不及格"); } else if (score < 70) { printf("及格"); } else if (score < 90) { printf("良"); } else { printf("优"); } }
Output
不及格6.2. 三元运算符
#include6.3. switchint main() { int num1 = 10; int num2 = 11; int max = num1 > num2 ? num1 : num2; printf("%dn", max); // Output: 11 }
#includeint 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
#includeint main() { int num = 10; if (num >= 10) { goto a; } // 跳过此处 num++; a: printf("%dn", num); }
Output
107. 函数 7.1. 值传递
#include7.2. 地址传递// 函数声明 int sum(int a, int b); int main() { int r = sum(1, 2); printf("%d", r); // Output: 3 } // 函数定义 int sum(int a, int b) { return a + b; // 函数返回值 }
#include7.3. 保护参数void swap(int *a, int *b); int main() { int a = 1; int b = 2; swap(&a, &b); printf("a=%d b=%dn", a, b); // Output: a=2 b=1 } void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
#include7.4. 可变参数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("%dn", result); // Output: 7 }
#include8. 数组#include 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); // 回收vaList空间 return result; } int main() { printf("%dn", sum(2, 3, 4)); // Output: 7 }
注意
- 数组地址传递
// 声明
// 示例1
int arr[5];
// 示例2
int arr2[] = {1, 2, 3};
8.2. 遍历
#include9. 指针 9.1. 地址引用示例int main() { int arr2[] = {1, 2, 3}; // 遍历 int len = sizeof(arr2) / sizeof(arr2[0]); int i; for (i = 0; i < len; i++) { printf("%dn", arr2[i]); } }
#include9.2. 指针运算int main() { int i = 1; int *p1 = &i; *p1 = 2; printf("%d", i); // Output: 2 }
这里只是演示指针加运算,还可以减运算。
#include9.3. 指针运算和二组数组int main() { int arr[] = {1, 2, 3}; int *p1 = arr; int *p2 = &arr[1]; int *p3 = &arr[2]; // 计算2个指针中间间隔 printf("%ldn", p3 - p1); // Output: 2 // 指针加 printf("%dn", *(p1 + 1)); // Output: 2 // 指针减 printf("%dn", *(p2 - 1)); // Output: 1 // 比较指针 // 注意:此处0是指不相同意思 printf("%d", p1 == p2); // Output: 0 }
#include10. 内存管理int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; printf("%lun", sizeof(arr[0][0])); // Output: 4 printf("arr[0]=%p arr[0][0]=%pn", arr[0], &arr[0][0]); // Output: arr[0]=0x7ff7bdb05900 arr[0][0]=0x7ff7bdb05900 printf("arr=%p arr+1=%pn", arr, arr + 1); // Output: arr=0x7ff7bdb05900 arr+1=0x7ff7bdb0590c }
#include11. 限定词int main() { // 示例1 // 申请内存 double *temp = (double *) malloc(10 * sizeof(double)); // 回收内存 free(temp); // 示例2 // 申请内存 double *temp2 = (double *) calloc(10, sizeof(double)); // 回收内存 free(temp2); return 0; }
#include12. 结构体int main() { // const const int i = 1; // i = 2; // 禁止修改 // volatile // 易变的位置,不使用缓存 volatile int loc1; // restrict // 告诉编译器,对象已经被指针所引用,不能通过除该指针外所有其他直接或间接的方式修改该对象的内容。 int *restrict temp = (int *) malloc(10 * sizeof(int)); free(temp); return 0; }
#include13. 预处理 13.1. define#include struct book { char title[40]; char author[40]; float price; }; int main() { // 示例1 struct book b1 = { .title = "Example", .author = "yimt", .price = 10.5f }; printf("%s %s %f", b1.title, b1.author, b1.price); // Output: Example yimt 10.500000 // 示例2 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);// Output: Example yimt 10.500000 return 0; }
#include13.2. include// 常量 #define MAX 10 // 宏 #define SQUARE(x) x*x #define PSQR(name) printf("Hello %sn", name) // ##运算符 #define XNAME(n) x ##n #define PRINT_XN(n) printf("%dn", x ##n) // 可变宏 #define PR(...) printf(__VA_ARGS__) int main() { // 宏 printf("%dn", SQUARE(2)); // Output: 4 PSQR("yimt"); // Output: Hello yimt // ##运算符 int XNAME(1) = 10; int XNAME(2) = 11; printf("%d %dn", x1, x2); // Output: 10 11 PRINT_XN(1); // Output: 10 PRINT_XN(2); // Output: 11 // 可变宏 PR("yimtn"); // Output: yimt PR("hello %s", "yimtn"); // Output: hello world return 0; }
#include14. 注意 14.1. 浮点数精度变量// 优先搜索系统目录 #include "stdlib.h" // 优先搜索当前工作目录
#include15. 参考int main() { float f = 0.12345678; printf("%f", f); // Output: 0.123457 return 0; }
- 《C Primer Plus》



