实例1:打印“Hello, World!”
C语言编程的第一步通常是学会如何打印一段文字。下面是一个简单的例子:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
这段代码展示了C语言中最基本的输出操作。
实例2:变量和数据类型
变量是编程中的基础概念。以下是如何在C语言中声明和使用变量的示例:
#include <stdio.h>
int main() {
int a = 10; // 整型变量
float b = 3.14; // 浮点型变量
char c = 'A'; // 字符型变量
printf("a = %d, b = %f, c = %c\n", a, b, c);
return 0;
}
实例3:算术运算
C语言支持基本的算术运算,如下所示:
#include <stdio.h>
int main() {
int num1 = 5, num2 = 3;
printf("Sum: %d\n", num1 + num2);
printf("Difference: %d\n", num1 - num2);
printf("Product: %d\n", num1 * num2);
printf("Quotient: %d\n", num1 / num2);
printf("Modulus: %d\n", num1 % num2);
return 0;
}
实例4:控制结构
控制结构用于改变程序的执行流程。以下是一个简单的if语句示例:
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
实例5:循环结构
循环用于重复执行代码块。以下是一个while循环的例子:
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}
实例6:数组操作
数组是存储多个同类型变量的集合。以下是如何声明和使用数组的示例:
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
实例7:函数定义
函数是代码的模块化部分。以下是一个简单的函数示例:
#include <stdio.h>
void sayHello() {
printf("Hello!\n");
}
int main() {
sayHello();
return 0;
}
实例8:结构体
结构体允许您创建包含不同类型成员的复合数据类型。以下是一个结构体示例:
#include <stdio.h>
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
struct Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.5;
printf("Name: %s, Age: %d, GPA: %.2f\n", student1.name, student1.age, student1.gpa);
return 0;
}
实例9:指针和地址
指针是存储变量地址的变量。以下是如何使用指针的示例:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("num = %d, *ptr = %d, ptr = %p\n", num, *ptr, (void *)ptr);
return 0;
}
实例10:函数指针
函数指针是指向函数的指针。以下是如何使用函数指针的示例:
#include <stdio.h>
void printHello() {
printf("Hello!\n");
}
int main() {
void (*funcPtr)() = printHello;
funcPtr();
return 0;
}
实例11:动态内存分配
动态内存分配允许您在运行时分配内存。以下是如何使用malloc和free的示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}
实例12:字符串处理
C语言中字符串处理非常重要。以下是如何使用字符串函数的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char *str3 = strcat(str1, str2);
printf("Concatenated string: %s\n", str3);
return 0;
}
实例13:文件操作
文件操作是C语言中的另一个重要部分。以下是如何读取和写入文件的示例:
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(file, "This is a test file.\n");
fclose(file);
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
实例14:输入输出
以下是如何使用scanf和printf进行输入输出的示例:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
实例15:宏定义
宏定义是C语言中的一种预处理器指令。以下是如何定义和使用宏的示例:
#include <stdio.h>
#define PI 3.14159
int main() {
printf("The value of PI is: %.5f\n", PI);
return 0;
}
实例16:条件运算符
条件运算符是C语言中的一种表达式运算符。以下是如何使用条件运算符的示例:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("The maximum of %d and %d is %d\n", a, b, max);
return 0;
}
实例17:位运算
位运算用于操作二进制位。以下是如何使用位运算的示例:
#include <stdio.h>
int main() {
int num1 = 5; // 101
int num2 = 3; // 011
printf("num1 & num2 = %d\n", num1 & num2); // 001 -> 1
printf("num1 | num2 = %d\n", num1 | num2); // 111 -> 7
printf("num1 ^ num2 = %d\n", num1 ^ num2); // 110 -> 6
printf("~num1 = %d\n", ~num1); // 010 -> -6
printf("num1 << 1 = %d\n", num1 << 1); // 1010 -> 10
printf("num1 >> 1 = %d\n", num1 >> 1); // 010 -> 2
return 0;
}
实例18:结构体数组和指针
以下是如何使用结构体数组和指针的示例:
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person people[2] = {
{"John Doe", 25},
{"Jane Smith", 30}
};
struct Person *ptr = people;
for (int i = 0; i < 2; i++) {
printf("Name: %s, Age: %d\n", ptr[i].name, ptr[i].age);
}
return 0;
}
实例19:动态数组
以下是如何使用动态数组来存储和访问数据的示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array = (int *)malloc(5 * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
array[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
printf("\n");
free(array);
return 0;
}
实例20:字符串复制
以下是如何使用strcpy函数复制字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char src[50] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("src: %s, dest: %s\n", src, dest);
return 0;
}
实例21:字符串连接
以下是如何使用strcat函数连接两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[50] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
实例22:字符串比较
以下是如何使用strcmp函数比较两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
实例23:字符串查找
以下是如何使用strstr函数查找子字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, World!";
char str2[50] = "World";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at: %ld\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
实例24:字符串替换
以下是如何使用strreplace函数替换字符串中的子字符串的示例:
#include <stdio.h>
#include <string.h>
void strreplace(char *str, const char *from, const char *to) {
char *p = str, *q;
while ((q = strstr(p, from)) != NULL) {
while (*q) *p++ = *q++;
*p++ = *to;
}
}
int main() {
char str[100] = "Hello, World! World is great.";
strreplace(str, "World", "Universe");
printf("Modified string: %s\n", str);
return 0;
}
实例25:字符串反转
以下是如何使用strrev函数反转字符串的示例:
#include <stdio.h>
#include <string.h>
void strrev(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}
}
int main() {
char str[100] = "Hello, World!";
strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
实例26:字符串转换
以下是如何使用atoi函数将字符串转换为整数的示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[50] = "12345";
int num = atoi(str);
printf("Converted integer: %d\n", num);
return 0;
}
实例27:字符串连接
以下是如何使用strcat函数连接两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[50] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
实例28:字符串比较
以下是如何使用strcmp函数比较两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
实例29:字符串查找
以下是如何使用strstr函数查找子字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, World!";
char str2[50] = "World";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at: %ld\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
实例30:字符串替换
以下是如何使用strreplace函数替换字符串中的子字符串的示例:
#include <stdio.h>
#include <string.h>
void strreplace(char *str, const char *from, const char *to) {
char *p = str, *q;
while ((q = strstr(p, from)) != NULL) {
while (*q) *p++ = *q++;
*p++ = *to;
}
}
int main() {
char str[100] = "Hello, World! World is great.";
strreplace(str, "World", "Universe");
printf("Modified string: %s\n", str);
return 0;
}
实例31:字符串反转
以下是如何使用strrev函数反转字符串的示例:
#include <stdio.h>
#include <string.h>
void strrev(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}
}
int main() {
char str[100] = "Hello, World!";
strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
实例32:字符串转换
以下是如何使用atoi函数将字符串转换为整数的示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[50] = "12345";
int num = atoi(str);
printf("Converted integer: %d\n", num);
return 0;
}
实例33:字符串连接
以下是如何使用strcat函数连接两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[50] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
实例34:字符串比较
以下是如何使用strcmp函数比较两个字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
实例35:字符串查找
以下是如何使用strstr函数查找子字符串的示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, World!";
char str2[50] = "World";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at: %ld\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
实例36:字符串替换
以下是如何使用strreplace函数替换字符串中的子字符串的示例:
”`c
#include
void strreplace(char *str, const char *from, const char *to) {
char *p = str, *q;
while ((q = strstr(p, from)) != NULL) {
while (*q) *p++ = *q++;
*p++ = *to;
}
}
int main()
