基础语法示例
```c
include
int main() {
printf("Hello, World!\n");
return 0;
}
```
基本数据类型示例
```c
include
int main() {
int num = 10;
float pi = 3.14;
char letter = 'A';
printf("num = %d\n", num);
printf("pi = %.2f\n", pi);
printf("letter = %c\n", letter);
return 0;
}
```
控制流语句示例
```c
include
int main() {
int score = 85;
if (score >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
printf("\n");
int j = 0;
while (j < 3) {
printf("%d ", j);
j++;
}
printf("\n");
return 0;
}
```
乘法表
```c
include
int main() {
int i, j;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= 9; j++) {
printf("%d * %d = %d\t", i, j, i * j);
}
printf("\n");
}
return 0;
}
```
4x4数组逆时针旋转90度
```c
include
void rotate(int arr) {
int temp;
for (int x = 0; x < 4; x++) {
for (int y = x; y < 4; y++) {
temp[y][4 - x - 1] = arr[x][y];
}
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
arr[x][y] = temp[x][y];
}
}
}
int main() {
int arr = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
rotate(arr);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
printf("%d ", arr[x][y]);
}
printf("\n");
}
return 0;
}
```
判断101-200之间的素数
```c
include include bool is_prime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } int main() { int count = 0; for (int i = 101; i <= 200; i++) { if (is_prime(i)) { printf("%d ", i); count++; } } printf("\nTotal: %d\n", count); return 0; } ``` 这些示例涵盖了C语言的基础知识,包括基本语法、数据类型、控制流语句、数组操作和函数编写等。希望这些示例对你有所帮助。