引言C语言作为一种高级编程语言,因其高效、灵活和接近硬件的特性,在操作系统、嵌入式系统、系统软件等领域有着广泛的应用。本笔记旨在帮助读者从入门到精通C语言编程,通过实战案例和详细解析,让读者掌握C语言...
C语言作为一种高级编程语言,因其高效、灵活和接近硬件的特性,在操作系统、嵌入式系统、系统软件等领域有着广泛的应用。本笔记旨在帮助读者从入门到精通C语言编程,通过实战案例和详细解析,让读者掌握C语言的核心概念和实践技能。
int a = 10;(int)3.14 或 3.14 (int);注意隐式转换和显式转换的区别。if、else if、elsefor、while、do...whilegoto、break、continuevoid myFunction() { ... }return语句#define PI 3.14159#ifdef、#ifndef、#else、#endifint *ptr;*ptr、ptr++ptr = &a;、*(ptr + i)malloc()、calloc()、realloc()free()struct Person { char name[50]; int age; };struct Person people[100];struct Person *ptr;union Data { int i; float f; };data.i、data.fFILE *fp = fopen("example.txt", "r");fgets(line, sizeof(line), fp);fprintf(fp, "Hello, World!\n");fclose(fp);#include
int main() { int a, b; char op; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); // 注意在%c前面加空格,以跳过前面的换行符 switch (op) { case '+': printf("%d + %d = %d\n", a, b, a + b); break; case '-': printf("%d - %d = %d\n", a, b, a - b); break; case '*': printf("%d * %d = %d\n", a, b, a * b); break; case '/': if (b != 0) printf("%d / %d = %f\n", a, b, (float)a / b); else printf("Division by zero is not allowed\n"); break; default: printf("Invalid operator!\n"); } return 0;
} #include
void bubbleSort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
}
int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: \n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0;
} struct Node { int data; struct Node* next;
};#include
#include
#define MAX 100
int stack[MAX];
int top = -1;
void push(int data) { if (top < MAX - 1) { stack[++top] = data; } else { printf("Stack overflow\n"); }
}
int pop() { if (top >= 0) { return stack[top--]; } else { printf("Stack underflow\n"); return -1; }
} 通过本笔记的学习,读者应该掌握了C语言编程的基本概念、常用语法、数据结构以及实战技能。随着技术的不断发展,C语言也在不断进化,例如C99、C11等新标准引入了许多新特性。未来的学习可以关注以下几个方面:
希望本笔记能对读者的C语言学习之路有所帮助。