实战案例一:打印“Hello, World!”
在C语言编程中,第一个案例通常是打印“Hello, World!”。这个简单的程序可以帮助你熟悉C语言的基本语法和编译过程。
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
实战案例二:变量和赋值
变量是编程中的基本概念,了解如何声明和赋值变量对于编写程序至关重要。
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
printf("The sum of a and b is: %d\n", a + b);
return 0;
}
实战案例三:数据类型
C语言支持多种数据类型,包括整型、浮点型、字符型等。
#include <stdio.h>
int main() {
int i = 10;
float f = 3.14;
char c = 'A';
printf("Integer: %d\n", i);
printf("Float: %f\n", f);
printf("Character: %c\n", c);
return 0;
}
实战案例四:运算符
C语言提供了丰富的运算符,包括算术运算符、关系运算符、逻辑运算符等。
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("The result of a + b is: %d\n", a + b);
printf("The result of a - b is: %d\n", a - b);
printf("The result of a * b is: %d\n", a * b);
printf("The result of a / b is: %d\n", a / b);
printf("The result of a % b is: %d\n", a % b);
return 0;
}
实战案例五:控制语句
控制语句用于控制程序的执行流程,包括条件语句和循环语句。
#include <stdio.h>
int main() {
int i = 10;
if (i > 5) {
printf("i is greater than 5\n");
} else {
printf("i is not greater than 5\n");
}
for (i = 0; i < 10; i++) {
printf("Loop iteration: %d\n", i);
}
return 0;
}
实战案例六:函数
函数是C语言的核心概念,了解如何定义和调用函数对于编写大型程序至关重要。
#include <stdio.h>
void printMessage() {
printf("This is a function.\n");
}
int main() {
printMessage();
return 0;
}
实战案例七:指针
指针是C语言中的高级概念,它允许你直接访问和操作内存地址。
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("The value of a is: %d\n", a);
printf("The address of a is: %p\n", (void *)ptr);
printf("The value of *ptr is: %d\n", *ptr);
return 0;
}
实战案例八:结构体
结构体是用于组织相关数据的复合数据类型。
#include <stdio.h>
typedef struct {
int id;
char name[50];
float salary;
} Employee;
int main() {
Employee emp = {1, "John Doe", 5000.0};
printf("Employee ID: %d\n", emp.id);
printf("Employee Name: %s\n", emp.name);
printf("Employee Salary: %.2f\n", emp.salary);
return 0;
}
实战案例九:数组
数组是用于存储一系列相同类型数据的容器。
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, arr[i]);
}
return 0;
}
实战案例十:字符串处理
C语言提供了丰富的字符串处理函数,如strlen、strcpy等。
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
printf("Length of str1: %d\n", strlen(str1));
strcpy(str1, str2);
printf("str1 after copying: %s\n", str1);
return 0;
}
实战案例十一:文件操作
文件操作是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;
}
实战案例十二:动态内存分配
动态内存分配允许你在运行时分配和释放内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Error allocating memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, arr[i]);
}
free(arr);
return 0;
}
实战案例十三:函数指针
函数指针允许你将函数作为参数传递。
#include <stdio.h>
void printMessage() {
printf("This is a function.\n");
}
int main() {
void (*funcPtr)() = printMessage;
funcPtr();
return 0;
}
实战案例十四:结构体指针
结构体指针允许你通过指针访问结构体成员。
#include <stdio.h>
typedef struct {
int id;
char name[50];
float salary;
} Employee;
int main() {
Employee emp = {1, "John Doe", 5000.0};
Employee *ptr = &emp;
printf("Employee ID: %d\n", ptr->id);
printf("Employee Name: %s\n", ptr->name);
printf("Employee Salary: %.2f\n", ptr->salary);
return 0;
}
实战案例十五:链表
链表是一种常见的数据结构,用于存储一系列元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(Node **head, int data) {
Node *newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
printList(head);
return 0;
}
实战案例十六:树
树是一种非线性数据结构,用于存储具有层次关系的数据。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insertNode(Node **root, int data) {
if (*root == NULL) {
*root = createNode(data);
} else {
Node *current = *root;
while (current != NULL) {
if (data < current->data) {
if (current->left == NULL) {
current->left = createNode(data);
break;
} else {
current = current->left;
}
} else {
if (current->right == NULL) {
current->right = createNode(data);
break;
} else {
current = current->right;
}
}
}
}
}
void inorderTraversal(Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
Node *root = NULL;
insertNode(&root, 5);
insertNode(&root, 3);
insertNode(&root, 7);
insertNode(&root, 2);
insertNode(&root, 4);
insertNode(&root, 6);
insertNode(&root, 8);
inorderTraversal(root);
return 0;
}
实战案例十七:排序算法
排序算法是计算机科学中的基本概念,了解常见的排序算法对于编写高效程序至关重要。
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
实战案例十八:查找算法
查找算法是计算机科学中的基本概念,了解常见的查找算法对于编写高效程序至关重要。
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) {
return m;
} else if (arr[m] < x) {
l = m + 1;
} else {
r = m - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
printf("Element is present at index %d\n", linearSearch(arr, n, x));
printf("Element is present at index %d\n", binarySearch(arr, 0, n - 1, x));
return 0;
}
实战案例十九:递归
递归是一种编程技巧,用于解决具有重复结构的问题。
#include <stdio.h>
int factorial(int n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n = 5;
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
实战案例二十:动态规划
动态规划是一种编程技巧,用于解决具有重叠子问题的问题。
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n = 10;
printf("Fibonacci of %d is %d\n", n, fibonacci(n));
return 0;
}
实战案例二十一:网络编程
网络编程是C语言编程中的重要应用,了解如何使用套接字进行网络通信至关重要。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
exit(EXIT_FAILURE);
}
char buffer[1024] = {0};
read(new_socket, buffer, 1024);
printf("%s\n", buffer);
send(new_socket, "Hello from server", 18, 0);
close(new_socket);
return 0;
}
实战案例二十二:图形编程
图形编程是C语言编程中的重要应用,了解如何使用图形库进行图形绘制至关重要。
#include <stdio.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, -0.5);
glVertex2f(-0.5, -0.5);
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Triangle");
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
实战案例二十三:多线程编程
多线程编程是C语言编程中的重要应用,了解如何使用线程进行并发编程至关重要。
#include <stdio.h>
#include <pthread.h>
void *threadFunction(void *arg) {
int *num = (int *)arg;
printf("Thread ID: %ld, Value: %d\n", pthread_self(), *num);
return NULL;
}
int main() {
pthread_t thread1, thread2;
int num = 10;
if (pthread_create(&thread1, NULL, threadFunction, &num) != 0) {
perror("Failed to create thread");
return 1;
}
if (pthread_create(&thread2, NULL, threadFunction, &num) != 0) {
perror("Failed to create thread");
return 1;
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
实战案例二十四:网络编程(TCP)
网络编程是C语言编程中的重要应用,了解如何使用TCP进行网络通信至关重要。
”`c
#include
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen
