【C 程序设计】第 1 章:C 语言简介与思维导图
发布人:shili8
发布时间:2025-03-11 17:15
阅读次数:0
**C 程序设计**
================### 第1 章:C语言简介与思维导图####1.1 C语言的历史背景C语言是由丹尼斯·里奇(Dennis Ritchie)在1972 年开发的。它最初是为了实现 Unix 操作系统而设计的。C语言的主要特点是其简洁性、效率和灵活性。
####1.2 C语言的特点* **低级别语言**: C语言是一种低级别语言,它提供了直接访问计算机硬件资源的能力。
* **高性能**: C语言编译后的程序通常比其他语言的同类程序更快。
* **灵活性**: C语言允许程序员自由地使用内存和操作系统功能。
####1.3 C语言的应用场景* **系统编程**: C语言是 Unix 操作系统的主要开发语言之一。
* **嵌入式系统**: C语言常用于开发嵌入式系统,如微控制器、单板计算机等。
* **游戏开发**: C语言曾经是游戏开发的主流语言之一。
### 思维导图
markdownC语言简介 ├── 历史背景 │ ├── 丹尼斯·里奇(Dennis Ritchie) │ └── Unix 操作系统 ├── 特点 │ ├──低级别语言 │ ├── 高性能 │ └── 灵活性 ├── 应用场景 │ ├── 系统编程 │ ├── 嵌入式系统 │ └── 游戏开发
###代码示例####1.4 Hello World!
这是一个简单的 C 程序,打印出 "Hello, World!"。
c#include <stdio.h>
int main() {
printf("Hello, World!
");
return0;
}
####1.5 变量和数据类型C语言支持各种基本数据类型,如整数、浮点数、字符等。下面是一个例子。
c#include <stdio.h>
int main() {
int a =10; // 整数 float b =3.14f; // 浮点数 char c = 'A'; // 字符 printf("a = %d
", a);
printf("b = %f
", b);
printf("c = %c
", c);
return0;
}
####1.6 条件语句和循环C语言支持各种条件语句和循环,如 if、else、switch、for、while 等。下面是一个例子。
c#include <stdio.h>
int main() {
int a =10;
if (a >5) {
printf("a 大于5
");
} else {
printf("a 小于或等于5
");
}
switch (a %3) {
case0:
printf("余数为0
");
break;
case1:
printf("余数为1
");
break;
default:
printf("余数为2
");
break;
}
for (int i =0; i < 5; i++) {
printf("%d ", i);
}
printf("
");
while (a >0) {
printf("%d ", a--);
}
printf("
");
return0;
}
####1.7 函数C语言支持函数的定义和调用。下面是一个例子。
c#include <stdio.h>
void printHello() {
printf("Hello!
");
}
int main() {
printHello();
int a = add(2,3);
printf("%d
", a);
return0;
}
int add(int x, int y) {
return x + y;
}
####1.8 数组和指针C语言支持数组和指针的定义和操作。下面是一个例子。
c#include <stdio.h>
void printArray(int arr[5]) {
for (int i =0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("
");
}
int main() {
int arr[5] = {1,2,3,4,5};
printArray(arr);
int *ptr = &arr[0];
printf("%d
", *ptr);
printf("%d
", *(ptr +1));
printf("%d
", *(ptr +2));
return0;
}
####1.9 结构体C语言支持结构体的定义和操作。下面是一个例子。
c#include <stdio.h>
typedef struct {
int x;
float y;
} Point;
void printPoint(Point p) {
printf("x = %d
", p.x);
printf("y = %f
", p.y);
}
int main() {
Point p1 = {1,2.5};
printPoint(p1);
return0;
}
####1.10 unionsC语言支持联合体的定义和操作。下面是一个例子。
c#include <stdio.h>
typedef union {
int i;
float f;
} Union;
void printUnion(Union u) {
printf("i = %d
", u.i);
printf("f = %f
", u.f);
}
int main() {
Union u1;
u1.i =10;
printUnion(u1);
return0;
}
####1.11 枚举C语言支持枚举的定义和操作。下面是一个例子。
c#include <stdio.h>
typedef enum {
RED,
GREEN,
BLUE} Color;
void printColor(Color c) {
switch (c) {
case RED:
printf("RED
");
break;
case GREEN:
printf("GREEN
");
break;
default:
printf("BLUE
");
break;
}
}
int main() {
Color c1 = RED;
printColor(c1);
return0;
}
####1.12 位操作C语言支持位操作的定义和操作。下面是一个例子。
c#include <stdio.h>
void printBits(int n) {
for (int i =31; i >=0; i--) {
printf("%d", (n >> i) &1);
}
printf("
");
}
int main() {
int n =10;
printBits(n);
return0;
}
####1.13 位移C语言支持位移的定义和操作。下面是一个例子。
c#include <stdio.h>
void printShift(int n) {
printf("%d
", n << 2);
printf("%d
", n >>2);
}
int main() {
int n =10;
printShift(n);
return0;
}
####1.14 位与C语言支持位与的定义和操作。下面是一个例子。
c#include <stdio.h>
void printBitAnd(int a, int b) {
printf("%d
", a & b);
}
int main() {
int a =10;
int b =5;
printBitAnd(a, b);
return0;
}
####1.15 位或C语言支持位或的定义和操作。下面是一个例子。
c#include <stdio.h>
void printBitOr(int a, int b) {
printf("%d
", a | b);
}
int main() {
int a =10;
int b =5;
printBitOr(a, b);
return0;
}
####1.16 位异或C语言支持位异或的定义和操作。下面是一个例子。
c#include <stdio.h>
void printBitXor(int a, int b) {
printf("%d
", a ^ b);
}
int main() {
int a =10;
int b =5;
printBitXor(a, b);
return0;
}
####1.17 位非C语言支持位非的定义和操作。下面是一个例子。
c#include <stdio.h>
void printBitNot(int n) {
printf("%d
", ~n);
}
int main() {
int n =10;
printBitNot(n);
return0;
}
####1.18 位左移C语言支持位左移的定义和操作。下面是一个例子。
c#include <stdio.h>
void printLeftShift(int n) {
printf("%d

