一、基礎知識
1、C語言數據類型
C語言數據類型分為基本數據類型和派生數據類型,基本數據類型包括int、float、double、char等,派生數據類型包括數組、結構體、共用體和指針。
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
double c = 2.718281828;
char d = 'a';
int arr[5] = {1, 2, 3, 4, 5};
struct Student {
char name[20];
int age;
} stu;
union Test {
int a;
float b;
char c[10];
} test;
int *p = &a;
printf("%d\n", a);
printf("%.2f\n", b);
printf("%.9lf\n", c);
printf("%c\n", d);
printf("%d\n", arr[0]);
printf("%d\n", sizeof(stu));
printf("%d\n", sizeof(test));
printf("%d\n", *p);
return 0;
}
2、變數和常量
變數是程序運行過程中值可以改變的量,常量是值不可改變的量。在C語言中,常量可以使用#define或const關鍵字來定義。
#include <stdio.h>
#define MAX_NUM 100
const float PI = 3.1415926;
int main() {
int num = 10;
float radius = 5.0;
float area = PI * radius * radius;
printf("%d\n", MAX_NUM);
printf("%.2f\n", area);
printf("%d\n", num);
return 0;
}
3、運算符
C語言支持多種運算符,包括算術運算符、關係運算符、邏輯運算符、位運算符和賦值運算符等。
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d\n", a + b); // 13
printf("%d\n", a - b); // 7
printf("%d\n", a * b); // 30
printf("%d\n", a / b); // 3
printf("%d\n", a % b); // 1
printf("%d\n", a b); // 1
printf("%d\n", a == b); // 0
printf("%d\n", a && b); // 1
printf("%d\n", a | b); // 11
printf("%d\n", a <<= 1); // 20
return 0;
}
二、指針和數組
1、指針
指針是用來存儲地址的變數,其本身也有一個地址。指針變數需要通過&運算符獲取地址,通過*運算符獲取地址所存儲的值。
#include <stdio.h>
int main() {
int a = 10;
int *p;
p = &a;
printf("%d\n", *p);
printf("%p\n", p);
printf("%p\n", &p);
return 0;
}
2、數組
數組是用來存儲一組相同類型的數據的結構,使用[]運算符可以訪問數組元素。數組的下標從0開始。
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i, sum = 0;
for (i = 0; i < 5; i++) {
sum += arr[i];
}
printf("%d\n", sum);
return 0;
}
三、函數和指針
1、函數
函數是一段完成特定任務的代碼塊,函數可以接收參數,也可以返回值。
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 20);
printf("%d\n", result);
return 0;
}
2、指針
指針可以作為函數的參數和返回值,可以用來實現動態內存分配。
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
四、結構體和共用體
1、結構體
結構體是由多個不同類型的數據組成的自定義類型,可以使用.運算符訪問結構體成員。
#include <stdio.h>
struct Student {
char name[20];
int age;
float score;
};
int main() {
struct Student stu = {"John", 18, 95.5};
printf("name: %s\n", stu.name);
printf("age: %d\n", stu.age);
printf("score: %.1f\n", stu.score);
return 0;
}
2、共用體
共用體是一種特殊的數據類型,內部的所有成員共用同一塊內存空間,只能存儲其中的一個成員。
#include <stdio.h>
union Test {
int a;
float b;
char c[10];
};
int main() {
union Test test;
test.a = 123;
printf("%d\n", test.a);
printf("%.2f\n", test.b);
test.b = 3.14;
printf("%d\n", test.a);
printf("%.2f\n", test.b);
test.c[0] = 'h';
test.c[1] = 'i';
test.c[2] = '\0';
printf("%s\n", test.c);
return 0;
}
五、內存管理
1、動態內存分配
動態內存分配是指在程序運行時根據需要動態地為變數分配內存空間,可以使用malloc、calloc和realloc函數來分配和釋放內存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int *)malloc(5 * sizeof(int));
int i;
for (i = 0; i < 5; i++) {
p[i] = i + 1;
}
for (i = 0; i < 5; i++) {
printf("%d ", p[i]);
}
printf("\n");
free(p);
return 0;
}
2、內存管理函數
可以使用memset和memcpy函數對內存進行管理,其中memset可以將內存中的某一區域賦為相同的值,memcpy可以將一個內存區域的值複製到另一個內存區域。
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "hello";
char dest[20];
memset(dest, 0, sizeof(dest));
memcpy(dest, str, sizeof(str));
printf("%s\n", dest);
return 0;
}
原創文章,作者:LCPMQ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/371666.html