实例1:简单的C语言程序
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
这是一个非常基础的C语言程序,用于输出“Hello, World!”到控制台。这个例子展示了C语言的入门级语法和程序结构。
实例2:变量和数据类型
#include <stdio.h>
int main() {
int age = 18;
float pi = 3.14159;
char grade = 'A';
printf("Age: %d\n", age);
printf("Pi: %.2f\n", pi);
printf("Grade: %c\n", grade);
return 0;
}
在这个例子中,我们学习了如何声明和初始化变量,以及不同的数据类型(整数、浮点数、字符)。
实例3:运算符
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
这个例子介绍了C语言中的基本运算符,包括加、减、乘、除和取余。
实例4:控制流 - if语句
#include <stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is not greater than 5\n");
}
return 0;
}
这个例子展示了如何使用if语句来执行条件判断。
实例5:控制流 - switch语句
#include <stdio.h>
int main() {
int day = 3;
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Not a valid day\n");
}
return 0;
}
在这个例子中,我们使用了switch语句来处理多条件分支。
实例6:循环 - for循环
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Count: %d\n", i);
}
return 0;
}
这个例子展示了如何使用for循环来重复执行代码块。
实例7:循环 - while循环
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Count: %d\n", i);
i++;
}
return 0;
}
在这个例子中,我们使用了while循环来实现与for循环相同的功能。
实例8:函数
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
这个例子展示了如何定义和调用函数。
实例9:指针
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a);
printf("Value of ptr: %p\n", (void*)ptr);
printf("Value of *ptr: %d\n", *ptr);
return 0;
}
在这个例子中,我们学习了指针的概念,以及如何使用指针来访问变量的地址和值。
实例10:数组
#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;
}
这个例子展示了如何声明和初始化数组,以及如何遍历和访问数组元素。
实例11:字符串
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[] = "World";
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
strcat(str1, str2);
printf("Concatenated str1: %s\n", str1);
return 0;
}
在这个例子中,我们学习了如何声明和初始化字符串,以及如何使用字符串函数(如strcat)来操作字符串。
实例12:结构体
#include <stdio.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1;
strcpy(emp1.name, "John Doe");
emp1.age = 30;
emp1.salary = 50000.0;
printf("Name: %s\n", emp1.name);
printf("Age: %d\n", emp1.age);
printf("Salary: %.2f\n", emp1.salary);
return 0;
}
在这个例子中,我们学习了如何定义和使用结构体来组织相关数据。
实例13:联合体
#include <stdio.h>
typedef union {
int i;
float f;
char c;
} Data;
int main() {
Data data;
data.i = 10;
printf("Data as int: %d\n", data.i);
data.f = 3.14;
printf("Data as float: %.2f\n", data.f);
data.c = 'A';
printf("Data as char: %c\n", data.c);
return 0;
}
在这个例子中,我们学习了联合体的概念,它允许在同一个内存位置存储不同类型的数据。
实例14:枚举
#include <stdio.h>
typedef enum {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
int main() {
Weekday today = FRIDAY;
printf("Today is: %d\n", today);
return 0;
}
在这个例子中,我们学习了枚举的概念,它用于定义一组命名的整数值。
实例15:文件操作 - 写入文件
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
在这个例子中,我们学习了如何使用C语言进行文件操作,包括打开、写入和关闭文件。
实例16:文件操作 - 读取文件
#include <stdio.h>
int main() {
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;
}
在这个例子中,我们学习了如何从文件中读取内容。
实例17:动态内存分配
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array = (int*)malloc(5 * sizeof(int));
if (array == NULL) {
printf("Error allocating memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
array[i] = i * 2;
}
for (int i = 0; i < 5; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
free(array);
return 0;
}
在这个例子中,我们学习了如何使用malloc和free函数进行动态内存分配。
实例18:字符串函数 - strlen
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of str: %lu\n", strlen(str));
return 0;
}
在这个例子中,我们学习了如何使用strlen函数来获取字符串的长度。
实例19:字符串函数 - strcpy
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100];
strcpy(str2, str1);
printf("str2: %s\n", str2);
return 0;
}
在这个例子中,我们学习了如何使用strcpy函数来复制字符串。
实例20:字符串函数 - strcat
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[] = " World!";
strcat(str1, str2);
printf("str1: %s\n", str1);
return 0;
}
在这个例子中,我们学习了如何使用strcat函数来连接字符串。
实例21:字符串函数 - strcmp
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "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;
}
在这个例子中,我们学习了如何使用strcmp函数来比较字符串。
实例22:字符串函数 - strchr
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char ch = 'W';
char *pos = strchr(str, ch);
if (pos != NULL) {
printf("Character '%c' found at position %ld\n", ch, pos - str);
} else {
printf("Character '%c' not found\n", ch);
}
return 0;
}
在这个例子中,我们学习了如何使用strchr函数来查找字符串中特定字符的位置。
实例23:字符串函数 - strstr
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "World";
char *pos = strstr(str1, str2);
if (pos != NULL) {
printf("Substring '%s' found at position %ld\n", str2, pos - str1);
} else {
printf("Substring '%s' not found\n", str2);
}
return 0;
}
在这个例子中,我们学习了如何使用strstr函数来查找字符串中特定子字符串的位置。
实例24:字符串函数 - strtok
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *token;
char *delimiter = ",";
token = strtok(str, delimiter);
while (token != NULL) {
printf("Token: %s\n", token);
token = strtok(NULL, delimiter);
}
return 0;
}
在这个例子中,我们学习了如何使用strtok函数来分割字符串。
实例25:字符串函数 - strtol
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[] = "123";
char *endptr;
long value = strtol(str, &endptr, 10);
printf("Value: %ld\n", value);
printf("Remaining string: %s\n", endptr);
return 0;
}
在这个例子中,我们学习了如何使用strtol函数来将字符串转换为长整型数值。
实例26:字符串函数 - strtod
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[] = "123.45";
char *endptr;
double value = strtod(str, &endptr);
printf("Value: %f\n", value);
printf("Remaining string: %s\n", endptr);
return 0;
}
在这个例子中,我们学习了如何使用strtod函数来将字符串转换为双精度浮点数值。
实例27:字符串函数 - sprintf
#include <stdio.h>
#include <string.h>
int main() {
char buffer[100];
sprintf(buffer, "Hello, %s!", "World");
printf("Buffer: %s\n", buffer);
return 0;
}
在这个例子中,我们学习了如何使用sprintf函数来格式化字符串。
实例28:字符串函数 - sscanf
#include <stdio.h>
int main() {
char str[] = "123 45.67";
int num1;
double num2;
sscanf(str, "%d %lf", &num1, &num2);
printf("num1: %d\n", num1);
printf("num2: %f\n", num2);
return 0;
}
在这个例子中,我们学习了如何使用sscanf函数来解析字符串中的数据。
实例29:结构体函数 - memcpy
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1 = {"John Doe", 30, 50000.0};
Employee emp2;
memcpy(&emp2, &emp1, sizeof(Employee));
printf("emp2: %s %d %.2f\n", emp2.name, emp2.age, emp2.salary);
return 0;
}
在这个例子中,我们学习了如何使用memcpy函数来复制结构体。
实例30:结构体函数 - memset
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp;
memset(&emp, 0, sizeof(Employee));
printf("emp: %s %d %.2f\n", emp.name, emp.age, emp.salary);
return 0;
}
在这个例子中,我们学习了如何使用memset函数来将结构体的所有成员设置为默认值。
实例31:结构体函数 - memcmp
#include <stdio.h>
#include <string.h>
typedef struct {
int id;
char name[50];
} Student;
int main() {
Student stu1 = {1, "Alice"};
Student stu2 = {2, "Bob"};
int result = memcmp(&stu1, &stu2, sizeof(Student));
if (result == 0) {
printf("stu1 and stu2 are equal\n");
} else if (result < 0) {
printf("stu1 is less than stu2\n");
} else {
printf("stu1 is greater than stu2\n");
}
return 0;
}
在这个例子中,我们学习了如何使用memcmp函数来比较结构体。
实例32:结构体函数 - strcpy
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1 = {"John Doe", 30, 50000.0};
Employee emp2;
strcpy(emp2.name, emp1.name);
emp2.age = emp1.age;
emp2.salary = emp1.salary;
printf("emp2: %s %d %.2f\n", emp2.name, emp2.age, emp2.salary);
return 0;
}
在这个例子中,我们学习了如何使用strcpy函数来复制结构体中的字符串成员。
实例33:结构体函数 - strcat
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1 = {"John Doe", 30, 50000.0};
Employee emp2;
strcat(emp2.name, emp1.name);
emp2.age = emp1.age;
emp2.salary = emp1.salary;
printf("emp2: %s %d %.2f\n", emp2.name, emp2.age, emp2.salary);
return 0;
}
在这个例子中,我们学习了如何使用strcat函数来连接结构体中的字符串成员。
实例34:结构体函数 - strlen
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp = {"John Doe", 30, 50000.0};
printf("Length of emp.name: %lu\n", strlen(emp.name));
return 0;
}
在这个例子中,我们学习了如何使用strlen函数来获取结构体中字符串成员的长度。
实例35:结构体函数 - strcmp
”`c
#include
typedef struct {
char name[50];
int age;
float salary;
} Employee;
int main() {
Employee emp1 = {"John Doe", 30, 50000.0};
Employee emp2 = {"Jane Smith", 25
