C语言基础-基本操作
C语言基础知识详解
让我们系统地学习C语言的基础知识。
1. 基本数据类型
#include <stdio.h>
int main() {
// 整数类型
char c = 'A'; // 1字节,-128到127
unsigned char uc = 255; // 1字节,0到255
short s = -32768; // 2字节,-32768到32767
int i = 42; // 4字节,-2^31到2^31-1
long l = 123456L; // 4或8字节
long long ll = 123456LL;// 8字节
// 浮点类型
float f = 3.14f; // 4字节,6位精度
double d = 3.14159; // 8字节,15位精度
// 打印各种类型的大小
printf("char大小: %zu字节\n", sizeof(char));
printf("int大小: %zu字节\n", sizeof(int));
printf("float大小: %zu字节\n", sizeof(float));
printf("double大小: %zu字节\n", sizeof(double));
return 0;
}
2. 运算符和表达式
#include <stdio.h>
int main() {
// 算术运算符
int a = 10, b = 3;
printf("加法: %d\n", a + b);
printf("减法: %d\n", a - b);
printf("乘法: %d\n", a * b);
printf("除法: %d\n", a / b); // 整数除法
printf("取余: %d\n", a % b);
// 关系运算符
printf("a > b: %d\n", a > b); // 1(真)
printf("a == b: %d\n", a == b); // 0(假)
printf("a != b: %d\n", a != b); // 1(真)
// 逻辑运算符
int x = 1, y = 0;
printf("与: %d\n", x && y); // 0
printf("或: %d\n", x || y); // 1
printf("非: %d\n", !x); // 0
// 位运算符
printf("按位与: %d\n", 12 & 5); // 4
printf("按位或: %d\n", 12 | 5); // 13
printf("按位异或: %d\n", 12 ^ 5); // 9
printf("左移: %d\n", 12 << 1); // 24
printf("右移: %d\n", 12 >> 1); // 6
return 0;
}
3. 控制结构
#include <stdio.h>
int main() {
// if-else语句
int age = 20;
if (age >= 18) {
printf("成年人\n");
} else {
printf("未成年人\n");
}
// switch语句
char grade = 'B';
switch (grade) {
case 'A':
printf("优秀\n");
break;
case 'B':
printf("良好\n");
break;
case 'C':
printf("及格\n");
break;
default:
printf("不及格\n");
}
// while循环
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
printf("\n");
// do-while循环
i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
printf("\n");
// for循环
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
4. 数组
#include <stdio.h>
int main() {
// 一维数组
int numbers[5] = {1, 2, 3, 4, 5};
// 数组遍历
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// 二维数组
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 二维数组遍历
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// 字符数组(字符串)
char str[] = "Hello";
printf("%s\n", str);
return 0;
}
5. 指针
#include <stdio.h>
int main() {
// 基本指针操作
int x = 10;
int *ptr = &x; // 指针指向x的地址
printf("x的值: %d\n", x);
printf("x的地址: %p\n", (void*)&x);
printf("ptr存储的地址: %p\n", (void*)ptr);
printf("ptr指向的值: %d\n", *ptr);
// 通过指针修改值
*ptr = 20;
printf("修改后x的值: %d\n", x);
// 指针数组
int arr[] = {1, 2, 3, 4, 5};
int *p = arr; // 数组名就是首元素地址
// 使用指针遍历数组
for (int i = 0; i < 5; i++) {
printf("%d ", *(p + i)); // 等价于p[i]
}
printf("\n");
return 0;
}
6. 函数
#include <stdio.h>
// 函数声明
int add(int a, int b);
void swap(int *a, int *b);
int factorial(int n);
int main() {
// 基本函数调用
int result = add(5, 3);
printf("5 + 3 = %d\n", result);
// 通过指针传递参数
int x = 10, y = 20;
printf("交换前: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("交换后: x = %d, y = %d\n", x, y);
// 递归函数调用
int n = 5;
printf("%d的阶乘是: %d\n", n, factorial(n));
return 0;
}
// 函数定义
int add(int a, int b) {
return a + b;
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
7 文件操作
#include <stdio.h>
int main() {
// 写文件
FILE *fp = fopen("test.txt", "w");
if (fp == NULL) {
printf("文件打开失败\n");
return 1;
}
fprintf(fp, "Hello, World!\n");
fputs("这是第二行\n", fp);
fclose(fp);
// 读文件
char buffer[100];
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("文件打开失败\n");
return 1;
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}

浙公网安备 33010602011771号