C程式碼大全

以下是一些常見的C語言編程示例代碼:

1. 列印"Hello, World!":

```c

#include

int main() {

printf("Hello, World!\n");

return 0;

}

```

2. 計算兩個數的和:

```c

#include

int main() {

int num1, num2, sum;

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

sum = num1 + num2;

printf("Sum = %d\n", sum);

return 0;

}

```

3. 反轉字元串:

```c

#include

#include

int main() {

char str[100];

printf("Enter a string: ");

scanf("%s", str);

char *p = str;

while (*p != '\0') {

*p = *(p + strlen(p) - 1); // Reverse the string by swapping the last and first characters.

p--; // Move to the next character.

}

printf("Reversed string: %s\n", str);

return 0;

}

```

4. 計算斐波那契數列:

```c

#include

#include

int fibonacci(int n) {

if (n <= 1) {

return n; // Fibonacci sequence is 0 and 1 for first two numbers.

} else {

return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call to calculate the next number in the sequence.

}

}

int main() {

int n = 10; // Calculate the first 10 numbers in the sequence.

printf("Fibonacci sequence: ");

for (int i = 0; i < n; i++) { // Print the numbers in sequence.

printf("%d ", fibonacci(i));

}

printf("\n"); // Print a new line after the sequence is printed.

return 0;

}

```這只是C語言編程中的一小部分示例,實際上C語言有許多其他的用途和功能。如果你需要其他示例代碼,請提供更多具體的要求和上下文。

以上就是【C程式碼大全】的相關內容,敬請閱讀。