首頁 >後端開發 >C++ >C語言從0開始

C語言從0開始

Patricia Arquette
Patricia Arquette原創
2025-01-21 10:03:12216瀏覽

C language from 0

踏上你的 C 程式設計之旅! 雖然最初令人畏懼,但透過正確的方法掌握 C 語言的基礎知識是可以實現的。本指南提供了結構化的介紹,從基本概念到更高級的主題。

目錄

  1. C 基礎知識與資料型態
  2. 使用者輸入與輸出
  3. 條件語句(包含捷徑)
  4. Switch 語句
  5. 陣列:一維與二維
  6. 巢狀循環
  7. 函數:結構與用法
  8. 結構 (structs)
  9. 指針

C 基礎知識與資料型態

C 程式遵循標準結構並利用各種資料類型作為變數。 一個簡單的例子:

<code class="language-c">#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}</code>

關鍵概念:

  • 資料型態:
    • int:整數(例如,int x = 10;
    • floatdouble:浮點數(小數)(例如 float pi = 3.14;
    • char:單一字元或 ASCII 代碼(例如 char letter = 'A';
    • bool:布林值(真/假)(需要 <stdbool.h>
<code class="language-c">// Data Type Examples:
int a = 40;         // Integer (4 bytes)
short int b = 32767; // Short Integer (2 bytes)
unsigned int c = 4294967295; // Unsigned Integer (4 bytes)
float d = 9.81;     // Float (4 bytes)
double e = 3.14159; // Double (8 bytes)
bool f = true;      // Boolean (1 byte)
char g = 'e';       // Character (1 byte)
char h = 100;      // Character (1 byte)
char name[] = "Example"; // String (array of characters)

// Variable declaration and initialization
int age;      // Declaration
age = 5;      // Initialization
char letter = 'C'; // Declaration and initialization

// Displaying variables
printf("You are %d years old\n", age); // Integer
printf("Hello %s\n", name);           // String
printf("Learning %c\n", letter);      // Character

// Format specifiers: %d (int), %s (string), %c (char), %f (float), %.2f (float to 2 decimal places)</code>
  • 運算子: -*/%(模)、 (遞增)、--(遞減)。 請記住類型轉換以獲得準確的結果(例如,float z = 5 / (float)2;)。

使用者輸入與輸出

對於 VS Code 中的使用者輸入,請使用「終端」標籤。

<code class="language-c">int age;
char name[25];

// Integer Input
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old\n", age);

// String Input (using `fgets` for safer input)
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); // fgets handles spaces
name[strcspn(name, "\n")] = 0; // Remove trailing newline from fgets
printf("Hello, %s!\n", name);</code>

C 中區分大小寫很重要。使用 toupper() 中的 <ctype.h> 等函數進行不區分大小寫的比較。


條件快速鍵(三元運算子)

三元運算子提供了一個簡潔的方式來寫if-else語句:

<code class="language-c">int max = (a > b) ? a : b; // Equivalent to an if-else statement</code>

Switch 語句

高效處理多種情況:

<code class="language-c">char grade = 'A';

switch (grade) {
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good!\n");
        break;
    default:
        printf("Try again!\n");
}</code>

總是包含一個default案例。


陣列

陣列儲存相同類型變數的集合:

<code class="language-c">int numbers[5] = {10, 20, 30, 40, 50};
printf("%d\n", numbers[0]); // Accesses the first element (10)

// 2D Array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

// Array of Strings
char cars[][10] = {"BMW", "Tesla", "Toyota"};</code>

巢狀循環

循環中的循環,對於處理多維資料很有用:(為簡潔起見,省略範例,但使用巢狀 for 循環可以輕鬆建構)。


功能

函數促進程式碼可重複使用性:

<code class="language-c">void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

int main() {
    greet("Alice");
    return 0;
}</code>

結構 (structs)

將相關變數分組:

<code class="language-c">struct Player {
    char name[50];
    int score;
};

struct Player player1 = {"Bob", 150};
printf("Name: %s, Score: %d\n", player1.name, player1.score);</code>

指針

儲存記憶體位址的變數:

<code class="language-c">#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}</code>

指針對於動態記憶體分配至關重要。 本指南提供了堅實的基礎。 堅持練習是掌握 C 程式設計的關鍵。

以上是C語言從0開始的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn