c语言有趣代码大全
C语言是一种广泛使用的编程语言,有很多有趣和创造性的代码示例。
Hello World倒序输出:
c#include <stdio.h>
void reversePrint(char* str) {
if (*str) {
reversePrint(str + 1);
putchar(*str);
}
}
int main() {
reversePrint("dlroW olleH");
return 0;
}
打印ASCII表:
c#include <stdio.h>
int main() {
for (int i = 0; i <= 127; ++i) {
printf("%d: %c\n", i, i);
}
return 0;
}
FizzBuzz游戏:
c#include <stdio.h>
int main() {
for (int i = 1; i <= 100; ++i) {
if (i % 3 == 0 && i % 5 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}
return 0;
}
生命游戏:
c#include <stdio.h>
#include <unistd.h>
void display(int grid[10][10]) {
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (grid[i][j] == 1) {
printf("* ");
} else {
printf(". ");
}
}
printf("\n");
}
printf("\n");
usleep(100000);
}
void update(int grid[10][10]) {
int newGrid[10][10];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
int neighbors = 0;
for (int x = -1; x <= 1; ++x) {
for (int y = -1; y <= 1; ++y) {
if (!(x == 0 && y == 0) && i + x >= 0 && i + x < 10 && j + y >= 0 && j + y < 10) {
neighbors += grid[i + x][j + y];
}
}
}
newGrid[i][j] = (neighbors == 3 || (neighbors == 2 && grid[i][j] == 1)) ? 1 : 0;
}
}
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
grid[i][j] = newGrid[i][j];
}
}
}
int main() {
int grid[10][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
while (1) {
display(grid);
update(grid);
}
return 0;
}
随机密码生成器:
c#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generatePassword(int length) {
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
srand((unsigned int)time(NULL));
for (int i = 0; i < length; ++i) {
putchar(charset[rand() % (sizeof(charset) - 1)]);
}
printf("\n");
}
int main() {
generatePassword(12);
return 0;
}
颜色输出:
c#include <stdio.h>
// ANSI escape codes for text color
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
#define RESET "\x1B[0m"
int main() {
printf(RED "This text is red.\n" RESET);
printf(GREEN "This text is green.\n" RESET);
return 0;
}
文件复制:
c#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char ch;
sourceFile = fopen("source.txt", "r");
destinationFile = fopen("destination.txt", "w");
if (sourceFile == NULL || destinationFile == NULL) {
printf("Error opening file.\n");
return 1;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
printf("File copied successfully.\n");
fclose(sourceFile);
fclose(destinationFile);
return 0;
}
简单的HTTP服务器:
c#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
char response[] = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, World!";
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(8080);
bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSocket, 5);
printf("Simple HTTP Server Running on Port 8080...\n");
while (1) {
socklen_t clientLen = sizeof(clientAddr);
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientLen);
send(clientSocket, response, sizeof(response), 0);
close(clientSocket);
}
close(serverSocket);
return 0;
}