引言C语言作为一门历史悠久且广泛应用于系统编程、嵌入式开发等领域的编程语言,掌握其编程精髓对于广大程序员来说至关重要。本文将为您揭秘C语言项目实战秘籍,通过50个精选案例,帮助您轻松掌握编程精髓。1....
C语言作为一门历史悠久且广泛应用于系统编程、嵌入式开发等领域的编程语言,掌握其编程精髓对于广大程序员来说至关重要。本文将为您揭秘C语言项目实战秘籍,通过50个精选案例,帮助您轻松掌握编程精髓。
#include
int main() { int num = 10; float fnum = 3.14; char c = 'A'; printf("num = %d, fnum = %f, c = %c\n", num, fnum, c); return 0;
} #include
int main() { int a = 5, b = 3; printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); printf("a % b = %d\n", a % b); return 0;
} #include
int main() { int age = 18; if (age >= 18) { printf("You are an adult.\n"); } else { printf("You are not an adult.\n"); } return 0;
} #include
int main() { int i; for (i = 0; i < 5; i++) { printf("i = %d\n", i); } return 0;
} #include
void printHello() { printf("Hello, World!\n");
}
int main() { printHello(); return 0;
} #include
int main() { int a = 10; int *ptr = &a; printf("The value of a is %d\n", *ptr); *ptr = 20; printf("The new value of a is %d\n", *ptr); return 0;
} #include
int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("arr[0] = %d\n", arr[0]); arr[0] = 10; printf("arr[0] = %d\n", arr[0]); return 0;
} #include
#include
int main() { char str1[] = "Hello"; char str2[] = "World"; printf("str1 = %s\n", str1); printf("str2 = %s\n", str2); strcat(str1, str2); printf("str1 = %s\n", str1); return 0;
} #include
struct Student { char name[50]; int age; float score;
};
int main() { struct Student stu1; strcpy(stu1.name, "Alice"); stu1.age = 18; stu1.score = 90.5; printf("Name: %s, Age: %d, Score: %.1f\n", stu1.name, stu1.age, stu1.score); return 0;
} #include
union Data { int i; float f; char c;
};
int main() { union Data u; u.i = 10; printf("u.i = %d\n", u.i); u.f = 3.14; printf("u.f = %.2f\n", u.f); u.c = 'A'; printf("u.c = %c\n", u.c); return 0;
} #include
int main() { FILE *fp = fopen("example.txt", "w"); if (fp == NULL) { printf("Error opening file.\n"); return 1; } fprintf(fp, "Hello, World!\n"); fclose(fp); return 0;
} #include
int main() { FILE *fp = fopen("example.txt", "r"); if (fp == NULL) { printf("Error opening file.\n"); return 1; } char buffer[100]; while (fgets(buffer, sizeof(buffer), fp)) { printf("%s", buffer); } fclose(fp); return 0;
} #include
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; HWND hWnd; MSG Msg; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WindowProcedure; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "myWindowClass"; wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wcex)) { return 0; } hWnd = CreateWindowEx( 0, "myWindowClass", "Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
} #include
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); if (!CreateProcess(NULL, "notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { printf("Error creating process.\n"); return 0; } Sleep(5000); // Wait for 5 seconds TerminateProcess(pi.hProcess, 0); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0;
} #include
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HMODULE hModule; DWORD dwSize; char *pBuffer; hModule = GetModuleHandle(NULL); dwSize = GetModuleInfo(hModule, SIZEOF_IMAGE_DOS_HEADER, NULL); pBuffer = (char *)VirtualAlloc(NULL, dwSize, MEM_COMMIT, PAGE_READWRITE); if (pBuffer == NULL) { printf("Error allocating memory.\n"); return 0; } memcpy(pBuffer, GetModuleInfo(hModule, SIZEOF_IMAGE_DOS_HEADER, NULL), dwSize); Sleep(5000); // Wait for 5 seconds VirtualFree(pBuffer, 0, MEM_RELEASE); return 0;
} #include
#include
typedef struct Node { int data; struct Node *next;
} Node;
Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); if (newNode == NULL) { printf("Error allocating memory.\n"); return NULL; } newNode->data = data; newNode->next = NULL; return newNode;
}
void insertNode(Node **head, int data) { Node *newNode = createNode(data); if (newNode == NULL) { return; } if (*head == NULL) { *head = newNode; } else { Node *temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; }
}
void deleteNode(Node **head, int data) { if (*head == NULL) { return; } if ((*head)->data == data) { Node *temp = *head; *head = (*head)->next; free(temp); return; } Node *temp = *head; while (temp->next != NULL && temp->next->data != data) { temp = temp->next; } if (temp->next != NULL) { Node *toDelete = temp->next; temp->next = toDelete->next; free(toDelete); }
}
void printList(Node *head) { Node *temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n");
}
int main() { Node *head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); printList(head); deleteNode(&head, 2); printList(head); return 0;
} #include
void bubbleSort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
}
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"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0;
} #include
#include
#include
#include
#include
#include
int main() { int sockfd; struct sockaddr_in servaddr; sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8080); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); char buffer[] = "Hello, server!"; send(sockfd, buffer, strlen(buffer), 0); close(sockfd); return 0;
} #include
#include
#include
#include
#include
#include
int main() { int sockfd; struct sockaddr_in servaddr; char buffer[1024]; int n; sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8080); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); n = read(sockfd, buffer, sizeof(buffer)); buffer[n] = '\0'; printf("Received message: %s\n", buffer); close(sockfd); return 0;
} 本文通过50个精选案例,为您揭秘了C语言项目实战秘籍。希望这些案例能够帮助您轻松掌握C语言编程精髓,为您的编程之路打下坚实的基础。在实际开发过程中,不断实践和总结,相信您会成为一名优秀的程序员。