📅  最后修改于: 2023-12-03 15:14:26.735000             🧑  作者: Mango
C语言是一种广泛使用的程序设计语言,常用于系统编程和应用程序开发。这里将为程序员介绍7套关于C语言的编程题目,旨在帮助提升编程技能和理解C语言的基本知识。
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf("Result: %d\n", result);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 3;
int y = 5;
printf("Before swap: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After swap: x=%d, y=%d\n", x, y);
return 0;
}
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person person;
strcpy(person.name, "John Doe");
person.age = 30;
printf("Name: %s, Age: %d\n", person.name, person.age);
return 0;
}
#include <stdio.h>
int main() {
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
fprintf(file, "Hello World!");
fclose(file);
printf("File written successfully.\n");
} else {
printf("Failed to open file.\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int *arr = (int *) malloc(size * sizeof(int));
// 使用 arr 进行操作
free(arr);
return 0;
}
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*func_ptr)(int, int);
func_ptr = add;
int result = func_ptr(3, 4);
printf("Result: %d\n", result);
func_ptr = subtract;
result = func_ptr(7, 2);
printf("Result: %d\n", result);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
以上是关于C语言的7套编程题目,涵盖了基本概念、控制流程、数组与字符串、函数与指针、结构体与文件操作、动态内存分配与指针,以及高级特性等方面的内容。这些题目有助于巩固和扩展你的C语言编程技能。