C プログラミングの旅に出かけましょう! 最初は気が遠くなりますが、C の基本をマスターすることは、適切なアプローチをとれば達成可能です。このガイドでは、基本的な概念からより高度なトピックまで、体系的に説明します。
目次
structs
)C の基本とデータ型
C プログラムは標準構造に従い、変数にさまざまなデータ型を利用します。 簡単な例:
<code class="language-c">#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }</code>
主要な概念:
int
: 整数 (例: int x = 10;
)float
および double
: 浮動小数点数 (10 進数) (例: float pi = 3.14;
)char
: 単一の文字または ASCII コード (例: char letter = 'A';
)bool
: ブール値 (true/false) (<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 プログラミングをマスターするには、一貫した練習が鍵となります。
以上が0からのC言語の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。