From e3e527e4d0dca4564c728308f5f73aa1a9481ab5 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Wed, 26 Nov 2025 16:43:40 +0900 Subject: [PATCH 01/20] =?UTF-8?q?=E6=97=A5=E6=9C=AC=E8=AA=9E=E7=89=88?= =?UTF-8?q?=E3=81=AE=E4=BD=9C=E6=88=90=E9=96=8B=E5=A7=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Arrays and Pointers.md | 235 ++++++++++ tutorials/learn-c.org/ja/Arrays.md | 91 ++++ tutorials/learn-c.org/ja/Binary trees.md | 211 +++++++++ tutorials/learn-c.org/ja/Bitmasks.md | 184 ++++++++ tutorials/learn-c.org/ja/Conditions.md | 151 +++++++ .../learn-c.org/ja/Dynamic allocation.md | 93 ++++ tutorials/learn-c.org/ja/For loops.md | 77 ++++ tutorials/learn-c.org/ja/Function Pointers.md | 161 +++++++ .../ja/Function arguments by reference.md | 117 +++++ tutorials/learn-c.org/ja/Functions.md | 112 +++++ tutorials/learn-c.org/ja/Hello, World!.md | 98 +++++ tutorials/learn-c.org/ja/Linked lists.md | 401 ++++++++++++++++++ .../learn-c.org/ja/Multidimensional Arrays.md | 179 ++++++++ .../learn-c.org/ja/Pointer Arithmetics.md | 181 ++++++++ tutorials/learn-c.org/ja/Pointers.md | 116 +++++ tutorials/learn-c.org/ja/Recursion.md | 89 ++++ tutorials/learn-c.org/ja/Static.md | 95 +++++ tutorials/learn-c.org/ja/Strings.md | 127 ++++++ tutorials/learn-c.org/ja/Structures.md | 103 +++++ tutorials/learn-c.org/ja/Unions.md | 156 +++++++ .../learn-c.org/ja/Variables and Types.md | 105 +++++ tutorials/learn-c.org/ja/Welcome.md | 42 ++ tutorials/learn-c.org/ja/While loops.md | 110 +++++ 23 files changed, 3234 insertions(+) create mode 100644 tutorials/learn-c.org/ja/Arrays and Pointers.md create mode 100644 tutorials/learn-c.org/ja/Arrays.md create mode 100644 tutorials/learn-c.org/ja/Binary trees.md create mode 100644 tutorials/learn-c.org/ja/Bitmasks.md create mode 100644 tutorials/learn-c.org/ja/Conditions.md create mode 100644 tutorials/learn-c.org/ja/Dynamic allocation.md create mode 100644 tutorials/learn-c.org/ja/For loops.md create mode 100644 tutorials/learn-c.org/ja/Function Pointers.md create mode 100644 tutorials/learn-c.org/ja/Function arguments by reference.md create mode 100644 tutorials/learn-c.org/ja/Functions.md create mode 100644 tutorials/learn-c.org/ja/Hello, World!.md create mode 100644 tutorials/learn-c.org/ja/Linked lists.md create mode 100644 tutorials/learn-c.org/ja/Multidimensional Arrays.md create mode 100644 tutorials/learn-c.org/ja/Pointer Arithmetics.md create mode 100644 tutorials/learn-c.org/ja/Pointers.md create mode 100644 tutorials/learn-c.org/ja/Recursion.md create mode 100644 tutorials/learn-c.org/ja/Static.md create mode 100644 tutorials/learn-c.org/ja/Strings.md create mode 100644 tutorials/learn-c.org/ja/Structures.md create mode 100644 tutorials/learn-c.org/ja/Unions.md create mode 100644 tutorials/learn-c.org/ja/Variables and Types.md create mode 100644 tutorials/learn-c.org/ja/Welcome.md create mode 100644 tutorials/learn-c.org/ja/While loops.md diff --git a/tutorials/learn-c.org/ja/Arrays and Pointers.md b/tutorials/learn-c.org/ja/Arrays and Pointers.md new file mode 100644 index 000000000..213989ed4 --- /dev/null +++ b/tutorials/learn-c.org/ja/Arrays and Pointers.md @@ -0,0 +1,235 @@ +Tutorial +-------- + +In a previous tutorial on [[Pointers]], you learned that a pointer to a given data type can store the address of any variable of that particular data type. For example, in the following code, the pointer variable `pc` stores the address of the character variable `c`. + + char c = 'A'; + char *pc = &c; + +Here, `c` is a scalar variable that can store only a single value. However, you are already familiar with arrays that can hold multiple values of the same data type in a contiguously allocated memory block. So, you might wonder, can we have pointers to arrays too? Indeed, we can. + +Let us start with an example code and look at its output. We will discuss its behavior subsequently. + + char vowels[] = {'A', 'E', 'I', 'O', 'U'}; + char *pvowels = vowels; + int i; + + // Print the addresses + for (i = 0; i < 5; i++) { + printf("&vowels[%d]: %p, pvowels + %d: %p, vowels + %d: %p\n", i, &vowels[i], i, pvowels + i, i, vowels + i); + } + + // Print the values + for (i = 0; i < 5; i++) { + printf("vowels[%d]: %c, *(pvowels + %d): %c, *(vowels + %d): %c\n", i, vowels[i], i, *(pvowels + i), i, *(vowels + i)); + } + +A typical output of the above code is shown below. + +>&vowels[0]: 0x7ffee146da17, pvowels + 0: 0x7ffee146da17, vowels + 0: 0x7ffee146da17 +> +>&vowels[1]: 0x7ffee146da18, pvowels + 1: 0x7ffee146da18, vowels + 1: 0x7ffee146da18 +> +>&vowels[2]: 0x7ffee146da19, pvowels + 2: 0x7ffee146da19, vowels + 2: 0x7ffee146da19 +> +>&vowels[3]: 0x7ffee146da1a, pvowels + 3: 0x7ffee146da1a, vowels + 3: 0x7ffee146da1a +> +>&vowels[4]: 0x7ffee146da1b, pvowels + 4: 0x7ffee146da1b, vowels + 4: 0x7ffee146da1b +> +>vowels[0]: A, \*(pvowels + 0): A, *(vowels + 0): A +> +>vowels[1]: E, \*(pvowels + 1): E, *(vowels + 1): E +> +>vowels[2]: I, \*(pvowels + 2): I, *(vowels + 2): I +> +>vowels[3]: O, \*(pvowels + 3): O, *(vowels + 3): O +> +>vowels[4]: U, \*(pvowels + 4): U, *(vowels + 4): U + + + +As you rightly guessed, `&vowels[i]` gives the memory location of the *i*th element of the array `vowels`. Moreover, since this is a character array, each element occupies one byte so that the consecutive memory addresses are separated by a single byte. We also created a pointer, `pvowels`, and assigned the address of the array `vowels` to it. `pvowels + i` is a valid operation; although in general, this may not always be meaningful (explored further in [[Pointer Arithmetics]] ). In particular, the output shown above indicates that `&vowels[i]` and `pvowels + i` are equivalent. Feel free to alter the data types of the array and pointer variables to test this out. + +If you look carefully at the previous code, you will notice that we also used another apparently surprising notation: `vowels + i`. Moreover, `pvowels + i` and `vowels + i` returns the same thing — address of the *i*th element of the array `vowels`. On the other hand, `*(pvowels + i)` and `*(vowels + i)` both return the *i*th element of the array `vowels`. Why is that so? + +This is because the name of an array itself is a (constant) pointer to the first element of the array. In other words, the notations `vowels`, `&vowels[0]`, and `vowels + 0` all point to the same location. + + +Dynamic Memory Allocation for Arrays +------------------------------------ + +By now we know that we can traverse an array using pointers. Moreover, we also know that we can dynamically allocate (contiguous) memory using blocks pointers. These two aspects can be combined to dynamically allocate memory for an array. This is illustrated in the following code. + + // Allocate memory to store five characters + int n = 5; + char *pvowels = (char *) malloc(n * sizeof(char)); + int i; + + pvowels[0] = 'A'; + pvowels[1] = 'E'; + *(pvowels + 2) = 'I'; + pvowels[3] = 'O'; + *(pvowels + 4) = 'U'; + + for (i = 0; i < n; i++) { + printf("%c ", pvowels[i]); + } + + printf("\n"); + + free(pvowels); + +In the above code, we allocated five contiguous bytes of memory to store five characters. Subsequently, we used array notations to traverse the blocks of memory as if `pvowels` is an array. However, remember that `pvowels` actually is a pointer. Pointers and arrays, in general, are not the same thing. + +So when is this useful? Remember that while declaring an array, the number of elements that it would contain must be known beforehand. Therefore, in some scenarios it might happen that the space allocated for an array is either less than the desired space or more. However, by using dynamic memory allocation, one can allocate just as much memory as required by a program. Moreover, unused memory can be freed as soon as it is no longer required by invoking the `free()` function. On the down side, with dynamic memory allocation, one must responsibly call `free()` wherever relevant. Otherwise, memory leaks would occur. + +We conclude this tutorial by looking at dynamic memory allocation for a two-dimensional array. This can be generalized to *n*-dimensions in a similar way. Unlike one-dimensional arrays, where we used a pointer, in this case we require a pointer to a pointer, as shown below. + + int nrows = 2; + int ncols = 5; + int i, j; + + // Allocate memory for nrows pointers + char **pvowels = (char **) malloc(nrows * sizeof(char *)); + + // For each row, allocate memory for ncols elements + pvowels[0] = (char *) malloc(ncols * sizeof(char)); + pvowels[1] = (char *) malloc(ncols * sizeof(char)); + + pvowels[0][0] = 'A'; + pvowels[0][1] = 'E'; + pvowels[0][2] = 'I'; + pvowels[0][3] = 'O'; + pvowels[0][4] = 'U'; + + pvowels[1][0] = 'a'; + pvowels[1][1] = 'e'; + pvowels[1][2] = 'i'; + pvowels[1][3] = 'o'; + pvowels[1][4] = 'u'; + + for (i = 0; i < nrows; i++) { + for(j = 0; j < ncols; j++) { + printf("%c ", pvowels[i][j]); + } + + printf("\n"); + } + + // Free individual rows + free(pvowels[0]); + free(pvowels[1]); + + // Free the top-level pointer + free(pvowels); + + +Exercise +-------- + +The first seven rows of [Pascal's triangle](http://mathworld.wolfram.com/PascalsTriangle.html) are shown below. Note that row *i* contains *i* elements. Therefore, to store the numbers from the first three rows, one would require 1 + 2 + 3 = 6 memory slots. + +>1 +> +>1 1 +> +>1 2 1 +> +>1 3 3 1 +> +>1 4 6 4 1 +> +>1 5 10 10 5 1 +> +>1 6 15 20 15 6 1 + +Complete the skeleton code given below to store the numbers from the first three rows of Pascal's triangle in a two-dimensional "array" using dynamic memory allocation. Note that you must allocate exactly six memory slots to store those six numbers. No extra memory should be allocated. At the end of your program, free all the memory blocks used in this program. + + +Tutorial Code +------------- + + #include + #include + + int main() { + int i, j; + /* TODO: define the 2D pointer variable here */ + + /* TODO: complete the following line to allocate memory for holding three rows */ + pnumbers = (int **) malloc(); + + /* TODO: allocate memory for storing the individual elements in a row */ + pnumbers[0] = (int *) malloc(1 * sizeof(int)); + + pnumbers[0][0] = 1; + pnumbers[1][0] = 1; + pnumbers[1][1] = 1; + pnumbers[2][0] = 1; + pnumbers[2][1] = 2; + pnumbers[2][2] = 1; + + for (i = 0; i < 3; i++) { + for (j = 0; j <= i; j++) { + printf("%d", pnumbers[i][j]); + } + printf("\n"); + } + + for (i = 0; i < 3; i++) { + /* TODO: free memory allocated for each row */ + } + + /* TODO: free the top-level pointer */ + + return 0; + } + +Expected Output +--------------- + + 1 + 11 + 121 + +Solution +-------- + + #include + #include + + int main() { + int i, j; + /* TODO: define the 2D pointer variable here */ + int **pnumbers; + + /* TODO: Complete the following line to allocate memory for holding three rows */ + pnumbers = (int **) malloc(3 *sizeof(int *)); + + /* TODO: Allocate memory for storing the individual elements in a row */ + pnumbers[0] = (int *) malloc(1 * sizeof(int)); + pnumbers[1] = (int *) malloc(2 * sizeof(int)); + pnumbers[2] = (int *) malloc(3 * sizeof(int)); + + pnumbers[0][0] = 1; + pnumbers[1][0] = 1; + pnumbers[1][1] = 1; + pnumbers[2][0] = 1; + pnumbers[2][1] = 2; + pnumbers[2][2] = 1; + + for (i = 0; i < 3; i++) { + for (j = 0; j <= i; j++) { + printf("%d", pnumbers[i][j]); + } + printf("\n"); + } + + for (i = 0; i < 3; i++) { + free(pnumbers[i]); + } + + free(pnumbers); + + return 0; + } diff --git a/tutorials/learn-c.org/ja/Arrays.md b/tutorials/learn-c.org/ja/Arrays.md new file mode 100644 index 000000000..519f801e1 --- /dev/null +++ b/tutorials/learn-c.org/ja/Arrays.md @@ -0,0 +1,91 @@ +Tutorial +-------- + +Arrays are special variables which can hold more than one value under the same variable name, organised with an index. Arrays are defined using a very +straightforward syntax: + +配列は、同じ変数名で複数の値をインデックスで整理して保持できる特殊な変数です。配列は非常に簡単な構文で定義されます。 + + /* defines an array of 10 integers */ + int numbers[10]; + +Accessing a number from the array is done using the same syntax. Notice that arrays in C are zero-based, which means that if we +defined an array of size 10, then the array cells 0 through 9 (inclusive) are defined. `numbers[10]` is not an actual value. + +配列から数値にアクセスする場合も同じ構文を使用します。C言語の配列は0から始まります。つまり、サイズが10の配列を定義した場合、配列のセル0から9(両端を含む)が定義されます。`numbers[10]` は有効な値を持ちません。 + + int numbers[10]; + + /* populate the array */ + numbers[0] = 10; + numbers[1] = 20; + numbers[2] = 30; + numbers[3] = 40; + numbers[4] = 50; + numbers[5] = 60; + numbers[6] = 70; + + /* print the 7th number from the array, which has an index of 6 */ + printf("The 7th number in the array is %d", numbers[6]); + +Arrays can only have one type of variable, because they are implemented as a sequence of values in the computer's memory. +Because of that, accessing a specific array cell is very efficient. + +配列はコンピュータのメモリ内で、『値が等間隔に整列したもの』として実装されるため、1種類の変数しか持つことができません。 +そのため、特定の配列セルへのアクセスは非常に効率的です。 + +Exercise +-------- + +* The code below does not compile, because the `grades` variable is missing. +* One of the grades is missing. Can you define it so the grade average will be 85? + +* 以下のコードは `grades` 変数が欠落しているためコンパイルできません。 +* 成績の1つが欠落しています。成績平均が85になるように定義できますか? + +Tutorial Code +------------- + + #include + + int main() { + /* TODO: define the grades variable here */ + int average; + + grades[0] = 80; + /* TODO: define the missing grade + so that the average will sum to 85. */ + grades[2] = 90; + + average = (grades[0] + grades[1] + grades[2]) / 3; + printf("The average of the 3 grades is: %d", average); + + return 0; + } + +Expected Output +--------------- + + The average of the 3 grades is: 85 + +Solution +-------- + + #include + + int main() { + /* TODO: define the grades variable here */ + int grades[3]; + int average; + + grades[0] = 80; + /* TODO: define the missing grade + so that the average will sum to 85. */ + grades[1] = 85; + grades[2] = 90; + + average = (grades[0] + grades[1] + grades[2]) / 3; + printf("The average of the 3 grades is: %d", average); + + return 0; + } diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md new file mode 100644 index 000000000..4a5bd5aef --- /dev/null +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -0,0 +1,211 @@ +Tutorial +-------- + +### Introduction + +A Binary Tree is a type of data structure in which each node has at most two children (left child and right child). Binary trees are used to implement binary search trees and binary heaps, and are used for efficient searching and sorting. A binary tree is a special case of a K-ary tree, where k is 2. Common operations for binary trees include insertion, deletion, and traversal. The difficulty of performing these operations varies if the tree is balanced and also whether the nodes are leaf nodes or branch nodes. For **balanced trees** the depth of the left and right subtrees of every node differ by 1 or less. This allows for a predictable **depth** also known as **height**. This is the measure of a node from root to leaf, where root is 0 and sebsequent nodes are (1,2..n). This can be expressed by the integer part of log2(n) where n is the number of nodes in the tree. + + g s 9 + / \ / \ / \ + b m f u 5 13 + / \ / \ / \ + c d t y 11 15 + +The operations performed on trees requires searching in one of two main ways: Depth First Search and Breadth-first search. **Depth-first search (DFS)** is an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking. There are three types of depth first search traversal: **pre-order** visit, left, right, **in-order** left, visit, right, **post-order** left, right, visit. **Breadth-first search (BFS)** is an algorithm for traversing or searching tree or graph structures. In level-order, where we visit every node on a level before going to a lower level.
+ + +Exercise +-------- + +Below is an implementation of a binary tree that has insertion and printing capabilities. This tree is ordered but not balanced. This example maintains its ordering at insertion time. + +Change the print routine to depth-first search **pre-order**. + + +Tutorial Code +------------- + + #include + #include + + typedef struct node + { + int val; + struct node * left; + struct node * right; + } node_t; + + void insert(node_t * tree,int val); + void print_tree(node_t * current); + void printDFS(node_t * current); + + int main() + { + node_t * test_list = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + test_list->val = 0; + test_list->left = NULL; + test_list->right = NULL; + + insert(test_list,5); + insert(test_list,8); + insert(test_list,4); + insert(test_list,3); + + printDFS(test_list); + printf("\n"); + } + + void insert(node_t * tree, int val) + { + if (tree->val == 0) + { + /* insert on current (empty) position */ + tree->val = val; + } + else + { + if (val < tree->val) + { + /* insert left */ + if (tree->left != NULL) + { + insert(tree->left, val); + } + else + { + tree->left = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + tree->left->val = val; + tree->left->left = NULL; + tree->left->right = NULL; + } + } + else + { + if (val >= tree->val) + { + /* insert right */ + if (tree->right != NULL) + { + insert(tree->right,val); + } + else + { + tree->right = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + tree->right->val = val; + tree->right->left = NULL; + tree->right->right = NULL; + } + } + } + } + } + + /* depth-first search */ + void printDFS(node_t * current) + { + /* change the code here */ + if (current == NULL) return; /* security measure */ + if (current->left != NULL) printDFS(current->left); + if (current != NULL) printf("%d ", current->val); + if (current->right != NULL) printDFS(current->right); + } + + +Expected Output +--------------- + + 5 4 3 8 + +Solution +-------- + + #include + #include + + typedef struct node + { + int val; + struct node * left; + struct node * right; + } node_t; + + void insert(node_t * tree,int val); + void print_tree(node_t * current); + void printDFS(node_t * current); + + int main() + { + node_t * test_list = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + test_list->val = 0; + test_list->left = NULL; + test_list->right = NULL; + + insert(test_list,5); + insert(test_list,8); + insert(test_list,4); + insert(test_list,3); + + printDFS(test_list); + printf("\n"); + } + + void insert(node_t * tree, int val) + { + if (tree->val == 0) + { + /* insert on current (empty) position */ + tree->val = val; + } + else + { + if (val < tree->val) + { + /* insert left */ + if (tree->left != NULL) + { + insert(tree->left, val); + } + else + { + tree->left = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + tree->left->val = val; + tree->left->left = NULL; + tree->left->right = NULL; + } + } + else + { + if (val >= tree->val) + { + /* insert right */ + if (tree->right != NULL) + { + insert(tree->right,val); + } + else + { + tree->right = (node_t *) malloc(sizeof(node_t)); + /* set values explicitly, alternative would be calloc() */ + tree->right->val = val; + tree->right->left = NULL; + tree->right->right = NULL; + } + } + } + } + } + + /* depth-first search */ + void printDFS(node_t * current) + { + /* change the code here */ + if (current == NULL) return; /* security measure */ + if (current != NULL) printf("%d ", current->val); + if (current->left != NULL) printDFS(current->left); + if (current->right != NULL) printDFS(current->right); + } diff --git a/tutorials/learn-c.org/ja/Bitmasks.md b/tutorials/learn-c.org/ja/Bitmasks.md new file mode 100644 index 000000000..0eafb9cd3 --- /dev/null +++ b/tutorials/learn-c.org/ja/Bitmasks.md @@ -0,0 +1,184 @@ +Tutorial +-------- + +Bit masking is simply the process of storing data truly as bits, as opposed to storing it as chars/ints/floats. It is incredibly useful for storing certain types of data compactly and efficiently. + +The idea for bit masking is based on boolean logic. For those not familiar, boolean logic is the manipulation of 'true' (1) and 'false' (0) through logical operations (that take 0s and 1s as their argument). We are concerned with the following operations: + +* NOT a - the final value is the opposite of the input value (1 -> 0, 0 -> 1) +* a AND b - if both values are 1, the final value is 1, otherwise the final value is 0 +* a OR b - if either value is 1, the final value is 1, otherwise the final value is 0 +* a XOR b - if one value is 1 and the other value is 0, the final value is 1, otherwise the final value is 0 + +In computing, one of these true/false values is a *bit*. Primitives in C (`int`, `float`, etc) are made up of some number of bits, where that number is a multiple of 8. For example, an `int` may be at least 16 bits in size, where a `char` may be 8 bits. 8 bits is typically referred to as a *byte*. C guarantees that certain primitives are [at least some number](http://en.wikipedia.org/wiki/C_data_types#Basic_types) of bytes in size. The introduction of `stdint.h` in C11 allows the programmer to specify integer types that are exactly some number of bytes, which is extremely useful when using masks. + +Bit masks are often used when setting flags. Flags are values that can be in two states, such as 'on/off' and 'moving/stationary'. + +### Setting bit n + +Setting bit `n` is as simple as ORing the value of the storage variable with the value `2^n`. + +``` +storage |= 1 << n; +``` + +As an example, here is the setting of bit 3 where `storage` is a char (8 bits): + +``` +01000010 + OR +00001000 + == +01001010 +``` + +The `2^n` logic places the '1' value at the proper bit in the mask itself, allowing access to that same bit in the storage variable. + +### Clearing bit n + +Clearing bit `n` is the result of ANDing the value of the storage variable with the inverse (NOT) of the value `2^n`: + +``` +storage &= ~(1 << n); +``` + +Here's the example again: + +``` +01001010 + AND +11110111 + == +01000010 +``` + +### Flipping bit n + +Flipping bit `n` is the result of XORing the value of the storage variable with `2^n`: + +``` +storage ^= 1 << n; +``` + +``` +01000010 01001010 + XOR XOR +00001000 00001000 + == == +01001010 01000010 +``` + +### Checking bit n + +Checking a bit is ANDing the value of `2^n` with the bit storage: + +``` +bit = storage & (1 << n); +``` + +``` +01000010 01001010 + AND AND +00001000 00001000 + == == +00000000 00001000 +``` + +Exercise +-------- + +Use bit masks to manipulate some flags. + + +Tutorial Code +------------- + #include + #include + + /* Finish initializing the flags */ + + const short FLAG_ON = 1 << 0; // 1 (0x01) + const short FLAG_MOVEMENT = 1 << 1; // 2 (0x02) + const short FLAG_TRANSPARENT = 1 << 2; // 4 (0x04) + const short FLAG_ALIVE = ; + const short FLAG_BROKEN = ; + const short FLAG_EDIBLE = 1 << 5; // 32 (0x20) + + int main() { + short attributes = 0; + + /* Set the attributes ON, TRANSPARENT, and BROKEN */ + + assert(attributes == (FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN)); + + /* Modify (set/clear/toggle) so the only attributes are ON and ALIVE */ + + assert(attributes == (FLAG_ON | FLAG_ALIVE)); + + /* Check if the ALIVE flag is set */ + assert(/* ??? */); + + /* Check if the BROKEN flag is not set */ + assert(/* ??? */); + + /* Modify so only the EDIBLE attribute is set */ + + assert(attributes == FLAG_EDIBLE); + + printf("Done!"); + } + + +Expected Output +--------------- + Done! + +Solution +-------- + #include + #include + + /* Finish initializing the flags */ + + const short FLAG_ON = 1 << 0; // 1 (0x01) + const short FLAG_MOVEMENT = 1 << 1; // 2 (0x02) + const short FLAG_TRANSPARENT = 1 << 2; // 4 (0x04) + const short FLAG_ALIVE = 1 << 3; // 8 (0x08) + const short FLAG_BROKEN = 1 << 4; // 16 (0x10) + const short FLAG_EDIBLE = 1 << 5; // 32 (0x20) + + int main() { + short attributes = 0; + + /* Set the attributes ON, TRANSPARENT, and BROKEN */ + attributes |= FLAG_ON; + attributes |= FLAG_TRANSPARENT; + attributes |= FLAG_BROKEN; + // possible solution(s): + // attributes |= FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN; + // attributes = FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN; + + assert(attributes == (FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN)); + + /* Modify (set/clear/toggle) so the only attributes are ON and ALIVE */ + attributes &= ~FLAG_TRANSPARENT; + // possible: attributes ^= FLAG_TRANSPARENT; + attributes ^= FLAG_BROKEN; + // possible: attributes &= ~FLAG_BROKEN; + attributes |= FLAG_ALIVE; + + assert(attributes == (FLAG_ON | FLAG_ALIVE)); + + /* Check if the ALIVE flag is set */ + assert(attributes & FLAG_ALIVE); + + /* Check if the BROKEN flag is not set */ + assert(!(attributes & FLAG_BROKEN)); + + /* Modify so only the EDIBLE attribute is set */ + attributes = FLAG_EDIBLE; + + assert(attributes == FLAG_EDIBLE); + + printf("Done!"); + } diff --git a/tutorials/learn-c.org/ja/Conditions.md b/tutorials/learn-c.org/ja/Conditions.md new file mode 100644 index 000000000..a587d4d5d --- /dev/null +++ b/tutorials/learn-c.org/ja/Conditions.md @@ -0,0 +1,151 @@ +Tutorial +-------- + +### Decision Making + +In life, we all have to make decisions. In order to make a decision we weigh out our options and so do our programs. + +Here is the general form of the decision making structures found in C. + + + int target = 10; + if (target == 10) { + printf("Target is equal to 10"); + } + + +### The `if` statement + +The `if` statement allows us to check if an expression is `true` or `false`, and execute different code according to the result. + +To evaluate whether two variables are equal, the `==` operator is used, just like in the first example. + +Inequality operators can also be used to evaluate expressions. For example: + + int foo = 1; + int bar = 2; + + if (foo < bar) { + printf("foo is smaller than bar."); + } + + if (foo > bar) { + printf("foo is greater than bar."); + } + +We can use the `else` keyword to execute code when our expression evaluates to `false`. + + int foo = 1; + int bar = 2; + + if (foo < bar) { + printf("foo is smaller than bar."); + } else { + printf("foo is greater than bar."); + } + +Sometimes we will have more than two outcomes to choose from. In these cases, we can "chain" multiple `if` `else` statements together. + + int foo = 1; + int bar = 2; + + if (foo < bar) { + printf("foo is smaller than bar."); + } else if (foo == bar) { + printf("foo is equal to bar."); + } else { + printf("foo is greater than bar."); + } + +You can also nest `if` `else` statements if you like. + + int peanuts_eaten = 22; + int peanuts_in_jar = 100; + int max_peanut_limit = 50; + + if (peanuts_in_jar > 80) { + if (peanuts_eaten < max_peanut_limit) { + printf("Take as many peanuts as you want!\n"); + } + } else { + if (peanuts_eaten > peanuts_in_jar) { + printf("You can't have anymore peanuts!\n"); + } + else { + printf("Alright, just one more peanut.\n"); + } + } + + +Two or more expressions can be evaluated together using logical operators to check if two expressions evaluate to `true` together, or at least one of them. To check if two expressions both evaluate to `true`, use the AND operator `&&`. To check if at least one of the expressions evaluate to `true`, use the OR operator `||`. + + int foo = 1; + int bar = 2; + int moo = 3; + + if (foo < bar && moo > bar) { + printf("foo is smaller than bar AND moo is larger than bar."); + } + + if (foo < bar || moo > bar) { + printf("foo is smaller than bar OR moo is larger than bar."); + } + +The NOT operator `!` can also be used likewise: + + int target = 9; + if (target != 10) { + printf("Target is not equal to 10"); + } + + +Exercise +-------- + +In this exercise, you must construct an `if` statement inside the `guessNumber` function statement that checks if the number `guess` is equal to 555. If that is the case, the function must print out using `printf` "Correct. You guessed it!". If `guess` is less than 555, the function must print out using `printf` "Your guess is too low." If `guess` is greater than 555, the function must print out using `printf` "Your guess is too high." + +* **Important**: Don't forget to add a newline character `\n` at the end of the printf string. + +Tutorial Code +------------- + +#include + +void guessNumber(int guess) { + // TODO: write your code here +} + +int main() { + guessNumber(500); + guessNumber(600); + guessNumber(555); +} + +Expected Output +--------------- + +Your guess is too low. +Your guess is too high. +Correct. You guessed it! + +Solution +-------- + +#include + +void guessNumber(int guess) { + // TODO: write your code here + if (guess < 555) { + printf("Your guess is too low.\n"); + } else if (guess > 555) { + printf("Your guess is too high.\n"); + } else { + printf("Correct. You guessed it!\n"); + } +} + +int main() { + guessNumber(500); + guessNumber(600); + guessNumber(555); +} diff --git a/tutorials/learn-c.org/ja/Dynamic allocation.md b/tutorials/learn-c.org/ja/Dynamic allocation.md new file mode 100644 index 000000000..443f28903 --- /dev/null +++ b/tutorials/learn-c.org/ja/Dynamic allocation.md @@ -0,0 +1,93 @@ +Tutorial +-------- + +Dynamic allocation of memory is a very important subject in C. It allows building complex data structures such as linked lists. Allocating memory dynamically helps us to store data without initially knowing the size of the data in the time we wrote the program. + +To allocate a chunk of memory dynamically, we need to have a pointer ready to store the location of the newly allocated memory. We can access memory that was allocated to us using that same pointer, and we can use that pointer to free the memory again, once we have finished using it. + +Let's assume we want to dynamically allocate a person structure. The person is defined like this: + + typedef struct { + char * name; + int age; + } person; + +To allocate a new person in the `myperson` argument, we use the following syntax: + + person * myperson = (person *) malloc(sizeof(person)); + +This tells the compiler that we want to dynamically allocate just enough to hold a person struct in memory and then return a pointer of type `person` to the newly allocated data. The memory allocation function `malloc()` reserves the specified memory space. In this case, this is the size of `person` in bytes. + + The reason we write `(person *)` before the call to `malloc()` is that `malloc()` returns a "void pointer," which is a pointer that doesn't have a type. Writing `(person *)` in front of it is called *typecasting*, and changes the type of the pointer returned from `malloc()` to be `person`. However, it isn't strictly necessary to write it like this as C will implicitly convert the type of the returned pointer to that of the pointer it is assigned to (in this case, `myperson`) if you don't typecast it. + +Note that `sizeof` is not an actual function, because the compiler interprets it and translates it to the actual memory size of the person struct. + +To access the person's members, we can use the `->` notation: + + myperson->name = "John"; + myperson->age = 27; + +After we are done using the dynamically allocated struct, we can release it using `free`: + + free(myperson); + +Note that the free does not delete the `myperson` variable itself, it simply releases the data that it points to. The `myperson` variable will still point to somewhere in the memory - but after calling `free(myperson)` we are not allowed to access that area anymore. We must not use that pointer again until we allocate new data using it. + +Exercise +-------- + +Use `malloc` to dynamically allocate a point structure. + +Tutorial Code +------------- + + #include + #include + + typedef struct { + int x; + int y; + } point; + + int main() { + point * mypoint = NULL; + + /* Dynamically allocate a new point + struct which mypoint points to here */ + + mypoint->x = 10; + mypoint->y =5 ; + printf("mypoint coordinates: %d, %d\n", mypoint->x, mypoint->y); + + free(mypoint); + return 0; + } + +Expected Output +--------------- + + mypoint coordinates: 10, 5 + +Solution +-------- + + #include + #include + + typedef struct { + int x; + int y; + } point; + + int main() { + point * mypoint; + + mypoint = (point *)malloc(sizeof(point)); + + mypoint->x = 10; + mypoint->y =5 ; + printf("mypoint coordinates: %d, %d\n", mypoint->x, mypoint->y); + + free(mypoint); + return 0; + } diff --git a/tutorials/learn-c.org/ja/For loops.md b/tutorials/learn-c.org/ja/For loops.md new file mode 100644 index 000000000..cbb8eaebe --- /dev/null +++ b/tutorials/learn-c.org/ja/For loops.md @@ -0,0 +1,77 @@ +Tutorial +-------- + +For loops in C are straightforward. They supply the ability to create a loop - a code block that runs multiple times. +For loops require an iterator variable, usually notated as `i`. + +For loops give the following functionality: + +* Initialize the iterator variable using an initial value +* Check if the iterator has reached its final value +* Increases the iterator + +For example, if we wish to iterate on a block for 10 times, we write: + + int i; + for (i = 0; i < 10; i++) { + printf("%d\n", i); + } + +This block will print the numbers 0 through 9 (10 numbers in total). + +For loops can iterate on array values. For example, if we would want to sum all the values of an array, we would use +the iterator `i` as the array index: + + int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int sum = 0; + int i; + + for (i = 0; i < 10; i++) { + sum += array[i]; + } + + /* sum now contains a[0] + a[1] + ... + a[9] */ + printf("Sum of the array is %d\n", sum); + +Exercise +-------- + +Calculate the factorial (multiplication of all items `array[0]` to `array[9]`, inclusive), of the variable `array`. + +Tutorial Code +------------- + + #include + + int main() { + int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int factorial = 1; + int i; + + /* calculate the factorial using a for loop here*/ + + printf("10! is %d.\n", factorial); + } + +Expected Output +--------------- + + 10! is 3628800. + +Solution +-------- + + #include + + int main() { + int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int factorial = 1; + + int i; + + for(i=0;i<10;i++){ + factorial *= array[i]; + } + + printf("10! is %d.\n", factorial); + } diff --git a/tutorials/learn-c.org/ja/Function Pointers.md b/tutorials/learn-c.org/ja/Function Pointers.md new file mode 100644 index 000000000..1bbe0e51b --- /dev/null +++ b/tutorials/learn-c.org/ja/Function Pointers.md @@ -0,0 +1,161 @@ +Tutorial +-------- + +Remember pointers? We used them to point to an array of chars then make a string out of them. +Then things got more interesting when we learned how to control these pointers. +Now it is time to do something even more interesting with pointers, using them to point to and call functions. + +### Why point to a function? + +The first question that may come to your mind is why would we use pointers to call a function when we can simply call a function by its name: `function();` - that's a great question! Now imagine the `sort` function where you need to sort an array. Sometimes you want to order array elements in an ascending order or descending order. How would you choose? Function pointers! + + +### Function Pointer Syntax + + void (*pf)(int); + +I agree with you. This definitely is very complicated, or so you may think. Let's re-read that code and try to understand it point by point. Read it inside-out. `*pf` is the pointer to a function. `void` is the return type of that function, and finally `int` is the argument type of that function. Got it? Good. + +Let's insert pointers into the function pointer and try to read it again: + + char* (*pf)(int*) + +Again: +1. `*pf` is the function pointer. +2. `char*` is the return type of that function. +3. `int*` is the type of the argument. + +Ok enough with theory. Let's get our hands dirty with some real code. +See this example: + + #include + void someFunction(int arg) + { + printf("This is someFunction being called and arg is: %d\n", arg); + printf("Whoops leaving the function now!\n"); + } + + main() + { + void (*pf)(int); + pf = &someFunction; + printf("We're about to call someFunction() using a pointer!\n"); + (pf)(5); + printf("Wow that was cool. Back to main now!\n\n"); + } + +Remember `sort()` we talked about earlier? We can do the same with it. +Instead of ordering a set in an ascending way we can do the opposite using our own comparison function as follows: + + #include + #include //for qsort() + + int compare(const void* left, const void* right) + { + return (*(int*)right - *(int*)left); + // go back to ref if this seems complicated: http://www.cplusplus.com/reference/cstdlib/qsort/ + } + main() + { + int (*cmp) (const void* , const void*); + cmp = &compare; + + int iarray[] = {1,2,3,4,5,6,7,8,9}; + qsort(iarray, sizeof(iarray)/sizeof(*iarray), sizeof(*iarray), cmp); + + int c = 0; + while (c < sizeof(iarray)/sizeof(*iarray)) + { + printf("%d \t", iarray[c]); + c++; + } + } + +Let's remember again. Why do we use function pointers? +1. To allow programmers to use libraries for different usages -> "Flexibility" + + +Exercise +-------- +Complete the array of pointers to functions and call each function using its pointer from the array. Array of pointers to functions? Yes you can do that! + +Tutorial Code +------------- + + #include + + void f1(int var) + { + printf("this is f1 and var is: %d\n", var); + } + + void f2(int var) + { + printf("this is f2 and var is: %d\n", var); + } + + void f3(int var) + { + printf("this is f3 and var is: %d\n", var); + } + + int main() + { + /* define an array full of function pointers + to the above functions, that take an `int` as + their only argument */ + + + int c = 0; + while(c < 3) + { + /* call the functions using the function pointers + of the array at index `c` with `c` as an argument */ + + ++c; + } + + return 0; + } + + +Expected Output +--------------- + + this is f1 and var is: 0 + this is f2 and var is: 1 + this is f3 and var is: 2 + +Solution +-------- + + #include + + void f1(int var) + { + printf("this is f1 and var is: %d\n", var); + } + + void f2(int var) + { + printf("this is f2 and var is: %d\n", var); + } + + void f3(int var) + { + printf("this is f3 and var is: %d\n", var); + } + + int main() + { + void (*pf[])(int) = {f1, f2, f3}; + + int c = 0; + while(c < 3) + { + pf[c](c); + ++c; + } + + return 0; + } diff --git a/tutorials/learn-c.org/ja/Function arguments by reference.md b/tutorials/learn-c.org/ja/Function arguments by reference.md new file mode 100644 index 000000000..2d7e5e29b --- /dev/null +++ b/tutorials/learn-c.org/ja/Function arguments by reference.md @@ -0,0 +1,117 @@ +Tutorial +-------- + +Assuming you now understand pointers and functions, you are aware that function arguments are passed by value, by which means they are copied in and out of functions. +But what if we pass pointers to values instead of the values themselves? This will allow us to give functions control over the variables and structures of the parent functions and not just a copy of them, thus directly reading and writing the original object. + +Let's say we want to write a function which increments a number by one, called `addone`. This will not work: + + void addone(int n) { + // n is local variable which only exists within the function scope + n++; // therefore incrementing it has no effect + } + + int n; + printf("Before: %d\n", n); + addone(n); + printf("After: %d\n", n); + +However, this will work: + + void addone(int *n) { + // n is a pointer here which point to a memory-adress outside the function scope + (*n)++; // this will effectively increment the value of n + } + + int n; + printf("Before: %d\n", n); + addone(&n); + printf("After: %d\n", n); + +The difference is that the second version of `addone` receives a pointer to the variable `n` as an argument, and then it can manipulate it, because it knows where it is in the memory. + +Notice that when calling the `addone` function, we **must** pass a reference to the variable `n`, and not the variable itself - this is done so that the function knows the address of the variable, and won't just receive a copy of the variable itself. + +### Pointers to structures + +Let's say we want to create a function which moves a point forward in both `x` and `y` directions, called `move`. Instead of sending two pointers, we can now send only one pointer to the function of the point structure: + + void move(point * p) { + (*p).x++; + (*p).y++; + } + +However, if we wish to dereference a structure and access one of it's internal members, we have a shorthand syntax for that, because this operation is widely used in data structures. We can rewrite this function using the following syntax: + + void move(point * p) { + p->x++; + p->y++; + } + +Exercise +-------- + +Write a function called `birthday`, which adds one to the `age` of a `person`. + +Tutorial Code +------------- + + #include + + typedef struct { + char * name; + int age; + } person; + + /* function declaration */ + void birthday(person * p); + + /* write your function here */ + + int main() { + person john; + john.name = "John"; + john.age = 27; + + printf("%s is %d years old.\n", john.name, john.age); + birthday(&john); + printf("Happy birthday! %s is now %d years old.\n", john.name, john.age); + + return 0; + } + +Expected Output +--------------- + + John is 27 years old. + Happy birthday! John is now 28 years old. + +Solution +-------- + + #include + + typedef struct { + char * name; + int age; + } person; + + /* function declaration */ + void birthday(person * p); + + void birthday(person * p){ + p->age++; // This is the same.. + //(*p).age++; // ... as this would be + } + + int main() { + person john; + john.name = "John"; + john.age = 27; + + printf("%s is %d years old.\n", john.name, john.age); + birthday(&john); + printf("Happy birthday! %s is now %d years old.\n", john.name, john.age); + + return 0; + } diff --git a/tutorials/learn-c.org/ja/Functions.md b/tutorials/learn-c.org/ja/Functions.md new file mode 100644 index 000000000..780c73099 --- /dev/null +++ b/tutorials/learn-c.org/ja/Functions.md @@ -0,0 +1,112 @@ +Tutorial +-------- + +C functions are simple, but because of how C works, the power of functions is a bit limited. + +* Functions receive either a fixed or variable amount of arguments. +* Functions can only return one value, or return no value. + +In C, arguments are copied by value to functions, which means that we cannot change the arguments to affect their value outside of +the function. To do that, we must use pointers, which are taught later on. + +Functions are defined using the following syntax: + + int foo(int bar) { + /* do something */ + return bar * 2; + } + + int main() { + foo(1); + } + +The function `foo` we defined receives one argument, which is `bar`. The function receives an integer, multiplies it by two, and returns the result. + +To execute the function `foo` with 1 as the argument `bar`, we use the following syntax: + + foo(1); + +In C, functions must be first defined before they are used in the code. They can be either declared first and then implemented later on using a +header file or in the beginning of the C file, or they can be implemented in the order they are used (less preferable). + +The correct way to use functions is as follows: + + /* function declaration */ + int foo(int bar); + + int main() { + /* calling foo from main */ + printf("The value of foo is %d", foo(1)); + } + + int foo(int bar) { + return bar + 1; + } + +We can also create functions that do not return a value by using the keyword `void`: + + void moo() { + /* do something and don't return a value */ + } + + int main() { + moo(); + } + +Exercise +-------- + +Write a function called `print_big` which receives one argument (an integer) and prints the line `x is big` (where x is the argument) if the argument given +to the function is a number bigger than 10. + +* **Important**: Don't forget to add a newline character `\n` at the end of the printf string. + +Tutorial Code +------------- + + #include + + /* function declaration */ + void print_big(int number); + + int main() { + int array[] = { 1, 11, 2, 22, 3, 33 }; + int i; + for (i = 0; i < 6; i++) { + print_big(array[i]); + } + return 0; + } + + /* write your function here */ + +Expected Output +--------------- + + 11 is big + 22 is big + 33 is big + +Solution +-------- + + #include + + /* function declaration */ + void print_big(int number); + + int main() { + int array[] = { 1, 11, 2, 22, 3, 33 }; + int i; + for (i = 0; i < 6; i++) { + print_big(array[i]); + } + return 0; + } + + void print_big(int number){ + if(number > 10){ + printf("%d is big\n",number); + } + } + diff --git a/tutorials/learn-c.org/ja/Hello, World!.md b/tutorials/learn-c.org/ja/Hello, World!.md new file mode 100644 index 000000000..dac268015 --- /dev/null +++ b/tutorials/learn-c.org/ja/Hello, World!.md @@ -0,0 +1,98 @@ +Tutorial +-------- + +### Introduction + +The C programming language is a general purpose programming language, which relates closely to the way machines work. +Understanding how computer memory works is an important aspect of the C programming language. Although C can be considered +as "hard to learn", C is in fact a very simple language, with very powerful capabilities. + +C is a very common language, and it is the language of many applications such as Windows, the Python interpreter, Git, and +many many more. + +C is a compiled language - which means that in order to run it, the compiler (for example, GCC or Visual Studio) must take the code that +we wrote, process it, and then create an executable file. This file can then be executed, and will do what we intended for the program +to do. + +C言語は汎用プログラミング言語であり、機械の動作と密接に関連しています。 +コンピュータメモリの仕組みを理解することは、C言語の重要な側面です。C言語は「習得が難しい」と考えられがちですが、実際には非常にシンプルな言語であり、非常に強力な機能を備えています。 + +C言語は非常に一般的な言語であり、Windows、Pythonインタープリター、Gitなど、多くのアプリケーションの言語となっています。 + +C言語はコンパイル言語です。つまり、C言語を実行するには、コンパイラ(GCCやVisual Studioなど)が記述したコードを受け取り、処理して、実行ファイルを作成する必要があります。 +このファイルは実行可能であり、プログラムに期待した動作を実行します。 + +### Our first program + +Every C program uses libraries, which give the ability to execute necessary functions. For example, the most basic function +called `printf`, which prints to the screen, is defined in the `stdio.h` header file. + +To add the ability to run the `printf` command to our program, we must add the following include directive to our first line of the code: + +すべてのCプログラムは、必要な関数を実行するためのライブラリを使用します。例えば、画面に出力する最も基本的な関数である「printf」は、「stdio.h」ヘッダーファイルで定義されています。 + +プログラムに「printf」コマンドを実行する機能を追加するには、コードの1行目に次のincludeディレクティブを追加する必要があります。 + + #include + +The second part of the code is the actual code which we are going to write. The first code which will run will always reside +in the `main` function. + +コードの2番目の部分は、実際に記述するコードです。最初に実行されるコードは、常に +`main` 関数内にあります。 + + int main() { + ... our code goes here + } + +The `int` keyword indicates that the function `main` will return an integer - a simple number. The number which will be returned +by the function indicates whether the program that we wrote worked correctly. If we want to say that our code +was run successfully, we will return the number 0. A number greater than 0 will mean that the program that we wrote failed. + +`int` キーワードは、関数 `main` が整数、つまり単純な数値を返すことを示します。関数によって返される数値は、記述したプログラムが正しく動作したかどうかを示します。コードが正常に実行されたことを示す場合は、数値 0 を返します。0 より大きい数値は、記述したプログラムが失敗したことを意味します。 + +For this tutorial, we will return 0 to indicate that our program was successful: + +このチュートリアルでは、プログラムが成功したことを示すために 0 を返します。 + + return 0; + +Notice that every statement in C must end with a semicolon, so that the compiler knows that a new statement has started. + +Last but not least, we will need to call the function `printf` to print our sentence. + +C言語のすべての文はセミコロンで終わる必要があることに注意してください。これにより、コンパイラは新しい文が始まったことを認識します。 + +最後に、文を出力するために関数「printf」を呼び出す必要があります。 + +Exercise +-------- + +Change the program at the bottom so that it prints to the output "Hello, World!". + +下部のプログラムを変更して、出力に「Hello, World!」と表示されるようにします。 + +Tutorial Code +------------- + + #include + + int main() { + printf("Goodbye, World!"); + return 0; + } + +Expected Output +--------------- + + Hello, World! + +Solution +-------- + + #include + + int main() { + printf("Hello, World!"); + return 0; + } diff --git a/tutorials/learn-c.org/ja/Linked lists.md b/tutorials/learn-c.org/ja/Linked lists.md new file mode 100644 index 000000000..4aa9c44c9 --- /dev/null +++ b/tutorials/learn-c.org/ja/Linked lists.md @@ -0,0 +1,401 @@ +Tutorial +-------- + +### Introduction + +Linked lists are the best and simplest example of a dynamic data structure that uses pointers for its implementation. +However, understanding pointers is crucial to understanding how linked lists work, so if you've skipped the pointers +tutorial, you should go back and redo it. You must also be familiar with dynamic memory allocation and structures. + +Essentially, linked lists function as an array that can grow and shrink as needed, from any point in the array. + +Linked lists have a few advantages over arrays: + +1. Items can be added or removed from the middle of the list +2. There is no need to define an initial size + +However, linked lists also have a few disadvantages: + +1. There is no "random" access - it is impossible to reach the nth item in the array without first iterating over +all items up until that item. This means we have to start from the beginning of the list and count how many times +we advance in the list until we get to the desired item. +2. Dynamic memory allocation and pointers are required, which complicates the code and increases the risk of +memory leaks and segment faults. +3. Linked lists have a much larger overhead over arrays, since linked list items are dynamically allocated (which +is less efficient in memory usage) and each item in the list also must store an additional pointer. + +### What is a linked list? + +A linked list is a set of dynamically allocated nodes, arranged in such a way that each node contains one value +and one pointer. The pointer always points to the next member of the list. If the pointer is NULL, then it is +the last node in the list. + +A linked list is held using a local pointer variable which points to the first item of the list. If that pointer +is also NULL, then the list is considered to be empty. + + ------------------------------ ------------------------------ + | | | \ | | | + | DATA | NEXT |--------------| DATA | NEXT | + | | | / | | | + ------------------------------ ------------------------------ + +Let's define a linked list node: + + typedef struct node { + int val; + struct node * next; + } node_t; + +Notice that we are defining the struct in a recursive manner, which is possible in C. Let's name our node type `node_t`. + +Now we can use the nodes. Let's create a local variable which points to the first item of the list (called `head`). + + node_t * head = NULL; + head = (node_t *) malloc(sizeof(node_t)); + if (head == NULL) { + return 1; + } + + head->val = 1; + head->next = NULL; + +We've just created the first variable in the list. We must set the value, and the next item to be empty, if we want +to finish populating the list. Notice that we should always check if malloc returned a NULL value or not. + +To add a variable to the end of the list, we can just continue advancing to the next pointer: + + node_t * head = NULL; + head = (node_t *) malloc(sizeof(node_t)); + head->val = 1; + head->next = (node_t *) malloc(sizeof(node_t)); + head->next->val = 2; + head->next->next = NULL; + +This can go on and on, but what we should actually do is advance to the last item of the list, until the `next` variable +will be `NULL`. + +### Iterating over a list + +Let's build a function that prints out all the items of a list. To do this, we need to use a `current` pointer +that will keep track of the node we are currently printing. After printing the value of the node, we set the `current` +pointer to the next node, and print again, until we've reached the end of the list (the next node is NULL). + + void print_list(node_t * head) { + node_t * current = head; + + while (current != NULL) { + printf("%d\n", current->val); + current = current->next; + } + } + +### Adding an item to the end of the list + +To iterate over all the members of the linked list, we use a pointer called `current`. We set it to start from the head +and then in each step, we advance the pointer to the next item in the list, until we reach the last item. + + void push(node_t * head, int val) { + node_t * current = head; + while (current->next != NULL) { + current = current->next; + } + + /* now we can add a new variable */ + current->next = (node_t *) malloc(sizeof(node_t)); + current->next->val = val; + current->next->next = NULL; + } + +The best use cases for linked lists are stacks and queues, which we will now implement: + +### Adding an item to the beginning of the list (pushing to the list) + +To add to the beginning of the list, we will need to do the following: + +1. Create a new item and set its value +2. Link the new item to point to the head of the list +3. Set the head of the list to be our new item + +This will effectively create a new head to the list with a new value, and keep the rest of the list linked to it. + +Since we use a function to do this operation, we want to be able to modify the head variable. To do this, we must +pass a pointer to the pointer variable (a double pointer) so we will be able to modify the pointer itself. + + + void push(node_t ** head, int val) { + node_t * new_node; + new_node = (node_t *) malloc(sizeof(node_t)); + + new_node->val = val; + new_node->next = *head; + *head = new_node; + } + + +### Removing the first item (popping from the list) + +To pop a variable, we will need to reverse this action: + +1. Take the next item that the head points to and save it +2. Free the head item +3. Set the head to be the next item that we've stored on the side + +Here is the code: + + int pop(node_t ** head) { + int retval = -1; + node_t * next_node = NULL; + + if (*head == NULL) { + return -1; + } + + next_node = (*head)->next; + retval = (*head)->val; + free(*head); + *head = next_node; + + return retval; + } + + +### Removing the last item of the list + +Removing the last item from a list is very similar to adding it to the end of the list, but with one big exception - +since we have to change one item before the last item, we actually have to look two items ahead and see if the next +item is the last one in the list: + + + int remove_last(node_t * head) { + int retval = 0; + /* if there is only one item in the list, remove it */ + if (head->next == NULL) { + retval = head->val; + free(head); + return retval; + } + + /* get to the second to last node in the list */ + node_t * current = head; + while (current->next->next != NULL) { + current = current->next; + } + + /* now current points to the second to last item of the list, so let's remove current->next */ + retval = current->next->val; + free(current->next); + current->next = NULL; + return retval; + + } + + +### Removing a specific item + +To remove a specific item from the list, either by its index from the beginning of the list or by its value, we will +need to go over all the items, continuously looking ahead to find out if we've reached the node before the item +we wish to remove. This is because we need to change the location to where the previous node points to as well. + +Here is the algorithm: + +1. Iterate to the node before the node we wish to delete +2. Save the node we wish to delete in a temporary pointer +3. Set the previous node's next pointer to point to the node after the node we wish to delete +4. Delete the node using the temporary pointer + +There are a few edge cases we need to take care of, so make sure you understand the code. + + int remove_by_index(node_t ** head, int n) { + int i = 0; + int retval = -1; + node_t * current = *head; + node_t * temp_node = NULL; + + + if (n == 0) { + return pop(head); + } + + for (i = 0; i < n-1; i++) { + if (current->next == NULL) { + return -1; + } + current = current->next; + } + + if (current->next == NULL) { + return -1; + } + + temp_node = current->next; + retval = temp_node->val; + current->next = temp_node->next; + free(temp_node); + + return retval; + + } + + +Exercise +-------- + +You must implement the function `remove_by_value` which receives a double pointer to the head and removes the first +item in the list which has the value `val`. + +Tutorial Code +------------- + + #include + #include + + typedef struct node { + int val; + struct node * next; + } node_t; + + void print_list(node_t * head) { + node_t * current = head; + + while (current != NULL) { + printf("%d\n", current->val); + current = current->next; + } + } + + int pop(node_t ** head) { + int retval = -1; + node_t * next_node = NULL; + + if (*head == NULL) { + return -1; + } + + next_node = (*head)->next; + retval = (*head)->val; + free(*head); + *head = next_node; + + return retval; + } + + int remove_by_value(node_t ** head, int val) { + /* TODO: fill in your code here */ + } + + int main() { + + node_t * test_list = (node_t *) malloc(sizeof(node_t)); + test_list->val = 1; + test_list->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->val = 2; + test_list->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->val = 3; + test_list->next->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->next->val = 4; + test_list->next->next->next->next = NULL; + + remove_by_value(&test_list, 3); + + print_list(test_list); + } + +Expected Output +--------------- + + 1 + 2 + 4 + +Solution +-------- + + #include + #include + + typedef struct node { + int val; + struct node * next; + } node_t; + + void print_list(node_t * head) { + node_t * current = head; + + while (current != NULL) { + printf("%d\n", current->val); + current = current->next; + } + } + + int pop(node_t ** head) { + int retval = -1; + node_t * next_node = NULL; + + if (*head == NULL) { + return -1; + } + + next_node = (*head)->next; + retval = (*head)->val; + free(*head); + *head = next_node; + + return retval; + } + + int remove_by_value(node_t ** head, int val) { + node_t *previous, *current; + + if (*head == NULL) { + return -1; + } + + if ((*head)->val == val) { + return pop(head); + } + + previous = *head; + current = (*head)->next; + while (current) { + if (current->val == val) { + previous->next = current->next; + free(current); + return val; + } + + previous = current; + current = current->next; + } + return -1; + } + + void delete_list(node_t *head) { + node_t *current = head, + *next = head; + + while (current) { + next = current->next; + free(current); + current = next; + } + } + + int main(void) { + node_t * test_list = (node_t *) malloc(sizeof(node_t)); + + test_list->val = 1; + test_list->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->val = 2; + test_list->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->val = 3; + test_list->next->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->next->val = 4; + test_list->next->next->next->next = NULL; + + remove_by_value(&test_list, 3); + + print_list(test_list); + delete_list(test_list); + + return EXIT_SUCCESS; + } diff --git a/tutorials/learn-c.org/ja/Multidimensional Arrays.md b/tutorials/learn-c.org/ja/Multidimensional Arrays.md new file mode 100644 index 000000000..8f251441f --- /dev/null +++ b/tutorials/learn-c.org/ja/Multidimensional Arrays.md @@ -0,0 +1,179 @@ +Tutorial +-------- + +In the previous tutorials on [Arrays](https://www.learn-c.org/en/Arrays), we covered, well, arrays and how they work. The arrays we looked at were all one-dimensional, but C can create and use multi-dimensional arrays. Here is the general form of a multidimensional array declaration: + +前回の[配列](https://www.learn-c.org/en/Arrays)のチュートリアルでは、配列とその仕組みについて説明しました。これまで見てきた配列はすべて1次元でしたが、C言語では多次元配列を作成・使用できます。多次元配列の宣言の一般的な形式は次のとおりです。 + + type name[size1][size2]...[sizeN]; + +For example, here's a basic one for you to look at - + +基本的な例をここに挙げると… + + int foo[1][2][3]; + +or maybe this one - + +ほかには、こんなのも… + + char vowels[1][5] = { + {'a', 'e', 'i', 'o', 'u'} + }; + +### Two-dimensional Arrays + +The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is pretty much a list of one-dimensional arrays. To declare a two-dimensional integer array of size [ x ][ y ], you would write something like this − + +多次元配列の最も単純な形式は二次元配列です。二次元配列は、基本的に一次元配列のリストです。サイズが[ x ][ y ]の二次元整数配列を宣言するには、次のように記述します。 + + type arrayName [x][y]; + +Where __type__ can be any C data type (int, char, long, long long, double, etc.) and __arrayName__ will be a valid C identifier, or variable. A two-dimensional array can be considered as a table which will have [ x ] number of rows and [ y ] number of columns. A two-dimensional array a, which contains three rows and four columns can be shown and thought about like this − + +ここで、__type__ は任意の C データ型(int、char、long、long long、double など)で、__arrayName__ は有効な C 識別子または変数です。2 次元配列は、[ x ] 行と [ y ] 列を持つ表と考えることができます。3 行 4 列の 2 次元配列 a は、次のように表すことができます。 + +![Table 1A](https://www.tutorialspoint.com/cprogramming/images/two_dimensional_arrays.jpg) + +In this sense, every element in the array a is identified by an element name in the form __a[i][j]__, where 'a' is the name of the array, and 'i' and 'j' are the indexes that uniquely identify, or show, each element in 'a'. + +配列 a 内のすべての要素は、__a[i][j]__ という形式の要素名によって識別されます。ここで、'a' は配列の名前であり、'i' と 'j' は 'a' 内の各要素を一意に識別または表示するインデックスです。 + +And honestly, you really don't have to put in a [ x ] value really, because if you did something like this - + +正直に言うと、[x]の値を入力する必要はありません。なぜなら、次のようにすれば、 + + char vowels[][5] = { + {'A', 'E', 'I', 'O', 'U'}, + {'a', 'e', 'i', 'o', 'u'} + }; + +the compiler would already know that there are two "dimensions" you could say, but, you NEED a [ y ] value!! The compiler may be smart, but it _will not know_ how many integers, characters, floats, whatever you're using you have in the dimensions. Keep that in mind. + +既にコンパイラは2つの「次元」があることを認識しています。が、[ y ] 値は必須です。コンパイラは賢いかもしれませんが、その次元に整数、文字、浮動小数点数などが何個あるかは*認識*できません。この点にご注意ください。 + +### Initializing Two-Dimensional Arrays + +Multidimensional arrays may be used by specifying bracketed[] values for each row. Below is an array with 3 rows and each row has 4 columns. To make it easier, you can forget the 3 and keep it blank, it'll still work. + +多次元配列は、各行に括弧[]で囲まれた値を指定することで使用できます。以下は3行の配列で、各行に4列あります。わかりやすくするために、3を省略して空白のままにしても問題ありません。 + + int a[3][4] = { + {0, 1, 2, 3} , /* initializers for row indexed by 0 */ + {4, 5, 6, 7} , /* initializers for row indexed by 1 */ + {8, 9, 10, 11} /* initializers for row indexed by 2 */ + }; + +The inside braces, which indicates the wanted row, are optional. The following initialization is the same to the previous example − + +必要な行を示す内側の括弧はオプションです。以下の初期化は前の例と同じです。 + + int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; + +### Accessing Two-Dimensional Array Elements + +An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example − + +二次元配列の要素には、添字、つまり配列の行インデックスと列インデックスを使ってアクセスします。例えば、 + + int val = a[2][3]; + +The above statement will take the 4th element from the 3rd row of the array. + +上記のステートメントは、配列の 3 行目から 4 番目の要素を取得します。 + +Exercise +-------- + +Let us try to find out the average marks of a group of five students for two subjects, Mathematics and Physics. To do this, we use a two-dimensional array called ```grades```. The marks corresponding to Mathematics would be stored in the first row (```grades[0]```), whereas those corresponding to Physics would be stored in the second row (```grades[1]```). Complete the following steps so that you can execute this program. + +数学と物理の2つの科目について、5人の生徒グループの平均点を調べてみましょう。そのためには、「grades」という2次元配列を使用します。数学の点数は1行目(「grades[0]」)に格納され、物理の点数は2行目(「grades[1]」)に格納されます。このプログラムを実行するには、以下の手順を実行してください。 + +- Declare grades as a two-dimensional array of integers +- Complete the for loops by specifying their terminating conditions +- Compute the average marks obtained in each subject + +- 成績を整数の2次元配列として宣言する +- 終了条件を指定してforループを完了する +- 各科目の平均点を計算する + +Tutorial Code +------------- + + #include + + int main() { + /* TODO: declare the 2D array grades here */ + float average; + int i; + int j; + + grades[0][0] = 80; + grades[0][1] = 70; + grades[0][2] = 65; + grades[0][3] = 89; + grades[0][4] = 90; + + grades[1][0] = 85; + grades[1][1] = 80; + grades[1][2] = 80; + grades[1][3] = 82; + grades[1][4] = 87; + + /* TODO: complete the for loop with appropriate terminating conditions */ + for (i = 0; i < ; i++) { + average = 0; + for (j = 0; j < ; j++) { + average += grades[i][j]; + } + + /* TODO: compute the average marks for subject i */ + printf("The average marks obtained in subject %d is: %.2f\n", i, average); + } + + return 0; + } + + +Expected Output +--------------- + + The average marks obtained in subject 0 is: 78.80 + The average marks obtained in subject 1 is: 82.80 + +Solution +-------- + + #include + + int main() { + int grades[2][5]; + float average; + int i; + int j; + + grades[0][0] = 80; + grades[0][1] = 70; + grades[0][2] = 65; + grades[0][3] = 89; + grades[0][4] = 90; + + grades[1][0] = 85; + grades[1][1] = 80; + grades[1][2] = 80; + grades[1][3] = 82; + grades[1][4] = 87; + + for (i = 0; i < 2; i++) { + average = 0; + + for (j = 0; j < 5; j++) { + average += grades[i][j]; + } + + average /= 5.0; + printf("The average marks obtained in subject %d is: %.2f\n", i, average); + } + + return 0; + } diff --git a/tutorials/learn-c.org/ja/Pointer Arithmetics.md b/tutorials/learn-c.org/ja/Pointer Arithmetics.md new file mode 100644 index 000000000..fbdeced0c --- /dev/null +++ b/tutorials/learn-c.org/ja/Pointer Arithmetics.md @@ -0,0 +1,181 @@ +Tutorial +-------- +You previously learned what is a pointer and how to manipulate pointers. In this tutorial you will be learning the arithmetic operations on pointers. +There are multiple arithmetic operations that can be applied on C pointers: ++, --, -, + + +### Incrementing a Pointer with (++) + +Just like any variable the ++ operation increases the value of that variable. In our case here the variable is a pointer hence when we increase its value we are increasing the address in the memory that pointer points to. +Let's combine this operation with an array in our example: + + #include + + int main() + { + int intarray[5] = {10,20,30,40,50}; + + int i; + for(i = 0; i < 5; i++) + printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]); + + + int *intpointer = &intarray[3]; //point to the 4th element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 4th element + + intpointer++; //now increase the pointer's address so it points to the 5th elemnt in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + + return 0; + } + + +### Decreasing a Pointer with (--) + +Just like in our previous example we increased the pointer's pointed-to address by one using the ++ operator, we can decrease the address pointed-to by one using the decrement operator (--). + + #include + + int main() + { + int intarray[5] = {10,20,30,40,50}; + + int i; + for(i = 0; i < 5; i++) + printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]); + + + int *intpointer = &intarray[4]; //point to the 5th element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + + intpointer--; //now decrease the point's address so it points to the 4th element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 4th element + + return 0; + } + +### Adding Pointers with (+) +We previously increased a pointer's pointed-to address by one. We can also increase it by an integer value such: + + #include + + int main() + { + int intarray[5] = {10,20,30,40,50}; + + int i; + for(i = 0; i < 5; i++) + printf("intarray[%d] has value: %d - and address @ %x\n", i, intarray[i], &intarray[i]); + + + int *intpointer = &intarray[1]; //point to the 2nd element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 2nd element + + intpointer += 2; //now shift by two the point's address so it points to the 4th element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the addres of the 4th element + + return 0; + } + +Note how in the output the address shifted by 8 steps in the memory. You might be wondering why? +The answer is simple: Because our pointer is an int-pointer and the size of an int variable is 4 bytes the memory is shift-able by 4 blocks. +In our code we shifted by 2 (added +2) to the initial address so that makes them 2 x 4 byte = 8. + +### Subtracting Pointers with (-) + +Similarly we can subtract: + + #include + + int main() + { + int intarray[5] = {10,20,30,40,50}; + + int i; + for(i = 0; i < 5; i++) + printf("intarray[%d] has value: %d - and address @ %x\n", i, intarray[i], &intarray[i]); + + + int *intpointer = &intarray[4]; //point to the 5th element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + + intpointer -= 2; //now shift by two the point's address so it points to the 3rd element in the array + printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 3rd element + + return 0; + } + +again the address is shifted by blocks of 4bytes (in case of int). + +### Other Operations +There are more operations such as comparison >, <, ==. The idea is very similar of comparing variables, but in this case we are comparing memory address. + +Exercise +-------- +Copy last three addresses of intarray into parray which is an array of pointers to an int. + +Tutorial Code +------------- + #include + + int main() { + int intarray[5] = {10,20,30,40,50}; + //-----------------------^ + int *pointer = &intarray[2]; + + // Array of 3 pointers + int *parray[3]; + + // Copy last three addresses of intarray into parray + // Use parray and pointer + int i; + for (i = 0; i < 3; i++) { + // Insert code here + } + + // Test code + for (i = 0; i < 3; i++) { + if (parray[i] == &pointer[i]) { + printf("Matched!\n"); + } else { + printf("Fail\n"); + } + } + + return 0; + } + + +Expected Output +--------------- + Matched! + Matched! + Matched! + +Solution +-------- + #include + + int main() { + int intarray[5] = {10,20,30,40,50}; + //-----------------------^ + int *pointer = &intarray[2]; + + int *parray[3]; + + int i; + for (i = 0; i < 3; i++) { + parray[i] = pointer + i; + } + + for (i = 0; i < 3; i++) { + if (parray[i] == &pointer[i]) { + printf("Matched!\n"); + } else { + printf("Fail\n"); + } + } + + return 0; + } + + \ No newline at end of file diff --git a/tutorials/learn-c.org/ja/Pointers.md b/tutorials/learn-c.org/ja/Pointers.md new file mode 100644 index 000000000..bdc174b1e --- /dev/null +++ b/tutorials/learn-c.org/ja/Pointers.md @@ -0,0 +1,116 @@ +Tutorial +-------- + +Pointers are also variables and play a very important role in C programming language. They are used for several reasons, such as: + +* Strings +* Dynamic memory allocation +* Sending function arguments by reference +* Building complicated data structures +* Pointing to functions +* Building special data structures (i.e. Tree, Tries, etc...) + +And many more. + +### What is a pointer? + +A pointer is essentially a simple integer variable which holds a **memory address** that points to a value, instead of holding the actual value itself. + +The computer's memory is a sequential store of data, and a pointer points to a specific part of the memory. Our program can use pointers in such a way that the pointers point to a large amount of memory - depending on how much we decide to read from that point on. + +### Strings as pointers + +We've already discussed strings, but now we can dive in a bit deeper and understand what strings in C really are (which are called C-Strings to differentiate them from other strings when mixed with C++) + +The following line: + + char * name = "John"; + +does three things: + +1. It allocates a local (stack) variable called `name`, which is a pointer to a single character. +2. It causes the string "John" to appear somewhere in the program memory (after it is compiled and executed, of course). +3. It initializes the `name` argument to point to where the `J` character resides at (which is followed by the rest of the string in the memory). + +If we try to access the `name` variable as an array, it will work, and will return the ordinal value of the character `J`, since the `name` variable actually points exactly to the beginning of the string. + +Since we know that the memory is sequential, we can assume that if we move ahead in the memory to the next character, we'll receive the next letter in the string, until we reach the end of the string, marked with a null terminator (the character with the ordinal value of 0, noted as `\0`). + +### Dereferencing + +Dereferencing is the act of referring to where the pointer points, instead of the memory address. We are already using dereferencing in arrays - but we just didn't know it yet. The brackets operator - `[0]` for example, accesses the first item of the array. And since arrays are actually pointers, accessing the first item in the array is the same as dereferencing a pointer. Dereferencing a pointer is done using the asterisk operator `*`. + +If we want to create an array that will point to a different variable in our stack, we can write the following code: + + /* define a local variable a */ + int a = 1; + + /* define a pointer variable, and point it to a using the & operator */ + int * pointer_to_a = &a; + + printf("The value a is %d\n", a); + printf("The value of a is also %d\n", *pointer_to_a); + +Notice that we used the `&` operator to point at the variable `a`, which we have just created. + +We then referred to it using the dereferencing operator. We can also change the contents of the dereferenced variable: + + int a = 1; + int * pointer_to_a = &a; + + /* let's change the variable a */ + a += 1; + + /* we just changed the variable again! */ + *pointer_to_a += 1; + + /* will print out 3 */ + printf("The value of a is now %d\n", a); + +Exercise +-------- + +Create a pointer to the local variable `n` called `pointer_to_n`, and use it to increase the value of `n` by one. + +Tutorial Code +------------- + + #include + + int main() { + int n = 10; + + /* your code goes here */ + + /* testing code */ + if (pointer_to_n != &n) return 1; + if (*pointer_to_n != 11) return 1; + + printf("Done!\n"); + return 0; + } + +Expected Output +--------------- + + Done! + +Solution +-------- + + #include + + int main() { + int n = 10; + + int * pointer_to_n = &n; + + *pointer_to_n += 1; + + /* testing code */ + if (pointer_to_n != &n) return 1; + if (*pointer_to_n != 11) return 1; + + printf("Done!\n"); + return 0; + } diff --git a/tutorials/learn-c.org/ja/Recursion.md b/tutorials/learn-c.org/ja/Recursion.md new file mode 100644 index 000000000..a3aac5352 --- /dev/null +++ b/tutorials/learn-c.org/ja/Recursion.md @@ -0,0 +1,89 @@ +Tutorial +-------- + +Recursion occurs when a function contains within it a call to itself. Recursion can result in very neat, elegant code that is intuitive to follow. It can also result in a very large amount of memory being used if the recursion gets too deep. + +Common examples of where recursion is used : + +* Walking recursive data structures such as linked lists, binary trees, etc. +* Exploring possible scenarios in games such as chess + +Recursion always consists of two main parts. A terminating case that indicates when the recursion will finish and a call to itself that must make progress towards the terminating case. + +For example, this function will perform multiplication by recursively adding : + + #include + + unsigned int multiply(unsigned int x, unsigned int y) + { + if (x == 1) + { + /* Terminating case */ + return y; + } + else if (x > 1) + { + /* Recursive step */ + return y + multiply(x-1, y); + } + + /* Catch scenario when x is zero */ + return 0; + } + + int main() { + printf("3 times 5 is %d", multiply(3, 5)); + return 0; + } + +Exercise +-------- + +Define a new function called `factorial()` that will compute the factorial by recursive multiplication (5! = 5 x 4 x 3 x 2 x 1). Note that by convention, the factorial of 0 is equal to 1 (0! = 1). + +Tutorial Code +------------- + + #include + + int main() { + /* testing code */ + printf("0! = %i\n", factorial(0)); + printf("1! = %i\n", factorial(1)); + printf("3! = %i\n", factorial(3)); + printf("5! = %i\n", factorial(5)); + } + + /* define your function here (don't forget to declare it) */ + +Expected Output +--------------- + + 0! = 1 + 1! = 1 + 3! = 6 + 5! = 120 + +Solution +-------- + + #include + + int factorial(int number); + + int main() { + /* testing code */ + printf("0! = %i\n", factorial(0)); + printf("1! = %i\n", factorial(1)); + printf("3! = %i\n", factorial(3)); + printf("5! = %i\n", factorial(5)); + } + + int factorial(int number) { + if (number > 1) { + return number * factorial(number-1); + } + else { + return 1; + } + } diff --git a/tutorials/learn-c.org/ja/Static.md b/tutorials/learn-c.org/ja/Static.md new file mode 100644 index 000000000..076844519 --- /dev/null +++ b/tutorials/learn-c.org/ja/Static.md @@ -0,0 +1,95 @@ +Tutorial +-------- +`static` is a keyword in the C programming language. It can be used with variables and functions. + +### What is a static variable? +By default, variables are local to the scope in which they are defined. Variables can be declared as static to increase their scope up to file containing them. As a result, these variables can be accessed anywhere inside a file. + +Consider the following scenario – where we want to count the runners participating in a race: + + #include + int runner() { + int count = 0; + count++; + return count; + } + + int main() + { + printf("%d ", runner()); + printf("%d ", runner()); + return 0; + } + +We will see that `count` is not updated because it is removed from memory as soon as the function completes. If `static` is used however, we get the desired result: + + #include + int runner() + { + static int count = 0; + count++; + return count; + } + + int main() + { + printf("%d ", runner()); + printf("%d ", runner()); + return 0; + } + +### What is a static function? +By default, functions are global in C. If we declare a function with `static`, the scope of that function is reduced to the file containing it. + +The syntax looks like this: + + static void fun(void) { + printf("I am a static function."); + } + +### Static vs Global? +While static variables have scope over the file containing them making them accessible only inside a given file, global variables can be accessed outside the file too. + +Exercise +-------- +In this exercise, try to find the sum of some numbers by using the static keyword. Do not pass any variable representing the running total to the `sum()` function. + +Tutorial Code +------------- + + #include + int sum (int num) { + /** + * find sum to n numbers + */ + } + + int main() { + printf("%d ",sum(55)); + printf("%d ",sum(45)); + printf("%d ",sum(50)); + return 0; + } + +Expected Output +--------------- + + 55 100 150 + +Solution +-------- + + #include + int sum (int num) { + static int total = 0; + total += num; + return total; + } + + int main() { + printf("%d ",sum(55)); + printf("%d ",sum(45)); + printf("%d ",sum(50)); + return 0; + } + diff --git a/tutorials/learn-c.org/ja/Strings.md b/tutorials/learn-c.org/ja/Strings.md new file mode 100644 index 000000000..676ba77e6 --- /dev/null +++ b/tutorials/learn-c.org/ja/Strings.md @@ -0,0 +1,127 @@ +Tutorial +-------- + +### Defining strings + +Strings in C are actually arrays of characters. Although using pointers in C is an advanced subject, fully explained later on, we will use pointers to a character array to define simple strings, in the following manner: + + char * name = "John Smith"; + +This method creates a string which we can only use for reading. +If we wish to define a string which can be manipulated, we will need to define it as a local character array: + + char name[] = "John Smith"; + +This notation is different because it allocates an array variable so we can manipulate it. The empty brackets notation `[]` tells the +compiler to calculate the size of the array automatically. This is in fact the same as allocating it explicitly, adding one to +the length of the string: + + char name[] = "John Smith"; + /* is the same as */ + char name[11] = "John Smith"; + +The reason that we need to add one, although the string `John Smith` is exactly 10 characters long, is for the string termination: +a special character (equal to 0) which indicates the end of the string. The end of the string is marked because the program +does not know the length of the string - only the compiler knows it according to the code. + +### String formatting with printf + +We can use the `printf` command to format a string together with other strings, in the following manner: + + char * name = "John Smith"; + int age = 27; + + /* prints out 'John Smith is 27 years old.' */ + printf("%s is %d years old.\n", name, age); + +Notice that when printing strings, we must add a newline (`\n`) character so that our next `printf` statement will print in a new line. + +### String Length + +The function 'strlen' returns the length of the string which has to be passed as an argument: + + char * name = "Nikhil"; + printf("%d\n",strlen(name)); + +### String comparison + +The function `strncmp` compares between two strings, returning the number 0 if they are equal, or a different number if they are different. +The arguments are the two strings to be compared, and the maximum comparison length. There is also an unsafe version of this function +called `strcmp`, but it is not recommended to use it. For example: + + char * name = "John"; + + if (strncmp(name, "John", 4) == 0) { + printf("Hello, John!\n"); + } else { + printf("You are not John. Go away.\n"); + } + +### String Concatenation + +The function 'strncat' appends first n characters of src string to the destination string where n is min(n,length(src)); +The arguments passed are destination string, source string, and n - maximum number of characters to be appended. For Example: + + char dest[20]="Hello"; + char src[20]="World"; + strncat(dest,src,3); + printf("%s\n",dest); + strncat(dest,src,20); + printf("%s\n",dest); + +Exercise +-------- + +Define the string `first_name` with the value `John` using the pointer notation, and define the string `last_name` with the value `Doe` +using the local array notation. + +Tutorial Code +------------- + + #include + #include + int main() { + /* define first_name */ + /* define last_name */ + char name[100]; + + last_name[0] = 'B'; + sprintf(name, "%s %s", first_name, last_name); + if (strncmp(name, "John Boe", 100) == 0) { + printf("Done!\n"); + } + name[0]='\0'; + strncat(name,first_name,4); + strncat(name,last_name,20); + printf("%s\n",name); + return 0; + } + + +Expected Output +--------------- + + Done! + JohnBoe + +Solution +-------- + + #include + #include + int main() { + char * first_name = "John"; + char last_name[] = "Doe"; + char name[100]; + + last_name[0] = 'B'; + sprintf(name, "%s %s", first_name, last_name); + if (strncmp(name, "John Boe", 100) == 0) { + printf("Done!\n"); + } + name[0]='\0'; + strncat(name,first_name,4); + strncat(name,last_name,20); + printf("%s\n",name); + return 0; + } diff --git a/tutorials/learn-c.org/ja/Structures.md b/tutorials/learn-c.org/ja/Structures.md new file mode 100644 index 000000000..4e971ddbe --- /dev/null +++ b/tutorials/learn-c.org/ja/Structures.md @@ -0,0 +1,103 @@ +Tutorial +-------- + +C structures are special, large variables which contain several named variables inside. Structures are the basic foundation for objects and classes in C. Structures are used for: + +* Serialization of data +* Passing multiple arguments in and out of functions through a single argument +* Data structures such as linked lists, binary trees, and more + +The most basic example of structures are **points**, which are a single entity that contains two variables - `x` and `y`. Let's define a point: + + struct point { + int x; + int y; + }; + +Now, let's define a new point, and use it. Assume the function `draw` receives a point and draws it on a screen. Without structs, using it would require two arguments - each for every coordinate: + + /* draws a point at 10, 5 */ + int x = 10; + int y = 5; + draw(x, y); + +Using structs, we can pass a point argument: + + /* draws a point at 10, 5 */ + struct point p; + p.x = 10; + p.y = 5; + draw(p); + +To access the point's variables, we use the dot `.` operator. + +### Typedefs + +Typedefs allow us to define types with a different name - which can come in handy when dealing with structs and pointers. In this case, we'd want to get rid of the long definition of a point structure. We can use the following syntax to remove the `struct` keyword from each time we want to define a new point: + + typedef struct { + int x; + int y; + } point; + +This will allow us to define a new point like this: + + point p; + +Structures can also hold pointers - which allows them to hold strings, or pointers to other structures as well - which is their real power. For example, we can define a vehicle structure in the following manner: + + typedef struct { + char * brand; + int model; + } vehicle; + +Since brand is a char pointer, the vehicle type can contain a string (which, in this case, indicates the brand of the vehicle). + + vehicle mycar; + mycar.brand = "Ford"; + mycar.model = 2007; + +Exercise +-------- + +Define a new data structure, named "person", which contains a string (pointer to char) called `name`, and an integer called `age`. + +Tutorial Code +------------- + + #include + + /* define the person struct here using the typedef syntax */ + + int main() { + person john; + + /* testing code */ + john.name = "John"; + john.age = 27; + printf("%s is %d years old.", john.name, john.age); + } + +Expected Output +--------------- + + John is 27 years old. + +Solution +-------- + + #include + + typedef struct { + char * name; + int age; + } person; + + int main() { + person john; + + /* testing code */ + john.name = "John"; + john.age = 27; + printf("%s is %d years old.", john.name, john.age); + } diff --git a/tutorials/learn-c.org/ja/Unions.md b/tutorials/learn-c.org/ja/Unions.md new file mode 100644 index 000000000..e3615fa87 --- /dev/null +++ b/tutorials/learn-c.org/ja/Unions.md @@ -0,0 +1,156 @@ +Tutorial +-------- + +C Unions are essentially the same as C Structures, except that instead of containing multiple variables each with their own memory a Union allows for multiple names to the same variable. These names can treat the memory as different types (and the size of the union will be the size of the largest type, + any padding the compiler might decide to give it) + +So if you wanted to be able to read a variable's memory in different ways, for example read an integer one byte at a time, you could have something like this: + + union intParts { + int theInt; + char bytes[sizeof(int)]; + }; + +Allowing you to look at each byte individually without casting a pointer and using pointer arithmetic: + + union intParts parts; + parts.theInt = 5968145; // arbitrary number > 255 (1 byte) + + printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", + parts.theInt, parts.bytes[0], parts.bytes[1], parts.bytes[2], parts.bytes[3]); + + // vs + + int theInt = parts.theInt; + printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", + theInt, *((char*)&theInt+0), *((char*)&theInt+1), *((char*)&theInt+2), *((char*)&theInt+3)); + + // or with array syntax which can be a tiny bit nicer sometimes + + printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", + theInt, ((char*)&theInt)[0], ((char*)&theInt)[1], ((char*)&theInt)[2], ((char*)&theInt)[3]); + +Combining this with a structure allows you to create a "tagged" union which can be used to store multiple different types, one at a time. + +For example, you might have a "number" struct, but you don't want to use something like this: + + struct operator { + int intNum; + float floatNum; + int type; + double doubleNum; + }; + +Because your program has a lot of them and it takes a bit too much memory for all of the variables, so you could use this: + + struct operator { + int type; + union { + int intNum; + float floatNum; + double doubleNum; + } types; + }; + +Like this the size of the struct is just the size of the int `type` + the size of the largest type in the union (the double). Not a huge gain, only 8 or 16 bytes, but the concept can be applied to similar structs. + +use: + + operator op; + op.type = 0; // int, probably better as an enum or macro constant + op.types.intNum = 352; + +Also, if you don't give the union a name then it's members are accessed directly from the struct: + + struct operator { + int type; + union { + int intNum; + float floatNum; + double doubleNum; + }; // no name! + }; + + operator op; + op.type = 0; // int + // intNum is part of the union, but since it's not named you access it directly off the struct itself + op.intNum = 352; + +Another, perhaps more useful feature, is when you always have multiple variables of the same type, and you want to be able to use both names (for readability) and indexes (for ease of iteration), in that case you can do something like this: + + union Coins { + struct { + int quarter; + int dime; + int nickel; + int penny; + }; // anonymous struct acts the same way as an anonymous union, members are on the outer container + int coins[4]; + }; + +In that example you can see that there is a struct which contains the four (common) coins in the United States. + +since the union makes the variables share the same memory the coins array matches with each int in the struct (in order): + + union Coins change; + for(int i = 0; i < sizeof(change) / sizeof(int); ++i) + { + scanf("%i", change.coins + i); // BAD code! input is always suspect! + } + printf("There are %i quarters, %i dimes, %i nickels, and %i pennies\n", + change.quarter, change.dime, change.nickel, change.penny); + + +Exercise +-------- + +Create a union that stores an array of 21 characters and 6 ints (6 since 21 / 4 == 5, but 5 * 4 == 20 so you need 1 more for the purpose of this exercise), you will set the integers to 6 given values and then print out the character array both as a series of chars and as a string. + +Tutorial Code +------------- + + #include + + /* define the union here */ + + int main() { + + // initializer lists like this are assigned to the first member of the union/struct! + // There are 6 ints here so... + intCharacters = {{1853169737, 1936876900, 1684955508, 1768838432, 561213039, 0}}; + + /* testing code */ + printf("["); + // only go to 18 because 1 byte is for the terminating 0 and we don't print the last in the loop + for(int i = 0; i < 19; ++i) + printf("%c, ", intCharacters.chars[i]); + printf("%c]\n", intCharacters.chars[19]); + + printf("%s\n", intCharacters.chars); + } + +Expected Output +--------------- + + [I, , u, n, d, e, r, s, t, a, n, d, , U, n, i, o, n, s, !] + I understand Unions! + +Solution +-------- + + #include + + union hiddenMessage { + int ints[6]; + char chars[21]; + }; + + int main() { + union hiddenMessage intCharacters = {{1853169737, 1936876900, 1684955508, 1768838432, 561213039, 0}}; + + printf("["); + // only go to 18 because 1 byte is for the terminating 0 and we don't print the last in the loop + for(int i = 0; i < 19; ++i) + printf("%c, ", intCharacters.chars[i]); + printf("%c]\n", intCharacters.chars[19]); + printf("%s\n", intCharacters.chars); + } diff --git a/tutorials/learn-c.org/ja/Variables and Types.md b/tutorials/learn-c.org/ja/Variables and Types.md new file mode 100644 index 000000000..447bc72eb --- /dev/null +++ b/tutorials/learn-c.org/ja/Variables and Types.md @@ -0,0 +1,105 @@ +Tutorial +-------- + +### Data types + +C has several types of variables, but there are a few basic types: + +* Integers - whole numbers which can be either positive or negative. Defined using `char`, `int`, `short`, `long` or `long long`. +* Unsigned integers - whole numbers which can only be positive. Defined using `unsigned char`, `unsigned int`, `unsigned short`, `unsigned long` or `unsigned long long`. +* Floating point numbers - real numbers (numbers with fractions). Defined using `float` and `double`. +* Structures - will be explained later, in the Structures section. + +The different types of variables define their bounds. A `char` can range only from -128 to 127, whereas a `long` can range from -2,147,483,648 to 2,147,483,647 (`long` and other numeric data types may have another range on different computers, for example - from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer). + +Note that C does _not_ have a boolean type. Usually, it is defined using the following notation: + +C言語にはいろいろな変数型がありますが、基本的な型はわずかです。 + +* 整数 - 正または負の値をとる整数。`char`、`int`、`short`、`long`、`long long` のいずれかで定義されます。 +* 符号なし整数 - 正の値しかとれない整数。`unsigned char`、`unsigned int`、`unsigned short`、`unsigned long`、`unsigned long long` のいずれかで定義されます。 +* 浮動小数点数 - 実数(小数部を含む数)。`float` と `double` のいずれかで定義されます。 +* 構造体 - 後述の「構造体」セクションで説明します。 + +変数の型によって、その境界が定義されます。 `char` 型の範囲は -128 から 127 までですが、`long` 型の範囲は -2,147,483,648 から 2,147,483,647 までです(`long` 型やその他の数値データ型は、コンピュータによって範囲が異なる場合があります。例えば、64 ビットコンピュータでは -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807 までです)。 + +C にはブール型が存在しないことに注意してください。通常、ブール型は以下の表記法で定義されます。 + + #define BOOL char + #define FALSE 0 + #define TRUE 1 + +C uses arrays of characters to define strings, and will be explained in the Strings section. + +C では文字の配列を使用して文字列を定義します。これについては文字列のセクションで説明します。 + +### Defining variables + +For numbers, we will usually use the type `int`. On most computers today, it is a 32-bit number, which means the number can range from -2,147,483,648 to 2,147,483,647. + +To define the variables `foo` and `bar`, we need to use the following syntax: + +数値の場合、通常は `int` 型を使用します。今日のほとんどのコンピュータでは、これは 32 ビット数値であり、数値の範囲は -2,147,483,648 から 2,147,483,647 です。 + +変数 `foo` と `bar` を定義するには、次の構文を使用する必要があります。 + + int foo; + int bar = 1; + +The variable `foo` can be used, but since we did not initialize it, we don't know what's in it. The variable `bar` contains the number 1. + +Now, we can do some math. Assuming `a`, `b`, `c`, `d`, and `e` are variables, we can simply use plus, minus and multiplication operators +in the following notation, and assign a new value to `a`: + +変数「foo」は使用できますが、初期化していないため、その内容はわかりません。変数「bar」には数値「1」が格納されています。 + +さて、計算してみましょう。「a」、「b」、「c」、「d」、「e」を変数と仮定すると、以下の記法で加算、減算、乗算演算子を使用し、 +「a」に新しい値を代入します。 + + int a = 0, b = 1, c = 2, d = 3, e = 4; + a = b - c + d * e; + printf("%d", a); /* will print 1-2+3*4 = 11 */ + +Exercise +-------- + +In the next exercise, you will need to create a program which prints out the sum of the numbers `a`, `b`, and `c`. + +次の演習では、数値 `a`、`b`、`c` の合計を出力するプログラムを作成する必要があります。 + +Tutorial Code +------------- + + #include + + int main() { + int a = 3; + float b = 4.5; + double c = 5.25; + float sum; + + /* Your code goes here */ + + printf("The sum of a, b, and c is %f.", sum); + return 0; + } + +Expected Output +--------------- + The sum of a, b, and c is 12.750000. + +Solution +-------- + #include + + int main() { + int a = 3; + float b = 4.5; + double c = 5.25; + float sum; + + sum = a + b + c; + + printf("The sum of a, b, and c is %f.", sum); + return 0; + } diff --git a/tutorials/learn-c.org/ja/Welcome.md b/tutorials/learn-c.org/ja/Welcome.md new file mode 100644 index 000000000..88fe1b447 --- /dev/null +++ b/tutorials/learn-c.org/ja/Welcome.md @@ -0,0 +1,42 @@ +# Welcome + +Welcome to the learn-c.org free interactive C tutorial. This website is proudly supported by [Boot.dev's Learn Memory Management in C course](https://www.boot.dev/courses/learn-memory-management-c?promo=LEARNXORG). If you'd like to learn how to manage memory in C from start to finish, [become a member and use code LEARNXORG](https://www.boot.dev/pricing?promo=LEARNXORG) for 25% off your first year! + +Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the C programming language. + +There is no need to download anything - Just click on the chapter you wish to begin from, and follow the instructions. Good luck! + +learn-c.org is still under construction - If you wish to contribute tutorials, please click on `Contributing Tutorials` down below. + +### Learn the Basics + +- [[Hello, World!]] +- [[Variables and Types]] +- [[Arrays]] +- [[Multidimensional Arrays]] +- [[Conditions]] +- [[Strings]] +- [[For loops]] +- [[While loops]] +- [[Functions]] +- [[Static]] + +### Advanced + +- [[Pointers]] +- [[Structures]] +- [[Function arguments by reference]] +- [[Dynamic allocation]] +- [[Arrays and Pointers]] +- [[Recursion]] +- [[Linked lists]] +- [[Binary trees]] +- [[Unions]] +- [[Pointer Arithmetics]] +- [[Function Pointers]] +- [[Bitmasks]] + +### Contributing Tutorials + +Read more here: [[Contributing Tutorials]] + diff --git a/tutorials/learn-c.org/ja/While loops.md b/tutorials/learn-c.org/ja/While loops.md new file mode 100644 index 000000000..26c66fa3c --- /dev/null +++ b/tutorials/learn-c.org/ja/While loops.md @@ -0,0 +1,110 @@ +Tutorial +-------- + +While loops are similar to for loops, but have less functionality. A while loop continues executing the while block as long as the +condition in the while remains true. For example, the following code will execute exactly ten times: + + int n = 0; + while (n < 10) { + n++; + } + +While loops can also execute infinitely if a condition is given which always evaluates as true (non-zero): + + while (1) { + /* do something */ + } + +### Loop directives + +There are two important loop directives that are used in conjunction with all loop types in C - the `break` and `continue` directives. + +The `break` directive halts a loop after ten loops, even though the while loop never finishes: + + int n = 0; + while (1) { + n++; + if (n == 10) { + break; + } + } + +In the following code, the `continue` directive causes the `printf` command to be skipped, so that only even numbers are printed out: + + int n = 0; + while (n < 10) { + n++; + + /* check that n is odd */ + if (n % 2 == 1) { + /* go back to the start of the while block */ + continue; + } + + /* we reach this code only if n is even */ + printf("The number %d is even.\n", n); + } + +Exercise +-------- + +The `array` variable consists of a sequence of ten numbers. Inside the while loop, you must write two `if` conditions, +which change the flow of the loop in the following manner (without changing the `printf` command): + +* If the current number which is about to printed is less than 5, don't print it. +* If the current number which is about to printed is greater than 10, don't print it and stop the loop. + +Notice that if you do not advance the iterator variable `i` and use the `continue` derivative, you will get stuck in an infinite loop. + +Tutorial Code +------------- + + #include + + int main() { + int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4}; + int i = 0; + + while (i < 10) { + /* your code goes here */ + + printf("%d\n", array[i]); + i++; + } + + return 0; + } + +Expected Output +--------------- + + 7 + 5 + 9 + 5 + +Solution +-------- + + #include + + int main() { + int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4}; + int i = 0; + + while (i < 10) { + if(array[i] < 5){ + i++; + continue; + } + + if(array[i] > 10){ + break; + } + + printf("%d\n", array[i]); + i++; + } + + return 0; + } From 20d76d0b698397e551b5e5f0d0e2c50587ae66a4 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Wed, 26 Nov 2025 18:08:39 +0900 Subject: [PATCH 02/20] =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E5=85=83=E9=85=8D?= =?UTF-8?q?=E5=88=97=E3=81=BE=E3=81=A7=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Arrays and Pointers.md | 18 ++--- tutorials/learn-c.org/ja/Arrays.md | 46 +++++-------- tutorials/learn-c.org/ja/Binary trees.md | 18 ++--- tutorials/learn-c.org/ja/Bitmasks.md | 18 ++--- tutorials/learn-c.org/ja/Conditions.md | 18 ++--- .../learn-c.org/ja/Dynamic allocation.md | 18 ++--- tutorials/learn-c.org/ja/For loops.md | 18 ++--- tutorials/learn-c.org/ja/Function Pointers.md | 18 ++--- .../ja/Function arguments by reference.md | 18 ++--- tutorials/learn-c.org/ja/Functions.md | 18 ++--- tutorials/learn-c.org/ja/Hello, World!.md | 66 +++++-------------- tutorials/learn-c.org/ja/Linked lists.md | 18 ++--- .../learn-c.org/ja/Multidimensional Arrays.md | 66 ++++++------------- .../learn-c.org/ja/Pointer Arithmetics.md | 18 ++--- tutorials/learn-c.org/ja/Pointers.md | 18 ++--- tutorials/learn-c.org/ja/Recursion.md | 18 ++--- tutorials/learn-c.org/ja/Static.md | 18 ++--- tutorials/learn-c.org/ja/Strings.md | 18 ++--- tutorials/learn-c.org/ja/Structures.md | 18 ++--- tutorials/learn-c.org/ja/Unions.md | 18 ++--- .../learn-c.org/ja/Variables and Types.md | 52 ++++----------- tutorials/learn-c.org/ja/While loops.md | 18 ++--- 22 files changed, 230 insertions(+), 324 deletions(-) diff --git a/tutorials/learn-c.org/ja/Arrays and Pointers.md b/tutorials/learn-c.org/ja/Arrays and Pointers.md index 213989ed4..bde3c6c9b 100644 --- a/tutorials/learn-c.org/ja/Arrays and Pointers.md +++ b/tutorials/learn-c.org/ja/Arrays and Pointers.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- In a previous tutorial on [[Pointers]], you learned that a pointer to a given data type can store the address of any variable of that particular data type. For example, in the following code, the pointer variable `pc` stores the address of the character variable `c`. @@ -124,8 +124,8 @@ We conclude this tutorial by looking at dynamic memory allocation for a two-dime free(pvowels); -Exercise --------- +演習 +---- The first seven rows of [Pascal's triangle](http://mathworld.wolfram.com/PascalsTriangle.html) are shown below. Note that row *i* contains *i* elements. Therefore, to store the numbers from the first three rows, one would require 1 + 2 + 3 = 6 memory slots. @@ -146,8 +146,8 @@ The first seven rows of [Pascal's triangle](http://mathworld.wolfram.com/Pascals Complete the skeleton code given below to store the numbers from the first three rows of Pascal's triangle in a two-dimensional "array" using dynamic memory allocation. Note that you must allocate exactly six memory slots to store those six numbers. No extra memory should be allocated. At the end of your program, free all the memory blocks used in this program. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -185,15 +185,15 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- 1 11 121 -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/Arrays.md b/tutorials/learn-c.org/ja/Arrays.md index 519f801e1..de5c4e9d2 100644 --- a/tutorials/learn-c.org/ja/Arrays.md +++ b/tutorials/learn-c.org/ja/Arrays.md @@ -1,17 +1,11 @@ -Tutorial --------- - -Arrays are special variables which can hold more than one value under the same variable name, organised with an index. Arrays are defined using a very -straightforward syntax: +チュートリアル +------------- 配列は、同じ変数名で複数の値をインデックスで整理して保持できる特殊な変数です。配列は非常に簡単な構文で定義されます。 - /* defines an array of 10 integers */ + /* 10個の整数の配列を定義する */ int numbers[10]; -Accessing a number from the array is done using the same syntax. Notice that arrays in C are zero-based, which means that if we -defined an array of size 10, then the array cells 0 through 9 (inclusive) are defined. `numbers[10]` is not an actual value. - 配列から数値にアクセスする場合も同じ構文を使用します。C言語の配列は0から始まります。つまり、サイズが10の配列を定義した場合、配列のセル0から9(両端を含む)が定義されます。`numbers[10]` は有効な値を持ちません。 int numbers[10]; @@ -25,36 +19,30 @@ defined an array of size 10, then the array cells 0 through 9 (inclusive) are de numbers[5] = 60; numbers[6] = 70; - /* print the 7th number from the array, which has an index of 6 */ + /* 配列のインデックスが6である7番目の数値を出力します */ printf("The 7th number in the array is %d", numbers[6]); -Arrays can only have one type of variable, because they are implemented as a sequence of values in the computer's memory. -Because of that, accessing a specific array cell is very efficient. - 配列はコンピュータのメモリ内で、『値が等間隔に整列したもの』として実装されるため、1種類の変数しか持つことができません。 そのため、特定の配列セルへのアクセスは非常に効率的です。 -Exercise --------- - -* The code below does not compile, because the `grades` variable is missing. -* One of the grades is missing. Can you define it so the grade average will be 85? +演習 +---- * 以下のコードは `grades` 変数が欠落しているためコンパイルできません。 * 成績の1つが欠落しています。成績平均が85になるように定義できますか? -Tutorial Code -------------- +チュートリアル コード +------------------- #include int main() { - /* TODO: define the grades variable here */ + /* TODO: ここでgrades変数を定義します */ int average; grades[0] = 80; - /* TODO: define the missing grade - so that the average will sum to 85. */ + /* TODO: 欠落している成績を + 平均が85になるように定義します。 */ grades[2] = 90; average = (grades[0] + grades[1] + grades[2]) / 3; @@ -63,24 +51,24 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- The average of the 3 grades is: 85 -Solution --------- +解答 +---- #include int main() { - /* TODO: define the grades variable here */ + /* TODO: ここでgrades変数を定義します */ int grades[3]; int average; grades[0] = 80; - /* TODO: define the missing grade - so that the average will sum to 85. */ + /* TODO: 欠落している成績を + 平均が85になるように定義します。 */ grades[1] = 85; grades[2] = 90; diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index 4a5bd5aef..99fd5f75f 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- ### Introduction @@ -14,16 +14,16 @@ A Binary Tree is a type of data structure in which each node has at most two chi The operations performed on trees requires searching in one of two main ways: Depth First Search and Breadth-first search. **Depth-first search (DFS)** is an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking. There are three types of depth first search traversal: **pre-order** visit, left, right, **in-order** left, visit, right, **post-order** left, right, visit. **Breadth-first search (BFS)** is an algorithm for traversing or searching tree or graph structures. In level-order, where we visit every node on a level before going to a lower level.
-Exercise --------- +演習 +---- Below is an implementation of a binary tree that has insertion and printing capabilities. This tree is ordered but not balanced. This example maintains its ordering at insertion time. Change the print routine to depth-first search **pre-order**. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -114,13 +114,13 @@ Tutorial Code } -Expected Output +期待している出力 --------------- 5 4 3 8 -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/Bitmasks.md b/tutorials/learn-c.org/ja/Bitmasks.md index 0eafb9cd3..47d66dbc0 100644 --- a/tutorials/learn-c.org/ja/Bitmasks.md +++ b/tutorials/learn-c.org/ja/Bitmasks.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Bit masking is simply the process of storing data truly as bits, as opposed to storing it as chars/ints/floats. It is incredibly useful for storing certain types of data compactly and efficiently. @@ -84,14 +84,14 @@ bit = storage & (1 << n); 00000000 00001000 ``` -Exercise --------- +演習 +---- Use bit masks to manipulate some flags. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -129,12 +129,12 @@ Tutorial Code } -Expected Output +期待している出力 --------------- Done! -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/Conditions.md b/tutorials/learn-c.org/ja/Conditions.md index a587d4d5d..ab9ff2cb4 100644 --- a/tutorials/learn-c.org/ja/Conditions.md +++ b/tutorials/learn-c.org/ja/Conditions.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- ### Decision Making @@ -99,15 +99,15 @@ The NOT operator `!` can also be used likewise: } -Exercise --------- +演習 +---- In this exercise, you must construct an `if` statement inside the `guessNumber` function statement that checks if the number `guess` is equal to 555. If that is the case, the function must print out using `printf` "Correct. You guessed it!". If `guess` is less than 555, the function must print out using `printf` "Your guess is too low." If `guess` is greater than 555, the function must print out using `printf` "Your guess is too high." * **Important**: Don't forget to add a newline character `\n` at the end of the printf string. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -121,15 +121,15 @@ int main() { guessNumber(555); } -Expected Output +期待している出力 --------------- Your guess is too low. Your guess is too high. Correct. You guessed it! -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Dynamic allocation.md b/tutorials/learn-c.org/ja/Dynamic allocation.md index 443f28903..cddb6542a 100644 --- a/tutorials/learn-c.org/ja/Dynamic allocation.md +++ b/tutorials/learn-c.org/ja/Dynamic allocation.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Dynamic allocation of memory is a very important subject in C. It allows building complex data structures such as linked lists. Allocating memory dynamically helps us to store data without initially knowing the size of the data in the time we wrote the program. @@ -33,13 +33,13 @@ After we are done using the dynamically allocated struct, we can release it usin Note that the free does not delete the `myperson` variable itself, it simply releases the data that it points to. The `myperson` variable will still point to somewhere in the memory - but after calling `free(myperson)` we are not allowed to access that area anymore. We must not use that pointer again until we allocate new data using it. -Exercise --------- +演習 +---- Use `malloc` to dynamically allocate a point structure. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -63,13 +63,13 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- mypoint coordinates: 10, 5 -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/For loops.md b/tutorials/learn-c.org/ja/For loops.md index cbb8eaebe..1582a5bcd 100644 --- a/tutorials/learn-c.org/ja/For loops.md +++ b/tutorials/learn-c.org/ja/For loops.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- For loops in C are straightforward. They supply the ability to create a loop - a code block that runs multiple times. For loops require an iterator variable, usually notated as `i`. @@ -33,13 +33,13 @@ the iterator `i` as the array index: /* sum now contains a[0] + a[1] + ... + a[9] */ printf("Sum of the array is %d\n", sum); -Exercise --------- +演習 +---- Calculate the factorial (multiplication of all items `array[0]` to `array[9]`, inclusive), of the variable `array`. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -53,13 +53,13 @@ Tutorial Code printf("10! is %d.\n", factorial); } -Expected Output +期待している出力 --------------- 10! is 3628800. -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Function Pointers.md b/tutorials/learn-c.org/ja/Function Pointers.md index 1bbe0e51b..08ad4e0dc 100644 --- a/tutorials/learn-c.org/ja/Function Pointers.md +++ b/tutorials/learn-c.org/ja/Function Pointers.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Remember pointers? We used them to point to an array of chars then make a string out of them. Then things got more interesting when we learned how to control these pointers. @@ -75,12 +75,12 @@ Let's remember again. Why do we use function pointers? 1. To allow programmers to use libraries for different usages -> "Flexibility" -Exercise --------- +演習 +---- Complete the array of pointers to functions and call each function using its pointer from the array. Array of pointers to functions? Yes you can do that! -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -119,15 +119,15 @@ Tutorial Code } -Expected Output +期待している出力 --------------- this is f1 and var is: 0 this is f2 and var is: 1 this is f3 and var is: 2 -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Function arguments by reference.md b/tutorials/learn-c.org/ja/Function arguments by reference.md index 2d7e5e29b..230840406 100644 --- a/tutorials/learn-c.org/ja/Function arguments by reference.md +++ b/tutorials/learn-c.org/ja/Function arguments by reference.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Assuming you now understand pointers and functions, you are aware that function arguments are passed by value, by which means they are copied in and out of functions. But what if we pass pointers to values instead of the values themselves? This will allow us to give functions control over the variables and structures of the parent functions and not just a copy of them, thus directly reading and writing the original object. @@ -48,13 +48,13 @@ However, if we wish to dereference a structure and access one of it's internal m p->y++; } -Exercise --------- +演習 +---- Write a function called `birthday`, which adds one to the `age` of a `person`. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -80,14 +80,14 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- John is 27 years old. Happy birthday! John is now 28 years old. -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Functions.md b/tutorials/learn-c.org/ja/Functions.md index 780c73099..6f0e8ed60 100644 --- a/tutorials/learn-c.org/ja/Functions.md +++ b/tutorials/learn-c.org/ja/Functions.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- C functions are simple, but because of how C works, the power of functions is a bit limited. @@ -53,16 +53,16 @@ We can also create functions that do not return a value by using the keyword `vo moo(); } -Exercise --------- +演習 +---- Write a function called `print_big` which receives one argument (an integer) and prints the line `x is big` (where x is the argument) if the argument given to the function is a number bigger than 10. * **Important**: Don't forget to add a newline character `\n` at the end of the printf string. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -80,15 +80,15 @@ Tutorial Code /* write your function here */ -Expected Output +期待している出力 --------------- 11 is big 22 is big 33 is big -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Hello, World!.md b/tutorials/learn-c.org/ja/Hello, World!.md index dac268015..43c3cf327 100644 --- a/tutorials/learn-c.org/ja/Hello, World!.md +++ b/tutorials/learn-c.org/ja/Hello, World!.md @@ -1,33 +1,17 @@ -Tutorial --------- - -### Introduction - -The C programming language is a general purpose programming language, which relates closely to the way machines work. -Understanding how computer memory works is an important aspect of the C programming language. Although C can be considered -as "hard to learn", C is in fact a very simple language, with very powerful capabilities. - -C is a very common language, and it is the language of many applications such as Windows, the Python interpreter, Git, and -many many more. - -C is a compiled language - which means that in order to run it, the compiler (for example, GCC or Visual Studio) must take the code that -we wrote, process it, and then create an executable file. This file can then be executed, and will do what we intended for the program -to do. - -C言語は汎用プログラミング言語であり、機械の動作と密接に関連しています。 -コンピュータメモリの仕組みを理解することは、C言語の重要な側面です。C言語は「習得が難しい」と考えられがちですが、実際には非常にシンプルな言語であり、非常に強力な機能を備えています。 +チュートリアル +------------- -C言語は非常に一般的な言語であり、Windows、Pythonインタープリター、Gitなど、多くのアプリケーションの言語となっています。 +### イントロダクション -C言語はコンパイル言語です。つまり、C言語を実行するには、コンパイラ(GCCやVisual Studioなど)が記述したコードを受け取り、処理して、実行ファイルを作成する必要があります。 -このファイルは実行可能であり、プログラムに期待した動作を実行します。 +C言語は汎用プログラミング言語であり、マシンの動作と密接に関連しています。 +コンピュータメモリの仕組みを理解することは、C言語の重要な側面です。C言語は「習得が難しい」と考えられがちですが、実際には非常にシンプルな言語であり、同時に非常に強力な機能を備えています。 -### Our first program +C言語はきわめて一般的な言語であり、Windows、Pythonインタープリター、Gitなど、多くのアプリケーションがC言語で開発されました。 -Every C program uses libraries, which give the ability to execute necessary functions. For example, the most basic function -called `printf`, which prints to the screen, is defined in the `stdio.h` header file. +C言語はコンパイル言語です。つまり、それを動かすには、コンパイラ(GCCやVisual Studioなど)が記述したコードを受け取り、処理して、実行ファイルを作成する必要があります。 +そうなって、ようやく、われわれはプログラムに「そう動いてほしい」と期待した動きを行わせることができます。 -To add the ability to run the `printf` command to our program, we must add the following include directive to our first line of the code: +### 最初のプログラム すべてのCプログラムは、必要な関数を実行するためのライブラリを使用します。例えば、画面に出力する最も基本的な関数である「printf」は、「stdio.h」ヘッダーファイルで定義されています。 @@ -35,44 +19,28 @@ To add the ability to run the `printf` command to our program, we must add the f #include -The second part of the code is the actual code which we are going to write. The first code which will run will always reside -in the `main` function. - -コードの2番目の部分は、実際に記述するコードです。最初に実行されるコードは、常に -`main` 関数内にあります。 +コードの2番目の部分は、実際に記述するコードです。最初に実行されるコードは、常に`main` 関数内にあります。 int main() { - ... our code goes here + ... ここにコードが書かれる } -The `int` keyword indicates that the function `main` will return an integer - a simple number. The number which will be returned -by the function indicates whether the program that we wrote worked correctly. If we want to say that our code -was run successfully, we will return the number 0. A number greater than 0 will mean that the program that we wrote failed. - `int` キーワードは、関数 `main` が整数、つまり単純な数値を返すことを示します。関数によって返される数値は、記述したプログラムが正しく動作したかどうかを示します。コードが正常に実行されたことを示す場合は、数値 0 を返します。0 より大きい数値は、記述したプログラムが失敗したことを意味します。 -For this tutorial, we will return 0 to indicate that our program was successful: - このチュートリアルでは、プログラムが成功したことを示すために 0 を返します。 return 0; -Notice that every statement in C must end with a semicolon, so that the compiler knows that a new statement has started. +C言語のすべての文はセミコロンで終わる必要があることに注意してください。これにより、コンパイラは次の文が始まったことを認識します。 -Last but not least, we will need to call the function `printf` to print our sentence. +最後になりましたが、重要なことです。文を出力するために関数「printf」を呼び出さなくてはなりません。 -C言語のすべての文はセミコロンで終わる必要があることに注意してください。これにより、コンパイラは新しい文が始まったことを認識します。 - -最後に、文を出力するために関数「printf」を呼び出す必要があります。 - -Exercise +演習 -------- -Change the program at the bottom so that it prints to the output "Hello, World!". - 下部のプログラムを変更して、出力に「Hello, World!」と表示されるようにします。 -Tutorial Code +チュートリアル コード ------------- #include @@ -82,12 +50,12 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- Hello, World! -Solution +解答 -------- #include diff --git a/tutorials/learn-c.org/ja/Linked lists.md b/tutorials/learn-c.org/ja/Linked lists.md index 4aa9c44c9..fa771e665 100644 --- a/tutorials/learn-c.org/ja/Linked lists.md +++ b/tutorials/learn-c.org/ja/Linked lists.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- ### Introduction @@ -237,14 +237,14 @@ There are a few edge cases we need to take care of, so make sure you understand } -Exercise --------- +演習 +---- You must implement the function `remove_by_value` which receives a double pointer to the head and removes the first item in the list which has the value `val`. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -300,15 +300,15 @@ Tutorial Code print_list(test_list); } -Expected Output +期待している出力 --------------- 1 2 4 -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/Multidimensional Arrays.md b/tutorials/learn-c.org/ja/Multidimensional Arrays.md index 8f251441f..d2139f425 100644 --- a/tutorials/learn-c.org/ja/Multidimensional Arrays.md +++ b/tutorials/learn-c.org/ja/Multidimensional Arrays.md @@ -1,7 +1,5 @@ -Tutorial --------- - -In the previous tutorials on [Arrays](https://www.learn-c.org/en/Arrays), we covered, well, arrays and how they work. The arrays we looked at were all one-dimensional, but C can create and use multi-dimensional arrays. Here is the general form of a multidimensional array declaration: +チュートリアル +------------- 前回の[配列](https://www.learn-c.org/en/Arrays)のチュートリアルでは、配列とその仕組みについて説明しました。これまで見てきた配列はすべて1次元でしたが、C言語では多次元配列を作成・使用できます。多次元配列の宣言の一般的な形式は次のとおりです。 @@ -21,42 +19,30 @@ or maybe this one - {'a', 'e', 'i', 'o', 'u'} }; -### Two-dimensional Arrays - -The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is pretty much a list of one-dimensional arrays. To declare a two-dimensional integer array of size [ x ][ y ], you would write something like this − +### 二次元配列 多次元配列の最も単純な形式は二次元配列です。二次元配列は、基本的に一次元配列のリストです。サイズが[ x ][ y ]の二次元整数配列を宣言するには、次のように記述します。 type arrayName [x][y]; -Where __type__ can be any C data type (int, char, long, long long, double, etc.) and __arrayName__ will be a valid C identifier, or variable. A two-dimensional array can be considered as a table which will have [ x ] number of rows and [ y ] number of columns. A two-dimensional array a, which contains three rows and four columns can be shown and thought about like this − - ここで、__type__ は任意の C データ型(int、char、long、long long、double など)で、__arrayName__ は有効な C 識別子または変数です。2 次元配列は、[ x ] 行と [ y ] 列を持つ表と考えることができます。3 行 4 列の 2 次元配列 a は、次のように表すことができます。 ![Table 1A](https://www.tutorialspoint.com/cprogramming/images/two_dimensional_arrays.jpg) -In this sense, every element in the array a is identified by an element name in the form __a[i][j]__, where 'a' is the name of the array, and 'i' and 'j' are the indexes that uniquely identify, or show, each element in 'a'. - 配列 a 内のすべての要素は、__a[i][j]__ という形式の要素名によって識別されます。ここで、'a' は配列の名前であり、'i' と 'j' は 'a' 内の各要素を一意に識別または表示するインデックスです。 -And honestly, you really don't have to put in a [ x ] value really, because if you did something like this - - -正直に言うと、[x]の値を入力する必要はありません。なぜなら、次のようにすれば、 +実際には、[x]の値を入力する必要はありません。なぜなら、次のようにすれば、 char vowels[][5] = { {'A', 'E', 'I', 'O', 'U'}, {'a', 'e', 'i', 'o', 'u'} }; -the compiler would already know that there are two "dimensions" you could say, but, you NEED a [ y ] value!! The compiler may be smart, but it _will not know_ how many integers, characters, floats, whatever you're using you have in the dimensions. Keep that in mind. - -既にコンパイラは2つの「次元」があることを認識しています。が、[ y ] 値は必須です。コンパイラは賢いかもしれませんが、その次元に整数、文字、浮動小数点数などが何個あるかは*認識*できません。この点にご注意ください。 +それだけで、コンパイラは2つの「次元」があることを認識しています。が、それでも[ y ] 値は必須です。コンパイラは賢いかもしれませんが、その次元に整数、文字、浮動小数点数などが何個あるかは*認識*できません。この点にご注意ください。 -### Initializing Two-Dimensional Arrays +### 二次元配列の初期化 -Multidimensional arrays may be used by specifying bracketed[] values for each row. Below is an array with 3 rows and each row has 4 columns. To make it easier, you can forget the 3 and keep it blank, it'll still work. - -多次元配列は、各行に括弧[]で囲まれた値を指定することで使用できます。以下は3行の配列で、各行に4列あります。わかりやすくするために、3を省略して空白のままにしても問題ありません。 +多次元配列は、各行に括弧[]で囲まれた値を指定することで使用できます。下の配列は3行で、各行に4列あります。楽がしたいなら、3を省略して空白のままにしても問題ありません。 int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ @@ -64,46 +50,34 @@ Multidimensional arrays may be used by specifying bracketed[] values for each ro {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; -The inside braces, which indicates the wanted row, are optional. The following initialization is the same to the previous example − - -必要な行を示す内側の括弧はオプションです。以下の初期化は前の例と同じです。 +必要な行を示す内側の括弧は、オプションです。なくてもかまいませんん。したがって、以下の初期化は、上の例と同じです。 int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; -### Accessing Two-Dimensional Array Elements - -An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example − +### 二次元配列の要素にアクセスする 二次元配列の要素には、添字、つまり配列の行インデックスと列インデックスを使ってアクセスします。例えば、 int val = a[2][3]; -The above statement will take the 4th element from the 3rd row of the array. - -上記のステートメントは、配列の 3 行目から 4 番目の要素を取得します。 +というステートメントは、配列の 3 行目で 4 列目の要素を取得します。 -Exercise --------- - -Let us try to find out the average marks of a group of five students for two subjects, Mathematics and Physics. To do this, we use a two-dimensional array called ```grades```. The marks corresponding to Mathematics would be stored in the first row (```grades[0]```), whereas those corresponding to Physics would be stored in the second row (```grades[1]```). Complete the following steps so that you can execute this program. +演習 +---- 数学と物理の2つの科目について、5人の生徒グループの平均点を調べてみましょう。そのためには、「grades」という2次元配列を使用します。数学の点数は1行目(「grades[0]」)に格納され、物理の点数は2行目(「grades[1]」)に格納されます。このプログラムを実行するには、以下の手順を実行してください。 -- Declare grades as a two-dimensional array of integers -- Complete the for loops by specifying their terminating conditions -- Compute the average marks obtained in each subject - - 成績を整数の2次元配列として宣言する - 終了条件を指定してforループを完了する - 各科目の平均点を計算する -Tutorial Code -------------- +チュートリアル コード +------------------- #include int main() { - /* TODO: declare the 2D array grades here */ + /* TODO: ここで2次元配列の grades を宣言します */ float average; int i; int j; @@ -120,14 +94,14 @@ Tutorial Code grades[1][3] = 82; grades[1][4] = 87; - /* TODO: complete the for loop with appropriate terminating conditions */ + /* TODO: 適切な終了条件で for ループを完了する */ for (i = 0; i < ; i++) { average = 0; for (j = 0; j < ; j++) { average += grades[i][j]; } - /* TODO: compute the average marks for subject i */ + /* TODO: 科目 i の平均点を計算する */ printf("The average marks obtained in subject %d is: %.2f\n", i, average); } @@ -135,14 +109,14 @@ Tutorial Code } -Expected Output +期待している出力 --------------- The average marks obtained in subject 0 is: 78.80 The average marks obtained in subject 1 is: 82.80 -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Pointer Arithmetics.md b/tutorials/learn-c.org/ja/Pointer Arithmetics.md index fbdeced0c..616487623 100644 --- a/tutorials/learn-c.org/ja/Pointer Arithmetics.md +++ b/tutorials/learn-c.org/ja/Pointer Arithmetics.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- You previously learned what is a pointer and how to manipulate pointers. In this tutorial you will be learning the arithmetic operations on pointers. There are multiple arithmetic operations that can be applied on C pointers: ++, --, -, + @@ -109,12 +109,12 @@ again the address is shifted by blocks of 4bytes (in case of int). ### Other Operations There are more operations such as comparison >, <, ==. The idea is very similar of comparing variables, but in this case we are comparing memory address. -Exercise --------- +演習 +---- Copy last three addresses of intarray into parray which is an array of pointers to an int. -Tutorial Code -------------- +チュートリアル コード +------------------- #include int main() { @@ -145,14 +145,14 @@ Tutorial Code } -Expected Output +期待している出力 --------------- Matched! Matched! Matched! -Solution --------- +解答 +---- #include int main() { diff --git a/tutorials/learn-c.org/ja/Pointers.md b/tutorials/learn-c.org/ja/Pointers.md index bdc174b1e..131b00c2e 100644 --- a/tutorials/learn-c.org/ja/Pointers.md +++ b/tutorials/learn-c.org/ja/Pointers.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Pointers are also variables and play a very important role in C programming language. They are used for several reasons, such as: @@ -67,13 +67,13 @@ We then referred to it using the dereferencing operator. We can also change the /* will print out 3 */ printf("The value of a is now %d\n", a); -Exercise --------- +演習 +---- Create a pointer to the local variable `n` called `pointer_to_n`, and use it to increase the value of `n` by one. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -90,13 +90,13 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- Done! -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Recursion.md b/tutorials/learn-c.org/ja/Recursion.md index a3aac5352..3bfd26adf 100644 --- a/tutorials/learn-c.org/ja/Recursion.md +++ b/tutorials/learn-c.org/ja/Recursion.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- Recursion occurs when a function contains within it a call to itself. Recursion can result in very neat, elegant code that is intuitive to follow. It can also result in a very large amount of memory being used if the recursion gets too deep. @@ -36,13 +36,13 @@ For example, this function will perform multiplication by recursively adding : return 0; } -Exercise --------- +演習 +---- Define a new function called `factorial()` that will compute the factorial by recursive multiplication (5! = 5 x 4 x 3 x 2 x 1). Note that by convention, the factorial of 0 is equal to 1 (0! = 1). -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -56,7 +56,7 @@ Tutorial Code /* define your function here (don't forget to declare it) */ -Expected Output +期待している出力 --------------- 0! = 1 @@ -64,8 +64,8 @@ Expected Output 3! = 6 5! = 120 -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Static.md b/tutorials/learn-c.org/ja/Static.md index 076844519..05e5c7541 100644 --- a/tutorials/learn-c.org/ja/Static.md +++ b/tutorials/learn-c.org/ja/Static.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- `static` is a keyword in the C programming language. It can be used with variables and functions. ### What is a static variable? @@ -50,12 +50,12 @@ The syntax looks like this: ### Static vs Global? While static variables have scope over the file containing them making them accessible only inside a given file, global variables can be accessed outside the file too. -Exercise --------- +演習 +---- In this exercise, try to find the sum of some numbers by using the static keyword. Do not pass any variable representing the running total to the `sum()` function. -Tutorial Code -------------- +チュートリアル コード +------------------- #include int sum (int num) { @@ -71,13 +71,13 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- 55 100 150 -Solution --------- +解答 +---- #include int sum (int num) { diff --git a/tutorials/learn-c.org/ja/Strings.md b/tutorials/learn-c.org/ja/Strings.md index 676ba77e6..07bf77322 100644 --- a/tutorials/learn-c.org/ja/Strings.md +++ b/tutorials/learn-c.org/ja/Strings.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- ### Defining strings @@ -69,14 +69,14 @@ The arguments passed are destination string, source string, and n - maximum numb strncat(dest,src,20); printf("%s\n",dest); -Exercise --------- +演習 +---- Define the string `first_name` with the value `John` using the pointer notation, and define the string `last_name` with the value `Doe` using the local array notation. -Tutorial Code -------------- +チュートリアル コード +------------------- #include #include @@ -98,14 +98,14 @@ Tutorial Code } -Expected Output +期待している出力 --------------- Done! JohnBoe -Solution --------- +解答 +---- #include #include diff --git a/tutorials/learn-c.org/ja/Structures.md b/tutorials/learn-c.org/ja/Structures.md index 4e971ddbe..16f1b8d2f 100644 --- a/tutorials/learn-c.org/ja/Structures.md +++ b/tutorials/learn-c.org/ja/Structures.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- C structures are special, large variables which contain several named variables inside. Structures are the basic foundation for objects and classes in C. Structures are used for: @@ -57,13 +57,13 @@ Since brand is a char pointer, the vehicle type can contain a string (which, in mycar.brand = "Ford"; mycar.model = 2007; -Exercise --------- +演習 +---- Define a new data structure, named "person", which contains a string (pointer to char) called `name`, and an integer called `age`. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -78,13 +78,13 @@ Tutorial Code printf("%s is %d years old.", john.name, john.age); } -Expected Output +期待している出力 --------------- John is 27 years old. -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Unions.md b/tutorials/learn-c.org/ja/Unions.md index e3615fa87..82e35a0b4 100644 --- a/tutorials/learn-c.org/ja/Unions.md +++ b/tutorials/learn-c.org/ja/Unions.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- C Unions are essentially the same as C Structures, except that instead of containing multiple variables each with their own memory a Union allows for multiple names to the same variable. These names can treat the memory as different types (and the size of the union will be the size of the largest type, + any padding the compiler might decide to give it) @@ -100,13 +100,13 @@ since the union makes the variables share the same memory the coins array matche change.quarter, change.dime, change.nickel, change.penny); -Exercise --------- +演習 +---- Create a union that stores an array of 21 characters and 6 ints (6 since 21 / 4 == 5, but 5 * 4 == 20 so you need 1 more for the purpose of this exercise), you will set the integers to 6 given values and then print out the character array both as a series of chars and as a string. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -128,14 +128,14 @@ Tutorial Code printf("%s\n", intCharacters.chars); } -Expected Output +期待している出力 --------------- [I, , u, n, d, e, r, s, t, a, n, d, , U, n, i, o, n, s, !] I understand Unions! -Solution --------- +解答 +---- #include diff --git a/tutorials/learn-c.org/ja/Variables and Types.md b/tutorials/learn-c.org/ja/Variables and Types.md index 447bc72eb..3374494a8 100644 --- a/tutorials/learn-c.org/ja/Variables and Types.md +++ b/tutorials/learn-c.org/ja/Variables and Types.md @@ -1,20 +1,9 @@ -Tutorial --------- - -### Data types - -C has several types of variables, but there are a few basic types: - -* Integers - whole numbers which can be either positive or negative. Defined using `char`, `int`, `short`, `long` or `long long`. -* Unsigned integers - whole numbers which can only be positive. Defined using `unsigned char`, `unsigned int`, `unsigned short`, `unsigned long` or `unsigned long long`. -* Floating point numbers - real numbers (numbers with fractions). Defined using `float` and `double`. -* Structures - will be explained later, in the Structures section. - -The different types of variables define their bounds. A `char` can range only from -128 to 127, whereas a `long` can range from -2,147,483,648 to 2,147,483,647 (`long` and other numeric data types may have another range on different computers, for example - from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer). +チュートリアル +------------- -Note that C does _not_ have a boolean type. Usually, it is defined using the following notation: +### データ型 -C言語にはいろいろな変数型がありますが、基本的な型はわずかです。 +C言語にはいろいろな変数型がありますが、基本的な型はほんの少しです。 * 整数 - 正または負の値をとる整数。`char`、`int`、`short`、`long`、`long long` のいずれかで定義されます。 * 符号なし整数 - 正の値しかとれない整数。`unsigned char`、`unsigned int`、`unsigned short`、`unsigned long`、`unsigned long long` のいずれかで定義されます。 @@ -29,15 +18,9 @@ C にはブール型が存在しないことに注意してください。通常 #define FALSE 0 #define TRUE 1 -C uses arrays of characters to define strings, and will be explained in the Strings section. - C では文字の配列を使用して文字列を定義します。これについては文字列のセクションで説明します。 -### Defining variables - -For numbers, we will usually use the type `int`. On most computers today, it is a 32-bit number, which means the number can range from -2,147,483,648 to 2,147,483,647. - -To define the variables `foo` and `bar`, we need to use the following syntax: +### 変数を定義する 数値の場合、通常は `int` 型を使用します。今日のほとんどのコンピュータでは、これは 32 ビット数値であり、数値の範囲は -2,147,483,648 から 2,147,483,647 です。 @@ -46,29 +29,22 @@ To define the variables `foo` and `bar`, we need to use the following syntax: int foo; int bar = 1; -The variable `foo` can be used, but since we did not initialize it, we don't know what's in it. The variable `bar` contains the number 1. - -Now, we can do some math. Assuming `a`, `b`, `c`, `d`, and `e` are variables, we can simply use plus, minus and multiplication operators -in the following notation, and assign a new value to `a`: - -変数「foo」は使用できますが、初期化していないため、その内容はわかりません。変数「bar」には数値「1」が格納されています。 +変数「foo」は使用できますが、初期化していないため、その内容を知ることはできません。変数「bar」には数値「1」が格納されています。 さて、計算してみましょう。「a」、「b」、「c」、「d」、「e」を変数と仮定すると、以下の記法で加算、減算、乗算演算子を使用し、 「a」に新しい値を代入します。 int a = 0, b = 1, c = 2, d = 3, e = 4; a = b - c + d * e; - printf("%d", a); /* will print 1-2+3*4 = 11 */ - -Exercise --------- + printf("%d", a); /* 1-2+3*4 = 11 が出力されます */ -In the next exercise, you will need to create a program which prints out the sum of the numbers `a`, `b`, and `c`. +演習 +---- 次の演習では、数値 `a`、`b`、`c` の合計を出力するプログラムを作成する必要があります。 -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -84,12 +60,12 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- The sum of a, b, and c is 12.750000. -Solution --------- +解答 +---- #include int main() { diff --git a/tutorials/learn-c.org/ja/While loops.md b/tutorials/learn-c.org/ja/While loops.md index 26c66fa3c..28eb2808e 100644 --- a/tutorials/learn-c.org/ja/While loops.md +++ b/tutorials/learn-c.org/ja/While loops.md @@ -1,5 +1,5 @@ -Tutorial --------- +チュートリアル +------------- While loops are similar to for loops, but have less functionality. A while loop continues executing the while block as long as the condition in the while remains true. For example, the following code will execute exactly ten times: @@ -45,8 +45,8 @@ In the following code, the `continue` directive causes the `printf` command to b printf("The number %d is even.\n", n); } -Exercise --------- +演習 +---- The `array` variable consists of a sequence of ten numbers. Inside the while loop, you must write two `if` conditions, which change the flow of the loop in the following manner (without changing the `printf` command): @@ -56,8 +56,8 @@ which change the flow of the loop in the following manner (without changing the Notice that if you do not advance the iterator variable `i` and use the `continue` derivative, you will get stuck in an infinite loop. -Tutorial Code -------------- +チュートリアル コード +------------------- #include @@ -75,7 +75,7 @@ Tutorial Code return 0; } -Expected Output +期待している出力 --------------- 7 @@ -83,8 +83,8 @@ Expected Output 9 5 -Solution --------- +解答 +---- #include From 60c7cc53be42098b782933c7983f353559349292 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Wed, 26 Nov 2025 21:04:27 +0900 Subject: [PATCH 03/20] =?UTF-8?q?=E5=9F=BA=E7=A4=8E=E7=B7=A8=E3=81=AE?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E7=B5=82=E3=82=8F=E3=82=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Conditions.md | 83 +++++++++++++------------ tutorials/learn-c.org/ja/For loops.md | 25 +++++--- tutorials/learn-c.org/ja/Functions.md | 42 ++++++------- tutorials/learn-c.org/ja/Static.md | 31 +++++---- tutorials/learn-c.org/ja/Strings.md | 50 +++++++-------- tutorials/learn-c.org/ja/While loops.md | 31 +++++---- 6 files changed, 134 insertions(+), 128 deletions(-) diff --git a/tutorials/learn-c.org/ja/Conditions.md b/tutorials/learn-c.org/ja/Conditions.md index ab9ff2cb4..bda52dc6f 100644 --- a/tutorials/learn-c.org/ja/Conditions.md +++ b/tutorials/learn-c.org/ja/Conditions.md @@ -1,12 +1,11 @@ チュートリアル ------------- -### Decision Making +### 決断を下す -In life, we all have to make decisions. In order to make a decision we weigh out our options and so do our programs. - -Here is the general form of the decision making structures found in C. +人生において、私たちは皆、決断を下さなければなりません。決断を下すために、私たちは選択肢を検討し、計画も練り上げます。 +以下は、C 言語に見られる『決断を下す』構造の一般的な形式です。 int target = 10; if (target == 10) { @@ -14,13 +13,13 @@ Here is the general form of the decision making structures found in C. } -### The `if` statement +### `if`文 -The `if` statement allows us to check if an expression is `true` or `false`, and execute different code according to the result. +`if` 文を使うと、式が `true` か `false` かを確認し、結果に応じて異なるコードを実行できます。 -To evaluate whether two variables are equal, the `==` operator is used, just like in the first example. +2つの変数が等しいかどうかを評価するには、最初の例のように `==` 演算子を使用します。 -Inequality operators can also be used to evaluate expressions. For example: +不等号演算子も式の評価に使用できます。例: int foo = 1; int bar = 2; @@ -33,7 +32,7 @@ Inequality operators can also be used to evaluate expressions. For example: printf("foo is greater than bar."); } -We can use the `else` keyword to execute code when our expression evaluates to `false`. +式が `false` と評価された場合にコードを実行するには、 `else` キーワードを使用できます。 int foo = 1; int bar = 2; @@ -44,7 +43,7 @@ We can use the `else` keyword to execute code when our expression evaluates to ` printf("foo is greater than bar."); } -Sometimes we will have more than two outcomes to choose from. In these cases, we can "chain" multiple `if` `else` statements together. +場合によっては、2つ以上の結果から選択する必要があります。このような場合は、複数の `if` `else` 文を「連結」することができます。 int foo = 1; int bar = 2; @@ -57,7 +56,7 @@ Sometimes we will have more than two outcomes to choose from. In these cases, we printf("foo is greater than bar."); } -You can also nest `if` `else` statements if you like. +必要に応じて、`if` `else` ステートメントをネストすることもできます。 int peanuts_eaten = 22; int peanuts_in_jar = 100; @@ -77,7 +76,7 @@ You can also nest `if` `else` statements if you like. } -Two or more expressions can be evaluated together using logical operators to check if two expressions evaluate to `true` together, or at least one of them. To check if two expressions both evaluate to `true`, use the AND operator `&&`. To check if at least one of the expressions evaluate to `true`, use the OR operator `||`. +論理演算子を使用して2つ以上の式をまとめて評価することで、『2つの式が両方とも「true」と評価されるか』『または少なくともどちらか一方が「true」と評価されるか』を確認できます。2つの式が両方とも「true」と評価されるかどうかを確認するには、AND演算子「&&」を使用します。少なくとも一方の式が「true」と評価されるかどうかを確認するには、OR演算子「||」をを使用します。 int foo = 1; int bar = 2; @@ -93,6 +92,8 @@ Two or more expressions can be evaluated together using logical operators to che The NOT operator `!` can also be used likewise: +NOT 演算子 `!` も同様に使用できます。 + int target = 9; if (target != 10) { printf("Target is not equal to 10"); @@ -102,50 +103,50 @@ The NOT operator `!` can also be used likewise: 演習 ---- -In this exercise, you must construct an `if` statement inside the `guessNumber` function statement that checks if the number `guess` is equal to 555. If that is the case, the function must print out using `printf` "Correct. You guessed it!". If `guess` is less than 555, the function must print out using `printf` "Your guess is too low." If `guess` is greater than 555, the function must print out using `printf` "Your guess is too high." +この演習では、`guessNumber` 関数ステートメント内に、数値 `guess` が 555 に等しいかどうかを確認する `if` ステートメントを作成する必要があります。等しい場合、関数は `printf` を使用して「正解です。お察しの通りです!」と出力する必要があります。`guess` が 555 未満の場合、関数は `printf` を使用して「あなたの推測は低すぎます。」と出力する必要があります。`guess` が 555 より大きい場合、関数は `printf` を使用して「あなたの推測は高すぎます。」と出力する必要があります。 -* **Important**: Don't forget to add a newline character `\n` at the end of the printf string. +* **重要**: printf 文字列の最後に改行文字 `\n` を追加することを忘れないでください。 チュートリアル コード ------------------- -#include + #include -void guessNumber(int guess) { - // TODO: write your code here -} + void guessNumber(int guess) { + // TODO: ここにコードを記述してください + } -int main() { - guessNumber(500); - guessNumber(600); - guessNumber(555); -} + int main() { + guessNumber(500); + guessNumber(600); + guessNumber(555); + } 期待している出力 --------------- -Your guess is too low. -Your guess is too high. -Correct. You guessed it! + Your guess is too low. + Your guess is too high. + Correct. You guessed it! 解答 ---- -#include + #include -void guessNumber(int guess) { - // TODO: write your code here - if (guess < 555) { - printf("Your guess is too low.\n"); - } else if (guess > 555) { - printf("Your guess is too high.\n"); - } else { - printf("Correct. You guessed it!\n"); + void guessNumber(int guess) { + // TODO: ここにコードを記述してください + if (guess < 555) { + printf("Your guess is too low.\n"); + } else if (guess > 555) { + printf("Your guess is too high.\n"); + } else { + printf("Correct. You guessed it!\n"); + } } -} -int main() { - guessNumber(500); - guessNumber(600); - guessNumber(555); -} + int main() { + guessNumber(500); + guessNumber(600); + guessNumber(555); + } diff --git a/tutorials/learn-c.org/ja/For loops.md b/tutorials/learn-c.org/ja/For loops.md index 1582a5bcd..c1e0f8d62 100644 --- a/tutorials/learn-c.org/ja/For loops.md +++ b/tutorials/learn-c.org/ja/For loops.md @@ -1,16 +1,16 @@ チュートリアル ------------- -For loops in C are straightforward. They supply the ability to create a loop - a code block that runs multiple times. -For loops require an iterator variable, usually notated as `i`. +C言語のforループはシンプルです。ループとは、複数回実行されるコードブロックのことです。 +forループには、通常「i」と表記されるイテレータ変数が必要です。 -For loops give the following functionality: +forループには以下の機能があります。 -* Initialize the iterator variable using an initial value -* Check if the iterator has reached its final value -* Increases the iterator +* 初期値を使用してイテレータ変数を初期化する +* イテレータが最終値に達したかどうかを確認する +* イテレータの値を増やす -For example, if we wish to iterate on a block for 10 times, we write: +例えば、あるブロックを10回繰り返し処理したい場合は、次のように記述します。 int i; for (i = 0; i < 10; i++) { @@ -22,6 +22,11 @@ This block will print the numbers 0 through 9 (10 numbers in total). For loops can iterate on array values. For example, if we would want to sum all the values of an array, we would use the iterator `i` as the array index: +このブロックは0から9までの数字(合計10個)を出力します。 + +forループは配列の値を、繰り返し処理できます。例えば、配列のすべての値を合計したい場合は、 +イテレータ「i」を配列のインデックスとして使用します。 + int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; int i; @@ -30,13 +35,13 @@ the iterator `i` as the array index: sum += array[i]; } - /* sum now contains a[0] + a[1] + ... + a[9] */ + /* 合計には a[0] + a[1] + ... + a[9] が含まれます */ printf("Sum of the array is %d\n", sum); 演習 ---- -Calculate the factorial (multiplication of all items `array[0]` to `array[9]`, inclusive), of the variable `array`. +変数 `array` の階乗(`array[0]` から `array[9]` までのすべての項目の乗算)を計算します。 チュートリアル コード ------------------- @@ -48,7 +53,7 @@ Calculate the factorial (multiplication of all items `array[0]` to `array[9]`, i int factorial = 1; int i; - /* calculate the factorial using a for loop here*/ + /* ここでforループを使用して階乗を計算します*/ printf("10! is %d.\n", factorial); } diff --git a/tutorials/learn-c.org/ja/Functions.md b/tutorials/learn-c.org/ja/Functions.md index 6f0e8ed60..6cff6426c 100644 --- a/tutorials/learn-c.org/ja/Functions.md +++ b/tutorials/learn-c.org/ja/Functions.md @@ -1,18 +1,18 @@ チュートリアル ------------- -C functions are simple, but because of how C works, the power of functions is a bit limited. +C関数はシンプルですが、C言語の仕組み上、その威力には多少の限界があります。 -* Functions receive either a fixed or variable amount of arguments. -* Functions can only return one value, or return no value. +* 関数は、固定数または可変数の引数を受け取ります。 +* 関数は1つの値のみを返すか、値を返さないかのいずれかです。 -In C, arguments are copied by value to functions, which means that we cannot change the arguments to affect their value outside of -the function. To do that, we must use pointers, which are taught later on. +C言語では、引数は関数に値としてコピーされます。つまり、関数外で引数の値を変更して変更することはできません。 +そのためには、後ほど説明するポインタを使用する必要があります。 -Functions are defined using the following syntax: +関数は以下の構文で定義されます。 int foo(int bar) { - /* do something */ + /* 何かをする */ return bar * 2; } @@ -20,22 +20,21 @@ Functions are defined using the following syntax: foo(1); } -The function `foo` we defined receives one argument, which is `bar`. The function receives an integer, multiplies it by two, and returns the result. +定義した関数 `foo` は、`bar` という1つの引数を受け取ります。受け取った整数をを2倍して結果を返します。 -To execute the function `foo` with 1 as the argument `bar`, we use the following syntax: +引数 `bar` に1を指定して関数 `foo` を実行するには、次の構文を使用します。 foo(1); -In C, functions must be first defined before they are used in the code. They can be either declared first and then implemented later on using a -header file or in the beginning of the C file, or they can be implemented in the order they are used (less preferable). +C言語では、関数はコード内で使用される前に定義される必要があります。関数は最初に宣言し、後でヘッダーファイルやC言語ファイルの先頭で実装します。また、使用される順序で実装することもできます(こちらはあまり推奨されません)。 -The correct way to use functions is as follows: +関数の正しい使用方法は次のとおりです。 - /* function declaration */ + /* 関数宣言 */ int foo(int bar); int main() { - /* calling foo from main */ + /* main から foo を呼ぶ */ printf("The value of foo is %d", foo(1)); } @@ -43,10 +42,10 @@ The correct way to use functions is as follows: return bar + 1; } -We can also create functions that do not return a value by using the keyword `void`: +キーワード `void` を使用して、値を返さない関数を作成することもできます。 void moo() { - /* do something and don't return a value */ + /* 何かを実行するが、値を返さない */ } int main() { @@ -56,17 +55,16 @@ We can also create functions that do not return a value by using the keyword `vo 演習 ---- -Write a function called `print_big` which receives one argument (an integer) and prints the line `x is big` (where x is the argument) if the argument given -to the function is a number bigger than 10. +`print_big` という関数を作成してください。この関数は、引数(整数)を1つ受け取り、引数が10より大きい数値の場合に `x is big`(x は引数)という行を出力します。 -* **Important**: Don't forget to add a newline character `\n` at the end of the printf string. +* **重要**: printf 文字列の末尾に改行文字 `\n` を追加することを忘れないでください。 チュートリアル コード ------------------- #include - /* function declaration */ + /* 関数宣言 */ void print_big(int number); int main() { @@ -78,7 +76,7 @@ to the function is a number bigger than 10. return 0; } - /* write your function here */ + /* あなたの関数をここに書く */ 期待している出力 --------------- @@ -92,7 +90,7 @@ to the function is a number bigger than 10. #include - /* function declaration */ + /* 関数宣言 */ void print_big(int number); int main() { diff --git a/tutorials/learn-c.org/ja/Static.md b/tutorials/learn-c.org/ja/Static.md index 05e5c7541..62f99267f 100644 --- a/tutorials/learn-c.org/ja/Static.md +++ b/tutorials/learn-c.org/ja/Static.md @@ -1,11 +1,15 @@ チュートリアル ------------- -`static` is a keyword in the C programming language. It can be used with variables and functions. -### What is a static variable? -By default, variables are local to the scope in which they are defined. Variables can be declared as static to increase their scope up to file containing them. As a result, these variables can be accessed anywhere inside a file. +`static` はCプログラミング言語のキーワードです。変数や関数で使用できます。 -Consider the following scenario – where we want to count the runners participating in a race: +しかし、同じ`static`でも、変数と関数で、まったく効果が異なる点は、注意が必要です。 + +### static変数とは? + +変数はデフォルトではローカル変数で、それが定義されたスコープ内でのみ有効です。しかし、変数をstatic変数として宣言することで、その変数を含むファイル全体にスコープを拡張できます。その結果、これらの変数はファイル内のどこからでもアクセスできるようになります。 + +レースに参加するランナーの数をカウントしたいという次のシナリオを考えてみましょう。 #include int runner() { @@ -21,7 +25,7 @@ Consider the following scenario – where we want to count the runners participa return 0; } -We will see that `count` is not updated because it is removed from memory as soon as the function completes. If `static` is used however, we get the desired result: +`count` は関数の完了と同時にメモリから削除されるので、継続してカウントアップされることはありません。しかし、`static` を使用すると、期待通りの結果が得られます。 #include int runner() @@ -38,21 +42,24 @@ We will see that `count` is not updated because it is removed from memory as soo return 0; } -### What is a static function? -By default, functions are global in C. If we declare a function with `static`, the scope of that function is reduced to the file containing it. +### static関数とは? -The syntax looks like this: +C言語では、関数はデフォルトでグローバルです。しかし、`static` を使って関数を宣言すると、その関数のスコープはその関数を含むファイル内に縮小されます。 + +構文は次のようになります。 static void fun(void) { printf("I am a static function."); } -### Static vs Global? -While static variables have scope over the file containing them making them accessible only inside a given file, global variables can be accessed outside the file too. +### static変数とグローバル変数の違い + +static変数は、その変数を含むファイル内でのみアクセス可能なため、特定のファイル内でのみアクセス可能です。一方、グローバル変数はファイル外からもアクセスできます。 演習 ---- -In this exercise, try to find the sum of some numbers by using the static keyword. Do not pass any variable representing the running total to the `sum()` function. + +この演習では、staticキーワードを使っていくつかの数値の合計を計算してみましょう。`sum()`関数には、累計を表す変数を渡さないでください。 チュートリアル コード ------------------- @@ -60,7 +67,7 @@ In this exercise, try to find the sum of some numbers by using the static keywor #include int sum (int num) { /** - * find sum to n numbers + * * n 個の数値の合計を求める */ } diff --git a/tutorials/learn-c.org/ja/Strings.md b/tutorials/learn-c.org/ja/Strings.md index 07bf77322..25535ac99 100644 --- a/tutorials/learn-c.org/ja/Strings.md +++ b/tutorials/learn-c.org/ja/Strings.md @@ -1,53 +1,50 @@ チュートリアル ------------- -### Defining strings +### 文字列の定義 -Strings in C are actually arrays of characters. Although using pointers in C is an advanced subject, fully explained later on, we will use pointers to a character array to define simple strings, in the following manner: +C言語における文字列は、実際には文字の配列です。C言語におけるポインタの使用は高度なテーマであり、後ほど詳しく説明しますが、ここでは文字配列へのポインタを使用して、単純な文字列を定義します。その方法は以下のとおりです。 char * name = "John Smith"; -This method creates a string which we can only use for reading. -If we wish to define a string which can be manipulated, we will need to define it as a local character array: +このメソッドは、読み取り専用の文字列を作成します。 +操作可能な文字列を定義したい場合は、ローカル文字配列として定義する必要があります。 char name[] = "John Smith"; -This notation is different because it allocates an array variable so we can manipulate it. The empty brackets notation `[]` tells the -compiler to calculate the size of the array automatically. This is in fact the same as allocating it explicitly, adding one to -the length of the string: +この表記法は、配列変数を割り当てて操作できるようにするという点で異なります。空の括弧表記 `[]` は、コンパイラに配列のサイズを自動的に計算するように指示します。これは実際には、明示的に割り当てて文字列の長さに1を加算するのと同じです。 char name[] = "John Smith"; /* is the same as */ char name[11] = "John Smith"; -The reason that we need to add one, although the string `John Smith` is exactly 10 characters long, is for the string termination: -a special character (equal to 0) which indicates the end of the string. The end of the string is marked because the program -does not know the length of the string - only the compiler knows it according to the code. +文字列「John Smith」の長さはちょうど10文字ですが、1文字追加する必要があるのは、文字列の終端を示すためです。 +文字列の終端を示す特殊文字(0に等しい)です。文字列の終端を示すのは、プログラムが文字列の長さを把握していないためです。 +コードから判断して、コンパイラのみが文字列の長さを把握します。 -### String formatting with printf +### printfによる文字列のフォーマット -We can use the `printf` command to format a string together with other strings, in the following manner: +次のように、`printf` コマンドを使用して文字列を他の文字列と一緒にフォーマットすることができます。 char * name = "John Smith"; int age = 27; - /* prints out 'John Smith is 27 years old.' */ + /* 'John Smith は 27 歳です。' と出力します。 */ printf("%s is %d years old.\n", name, age); -Notice that when printing strings, we must add a newline (`\n`) character so that our next `printf` statement will print in a new line. +文字列を印刷するとき、次の `printf` ステートメントが新しい行に印刷されるように、改行文字 (`\n`) を追加する必要があることに注意してください。 -### String Length +### 文字列の長さ -The function 'strlen' returns the length of the string which has to be passed as an argument: +関数 'strlen' は、引数として渡す必要のある文字列の長さを返します。 char * name = "Nikhil"; printf("%d\n",strlen(name)); -### String comparison +### 文字列の比較 -The function `strncmp` compares between two strings, returning the number 0 if they are equal, or a different number if they are different. -The arguments are the two strings to be compared, and the maximum comparison length. There is also an unsafe version of this function -called `strcmp`, but it is not recommended to use it. For example: +関数 `strncmp` は2つの文字列を比較し、等しい場合は数値 0 を、異なる場合は異なる数値を返します。 +引数は、比較する2つの文字列と比較する文字列の最大長です。この関数には安全でないバージョンである `strcmp` も存在しますが、使用は推奨されません。例: char * name = "John"; @@ -57,10 +54,10 @@ called `strcmp`, but it is not recommended to use it. For example: printf("You are not John. Go away.\n"); } -### String Concatenation +### 文字列の連結 -The function 'strncat' appends first n characters of src string to the destination string where n is min(n,length(src)); -The arguments passed are destination string, source string, and n - maximum number of characters to be appended. For Example: +関数 'strncat' は、src 文字列の最初の n 文字を destination 文字列に追加します。ここで n は min(n,length(src)) です。 +渡される引数は、 destination 文字列、source 文字列、および n です。n は追加する最大文字数です。例: char dest[20]="Hello"; char src[20]="World"; @@ -72,8 +69,7 @@ The arguments passed are destination string, source string, and n - maximum numb 演習 ---- -Define the string `first_name` with the value `John` using the pointer notation, and define the string `last_name` with the value `Doe` -using the local array notation. +ポインタ表記法を使用して、文字列 `first_name` を値 `John` で定義し、ローカル配列表記法を使用して、文字列 `last_name` を値 `Doe` で定義します。 チュートリアル コード ------------------- @@ -81,8 +77,8 @@ using the local array notation. #include #include int main() { - /* define first_name */ - /* define last_name */ + /* first_name を定義 */ + /* last_name を定義 */ char name[100]; last_name[0] = 'B'; diff --git a/tutorials/learn-c.org/ja/While loops.md b/tutorials/learn-c.org/ja/While loops.md index 28eb2808e..3f538b465 100644 --- a/tutorials/learn-c.org/ja/While loops.md +++ b/tutorials/learn-c.org/ja/While loops.md @@ -1,25 +1,24 @@ チュートリアル ------------- -While loops are similar to for loops, but have less functionality. A while loop continues executing the while block as long as the -condition in the while remains true. For example, the following code will execute exactly ten times: +whileループはforループに似ていますが、機能は少なくなっています。whileループは、while内の条件が真である限り、whileブロックの実行を継続します。例えば、次のコードはちょうど10回実行されます。 int n = 0; while (n < 10) { n++; } -While loops can also execute infinitely if a condition is given which always evaluates as true (non-zero): +常に true (ゼロ以外) と評価される条件が指定されている場合、while ループは無限に実行することもできます。 while (1) { /* do something */ } -### Loop directives +### ループ命令文 -There are two important loop directives that are used in conjunction with all loop types in C - the `break` and `continue` directives. +C言語には、あらゆるループタイプで併用される重要なループ命令文が2つあります。それは`break` 命令文と`continue` 命令文です。 -The `break` directive halts a loop after ten loops, even though the while loop never finishes: +下の「break」命令文は、whileループが終了しない場合でも、ループを10回実行した後に停止します。 int n = 0; while (1) { @@ -29,32 +28,32 @@ The `break` directive halts a loop after ten loops, even though the while loop n } } -In the following code, the `continue` directive causes the `printf` command to be skipped, so that only even numbers are printed out: +次のコードでは、`continue` 命令文によって `printf` コマンドがスキップされ、偶数のみが出力されます。 int n = 0; while (n < 10) { n++; - /* check that n is odd */ + /* nが奇数であることを確認する */ if (n % 2 == 1) { - /* go back to the start of the while block */ + /* whileブロックの先頭に戻る */ continue; } - /* we reach this code only if n is even */ + /* nが偶数の場合にのみこのコードに到達する */ printf("The number %d is even.\n", n); } 演習 ---- -The `array` variable consists of a sequence of ten numbers. Inside the while loop, you must write two `if` conditions, -which change the flow of the loop in the following manner (without changing the `printf` command): +`array` 変数は 10 個の数値のシーケンスで構成されています。while ループ内に 2 つの `if` 条件を記述する必要があります。 +これらの条件は、ループの流れを以下のように変更します(`printf` コマンドは変更しません)。 -* If the current number which is about to printed is less than 5, don't print it. -* If the current number which is about to printed is greater than 10, don't print it and stop the loop. +* 現在出力しようとしている数値が 5 未満の場合、出力しません。 +* 現在出力しようとしている数値が 10 より大きい場合、出力せずにループを停止します。 -Notice that if you do not advance the iterator variable `i` and use the `continue` derivative, you will get stuck in an infinite loop. +反復変数 `i` を進めずに `continue` 導関数を使用しないと、無限ループに陥ることに注意してください。 チュートリアル コード ------------------- @@ -66,7 +65,7 @@ Notice that if you do not advance the iterator variable `i` and use the `continu int i = 0; while (i < 10) { - /* your code goes here */ + /* ここにコードを記述します */ printf("%d\n", array[i]); i++; From 656f2b0bab9438c472611d090e070cd872eb1211 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Thu, 27 Nov 2025 12:07:29 +0900 Subject: [PATCH 04/20] =?UTF-8?q?=E3=83=9D=E3=82=A4=E3=83=B3=E3=82=BF?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Pointers.md | 42 +++++++++++++++------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tutorials/learn-c.org/ja/Pointers.md b/tutorials/learn-c.org/ja/Pointers.md index 131b00c2e..0b8b8f50d 100644 --- a/tutorials/learn-c.org/ja/Pointers.md +++ b/tutorials/learn-c.org/ja/Pointers.md @@ -1,45 +1,47 @@ チュートリアル ------------- -Pointers are also variables and play a very important role in C programming language. They are used for several reasons, such as: +ポインタも変数の一種であり、Cプログラミング言語において非常に重要な役割を果たします。ポインタは、以下のような様々な目的で使用されます。 -* Strings -* Dynamic memory allocation -* Sending function arguments by reference -* Building complicated data structures -* Pointing to functions -* Building special data structures (i.e. Tree, Tries, etc...) +* 文字列 +* 動的メモリ割り当て +* 関数の引数を参照渡しする +* 複雑なデータ構造を構築する +* 関数へのポインタ +* 特殊なデータ構造(ツリー、トライなど)を構築する -And many more. +その他多数。 -### What is a pointer? +### ポインタとは何ですか? -A pointer is essentially a simple integer variable which holds a **memory address** that points to a value, instead of holding the actual value itself. +ポインタは本質的には、実際の値自体を保持するのではなく、値を指す **メモリ アドレス** を保持する単純な整数変数です。 -The computer's memory is a sequential store of data, and a pointer points to a specific part of the memory. Our program can use pointers in such a way that the pointers point to a large amount of memory - depending on how much we decide to read from that point on. +コンピュータのメモリはデータを格納するための連続的な場所であり、ポインタはそのメモリの特定の部分を指し示します。プログラムでは、ポインタを広範囲のメモリ領域を指すように使用することができます。その範囲は、メモリ上の特定の一点からどれだけの量のデータを読み込むかによって決まります。 -### Strings as pointers +### ポインタとしての文字列 -We've already discussed strings, but now we can dive in a bit deeper and understand what strings in C really are (which are called C-Strings to differentiate them from other strings when mixed with C++) +文字列については既に説明しましたが、ここではもう少し深く掘り下げて、C における文字列が実際に何であるかを理解します (C++ と混在する場合、他の文字列と区別するために C 文字列と呼ばれます)。 -The following line: +以下を見てください: char * name = "John"; -does three things: +この行は3つの処理を行います。 -1. It allocates a local (stack) variable called `name`, which is a pointer to a single character. -2. It causes the string "John" to appear somewhere in the program memory (after it is compiled and executed, of course). -3. It initializes the `name` argument to point to where the `J` character resides at (which is followed by the rest of the string in the memory). +1. `name` というローカル(スタック)変数を割り当てます。これは1文字へのポインタです。 +2. 文字列 "John" をプログラムメモリのどこかに出現させます(もちろん、コンパイルおよび実行後)。 +3. `name` 引数を初期化し、`J` 文字の位置(メモリ内の文字列の残りの部分がそれに続きます)を指します。 -If we try to access the `name` variable as an array, it will work, and will return the ordinal value of the character `J`, since the `name` variable actually points exactly to the beginning of the string. +`name` 変数に配列としてアクセスしようとすると、それはうまくいって、文字 `J` のASCIIコード値を返します。これは、`name` 変数が実際には文字列の先頭を正確に指しているためです。 -Since we know that the memory is sequential, we can assume that if we move ahead in the memory to the next character, we'll receive the next letter in the string, until we reach the end of the string, marked with a null terminator (the character with the ordinal value of 0, noted as `\0`). +メモリは連続的であることがわかっているため、メモリ内で次の文字に進むと、文字列の末尾 (ASCIIコード値が 0 の文字、`\0` と表記) に達するまで、文字列内の次の文字を受け取ると想定できます。 ### Dereferencing Dereferencing is the act of referring to where the pointer points, instead of the memory address. We are already using dereferencing in arrays - but we just didn't know it yet. The brackets operator - `[0]` for example, accesses the first item of the array. And since arrays are actually pointers, accessing the first item in the array is the same as dereferencing a pointer. Dereferencing a pointer is done using the asterisk operator `*`. +デリファレンスとは、メモリアドレスではなく、ポインタが指している場所を参照する動作です。配列では既にデリファレンスを使用していますが、今回はそのことを知らなかっただけです。例えば、括弧演算子 `[0]` は配列の最初の項目にアクセスします。配列は実際にはポインタなので、配列の最初の項目にアクセスすることはポインタをデリファレンスすることと同じです。ポインタをデリファレンスするには、アスタリスク演算子 `*` を使用します。 + If we want to create an array that will point to a different variable in our stack, we can write the following code: /* define a local variable a */ From 1562ff67fb18dd311c85d2825fc133e7e75ee6e1 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Thu, 27 Nov 2025 17:13:15 +0900 Subject: [PATCH 05/20] =?UTF-8?q?=E3=83=9D=E3=82=A4=E3=83=B3=E3=82=BF?= =?UTF-8?q?=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Pointers.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/tutorials/learn-c.org/ja/Pointers.md b/tutorials/learn-c.org/ja/Pointers.md index 0b8b8f50d..e1c0f0fcb 100644 --- a/tutorials/learn-c.org/ja/Pointers.md +++ b/tutorials/learn-c.org/ja/Pointers.md @@ -28,51 +28,49 @@ この行は3つの処理を行います。 -1. `name` というローカル(スタック)変数を割り当てます。これは1文字へのポインタです。 +1. `name` というローカル(スタック)変数をメモリに割り当てます。メモリアドレスを格納する箱となるもので、1文字へのポインタとなります。 2. 文字列 "John" をプログラムメモリのどこかに出現させます(もちろん、コンパイルおよび実行後)。 -3. `name` 引数を初期化し、`J` 文字の位置(メモリ内の文字列の残りの部分がそれに続きます)を指します。 +3. `name` 引数を初期化し、`J` 文字のアドレスを格納します。メモリ内の文字列の残りの部分がそれに続きます。 `name` 変数に配列としてアクセスしようとすると、それはうまくいって、文字 `J` のASCIIコード値を返します。これは、`name` 変数が実際には文字列の先頭を正確に指しているためです。 メモリは連続的であることがわかっているため、メモリ内で次の文字に進むと、文字列の末尾 (ASCIIコード値が 0 の文字、`\0` と表記) に達するまで、文字列内の次の文字を受け取ると想定できます。 -### Dereferencing +### デリファレンス(逆参照) -Dereferencing is the act of referring to where the pointer points, instead of the memory address. We are already using dereferencing in arrays - but we just didn't know it yet. The brackets operator - `[0]` for example, accesses the first item of the array. And since arrays are actually pointers, accessing the first item in the array is the same as dereferencing a pointer. Dereferencing a pointer is done using the asterisk operator `*`. +デリファレンスとは、メモリアドレスを参照する代わりに、ポインタが指している場所に格納されている値を参照する動作です。実はもう、配列でデリファレンスを使用していたのですが、あのときはまだ、そのことを知らなかったのです。例えば、添字演算子 `[0]` は配列の最初の要素にアクセスします。配列は実際にはポインタなので、配列の最初の要素にアクセスすることは、すなわちポインタをデリファレンスすることと、同じ動作になります。ポインタをデリファレンスするには、アスタリスク演算子 `*` を使用します。 -デリファレンスとは、メモリアドレスではなく、ポインタが指している場所を参照する動作です。配列では既にデリファレンスを使用していますが、今回はそのことを知らなかっただけです。例えば、括弧演算子 `[0]` は配列の最初の項目にアクセスします。配列は実際にはポインタなので、配列の最初の項目にアクセスすることはポインタをデリファレンスすることと同じです。ポインタをデリファレンスするには、アスタリスク演算子 `*` を使用します。 +スタック内の別の変数を指す配列を作成する場合は、次のコードを記述します。 -If we want to create an array that will point to a different variable in our stack, we can write the following code: - - /* define a local variable a */ + /* ローカル変数 a を定義する */ int a = 1; - /* define a pointer variable, and point it to a using the & operator */ + /* ポインタ変数を定義し、&演算子を使用して、aを指す */ int * pointer_to_a = &a; printf("The value a is %d\n", a); printf("The value of a is also %d\n", *pointer_to_a); -Notice that we used the `&` operator to point at the variable `a`, which we have just created. +先ほど作成した変数 `a` のアドレスを得るために `&` 演算子を使用していることに注意してください。 -We then referred to it using the dereferencing operator. We can also change the contents of the dereferenced variable: +次に、アスタリスク演算子を使って、アドレスから逆に、そこに格納されている値を参照しました。値を読むだけでなく、逆参照(デリファレンス)した変数の内容を変更することもできます。 int a = 1; int * pointer_to_a = &a; - /* let's change the variable a */ + /* 変数 a を変更してみましょう */ a += 1; - /* we just changed the variable again! */ + /* 変数をもう一度変更しました! */ *pointer_to_a += 1; - /* will print out 3 */ + /* 3 と表示されます */ printf("The value of a is now %d\n", a); 演習 ---- -Create a pointer to the local variable `n` called `pointer_to_n`, and use it to increase the value of `n` by one. +`pointer_to_n` というローカル変数 `n` へのポインタを作成し、それを使用して `n` の値を 1 増やします。 チュートリアル コード ------------------- From 7b3bdb78828fed5d84d297198cdf0be6836a75d5 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Thu, 27 Nov 2025 17:30:56 +0900 Subject: [PATCH 06/20] =?UTF-8?q?=E6=A7=8B=E9=80=A0=E4=BD=93=E3=81=AE?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Structures.md | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tutorials/learn-c.org/ja/Structures.md b/tutorials/learn-c.org/ja/Structures.md index 16f1b8d2f..595811fcb 100644 --- a/tutorials/learn-c.org/ja/Structures.md +++ b/tutorials/learn-c.org/ja/Structures.md @@ -1,57 +1,57 @@ チュートリアル ------------- -C structures are special, large variables which contain several named variables inside. Structures are the basic foundation for objects and classes in C. Structures are used for: +C構造体は、内部に複数の名前付き変数を含む特別な大きな変数です。構造体は、構造体とクラスの基本的な基礎です。構造体は以下の目的で使用されます。 -* Serialization of data -* Passing multiple arguments in and out of functions through a single argument -* Data structures such as linked lists, binary trees, and more +* データのシリアライズ +* 1つの引数を通じて関数の引数と出力 +* 関連リスト、二分木などのデータ構造 -The most basic example of structures are **points**, which are a single entity that contains two variables - `x` and `y`. Let's define a point: +構造体の最も基本的な例は、**点**です。点は、xとyという2つの変数を含む単一のエンティティです。点を定義しましょう。 struct point { int x; int y; }; -Now, let's define a new point, and use it. Assume the function `draw` receives a point and draws it on a screen. Without structs, using it would require two arguments - each for every coordinate: +次に、新しい点を定義し、使用します。関数`draw`が点を受け取り、画面に点を描くと仮定します。構造体を使用せずに、使用するには、各座標用の2つの引数が必要です。 - /* draws a point at 10, 5 */ + /* 10, 5 の位置に点を描く */ int x = 10; int y = 5; draw(x, y); -Using structs, we can pass a point argument: +構造体を使用して、点を引数として渡すことができます。 - /* draws a point at 10, 5 */ + /* 10, 5 の位置に点を描く */ struct point p; p.x = 10; p.y = 5; draw(p); -To access the point's variables, we use the dot `.` operator. +点の変数にアクセスするには、ドット`.`演算子を使用します。 ### Typedefs -Typedefs allow us to define types with a different name - which can come in handy when dealing with structs and pointers. In this case, we'd want to get rid of the long definition of a point structure. We can use the following syntax to remove the `struct` keyword from each time we want to define a new point: +typedefを使用することで、型の別名を定義できます。これは構造体とポインタを扱う際に便利です。ここで私たちは、点構造体の長い定義をなんとかしたいと考えています。新しい点を定義するたびに、いちいち`struct`キーワードをつけなくても済むように、以下の構文を使用できます。 typedef struct { int x; int y; } point; -This will allow us to define a new point like this: +これにより、新しい点を定義する際に、`struct`キーワードが不要になります。 point p; -Structures can also hold pointers - which allows them to hold strings, or pointers to other structures as well - which is their real power. For example, we can define a vehicle structure in the following manner: +構造体は、ポインタを含むことができます。これにより、構造体は文字列を保持したり、他の構造体へのポインタを保持したりすることができます。つまり、これが構造体の本当の力です。例えば、以下の方法で乗り物構造体を定義できます。 typedef struct { char * brand; int model; } vehicle; -Since brand is a char pointer, the vehicle type can contain a string (which, in this case, indicates the brand of the vehicle). +ブランドは文字列を保持したり、他の構造体へのポインタを保持したりすることができます。つまり、これが構造体の本当の力です。例えば、以下の方法で乗り物構造体を定義できます。 vehicle mycar; mycar.brand = "Ford"; @@ -60,14 +60,14 @@ Since brand is a char pointer, the vehicle type can contain a string (which, in 演習 ---- -Define a new data structure, named "person", which contains a string (pointer to char) called `name`, and an integer called `age`. +新しい構造体を定義してください。構造体の名前は"person"で、`name`という名前の文字列(charへのポインタ)と、`age`という整数を含みます。 チュートリアル コード ------------------- #include - /* define the person struct here using the typedef syntax */ + /* typedef構文を使用してここでperson構造体を定義します */ int main() { person john; From aa7ca1deaacfa5cbcff4ee85b7e2389b6f4b36e3 Mon Sep 17 00:00:00 2001 From: Osamu Ochi Date: Thu, 27 Nov 2025 19:55:23 +0900 Subject: [PATCH 07/20] =?UTF-8?q?=E6=A7=8B=E9=80=A0=E4=BD=93=E3=81=AE?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ja/Function arguments by reference.md | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/tutorials/learn-c.org/ja/Function arguments by reference.md b/tutorials/learn-c.org/ja/Function arguments by reference.md index 230840406..10915f260 100644 --- a/tutorials/learn-c.org/ja/Function arguments by reference.md +++ b/tutorials/learn-c.org/ja/Function arguments by reference.md @@ -1,14 +1,14 @@ チュートリアル ------------- -Assuming you now understand pointers and functions, you are aware that function arguments are passed by value, by which means they are copied in and out of functions. -But what if we pass pointers to values instead of the values themselves? This will allow us to give functions control over the variables and structures of the parent functions and not just a copy of them, thus directly reading and writing the original object. +ポインタと関数について理解した上で、関数の引数は値渡し、つまり関数間でコピーされて渡されることをご存知でしょう。 +しかし、値そのものではなく、値へのポインタを渡したらどうなるでしょうか? こうすることで、関数は親関数の変数や構造体をコピーではなく制御できるようになり、元のオブジェクトを直接読み書きできるようになります。 -Let's say we want to write a function which increments a number by one, called `addone`. This will not work: +例えば、`addone` という、数値を1ずつ増やす関数を記述するとします。これはうまくいきません。 void addone(int n) { - // n is local variable which only exists within the function scope - n++; // therefore incrementing it has no effect + // nは関数スコープ内にのみ存在するローカル変数です + n++; // そのため、nを増やしても、関数の外からは影響を与えません } int n; @@ -16,11 +16,11 @@ Let's say we want to write a function which increments a number by one, called ` addone(n); printf("After: %d\n", n); -However, this will work: +しかし、これはうまくいきます。 void addone(int *n) { - // n is a pointer here which point to a memory-adress outside the function scope - (*n)++; // this will effectively increment the value of n + // nは関数スコープ外のメモリアドレスを指すポインタです + (*n)++; // これにより、nの値が実質的に増加します。 } int n; @@ -28,20 +28,20 @@ However, this will work: addone(&n); printf("After: %d\n", n); -The difference is that the second version of `addone` receives a pointer to the variable `n` as an argument, and then it can manipulate it, because it knows where it is in the memory. +違いは、`addone` の 2 番目のバージョンは変数 `n` へのポインタを引数として受け取り、それがメモリ内のどこにあるかを知っているため、それを操作できるという点です。 -Notice that when calling the `addone` function, we **must** pass a reference to the variable `n`, and not the variable itself - this is done so that the function knows the address of the variable, and won't just receive a copy of the variable itself. +`addone` 関数を呼び出す際は、変数 `n` 自体ではなく、変数への参照を渡す必要があることに注意してください。これは、関数が変数のアドレスを認識し、変数のコピーを受け取らないようにするためです。 -### Pointers to structures +### 構造体へのポインタ -Let's say we want to create a function which moves a point forward in both `x` and `y` directions, called `move`. Instead of sending two pointers, we can now send only one pointer to the function of the point structure: +点を `x` 方向と `y` 方向の両方に移動する、`move` という関数を作成するとします。2つのポインタを引数として渡す代わりに、点構造体の関数へのポインタを1つだけ渡すことができます。 void move(point * p) { (*p).x++; (*p).y++; } -However, if we wish to dereference a structure and access one of it's internal members, we have a shorthand syntax for that, because this operation is widely used in data structures. We can rewrite this function using the following syntax: +構造体を逆参照して内部メンバーにアクセスしたいときのために、簡略化された構文があります。それほど、この操作は構造体データで広く使用されているのです。この関数は、次の構文を使って書き換えることができます。 void move(point * p) { p->x++; @@ -51,7 +51,7 @@ However, if we wish to dereference a structure and access one of it's internal m 演習 ---- -Write a function called `birthday`, which adds one to the `age` of a `person`. +`person`の`age`に1を加算する`birthday`という関数を書いてください。 チュートリアル コード ------------------- @@ -63,10 +63,11 @@ Write a function called `birthday`, which adds one to the `age` of a `person`. int age; } person; - /* function declaration */ + /* 関数宣言 */ + void birthday(person * p); - /* write your function here */ + /* ここに関数を記述してください */ int main() { person john; @@ -96,7 +97,7 @@ Write a function called `birthday`, which adds one to the `age` of a `person`. int age; } person; - /* function declaration */ + /* 関数宣言 */ void birthday(person * p); void birthday(person * p){ From fa08137fb1e8b6148966b4d47db363a8e72c757a Mon Sep 17 00:00:00 2001 From: osamu377 Date: Fri, 28 Nov 2025 19:36:50 +0900 Subject: [PATCH 08/20] =?UTF-8?q?=E5=8B=95=E7=9A=84=E3=83=A1=E3=83=A2?= =?UTF-8?q?=E3=83=AA=E5=89=B2=E3=82=8A=E5=BD=93=E3=81=A6=E3=81=AE=E7=BF=BB?= =?UTF-8?q?=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Dynamic allocation.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tutorials/learn-c.org/ja/Dynamic allocation.md b/tutorials/learn-c.org/ja/Dynamic allocation.md index cddb6542a..cf0f6d9b6 100644 --- a/tutorials/learn-c.org/ja/Dynamic allocation.md +++ b/tutorials/learn-c.org/ja/Dynamic allocation.md @@ -1,42 +1,42 @@ チュートリアル ------------- -Dynamic allocation of memory is a very important subject in C. It allows building complex data structures such as linked lists. Allocating memory dynamically helps us to store data without initially knowing the size of the data in the time we wrote the program. +メモリの動的割り当てはC言語において非常に重要なテーマです。これにより、連結リストなどの複雑なデータ構造を構築できます。メモリを動的に割り当てることで、プログラム作成時にデータのサイズを事前に把握することなく、データを保存できるようになります。 -To allocate a chunk of memory dynamically, we need to have a pointer ready to store the location of the newly allocated memory. We can access memory that was allocated to us using that same pointer, and we can use that pointer to free the memory again, once we have finished using it. +メモリチャンクを動的に割り当てるには、新しく割り当てられたメモリの位置を格納するためのポインタを用意しておく必要があります。同じポインタを使って割り当てられたメモリにアクセスでき、使い終わったメモリもそのポインタを使って解放できます。 -Let's assume we want to dynamically allocate a person structure. The person is defined like this: +person構造体を動的に割り当てるとします。personは次のように定義されています。 typedef struct { char * name; int age; } person; -To allocate a new person in the `myperson` argument, we use the following syntax: +`myperson` 引数に新しい人物を割り当てるには、次の構文を使用します。 person * myperson = (person *) malloc(sizeof(person)); -This tells the compiler that we want to dynamically allocate just enough to hold a person struct in memory and then return a pointer of type `person` to the newly allocated data. The memory allocation function `malloc()` reserves the specified memory space. In this case, this is the size of `person` in bytes. +これは、person構造体を保持するのに十分なメモリを動的に割り当て、その新しく割り当てられたデータへの`person`型のポインタを返すことをコンパイラに指示します。メモリ割り当て関数`malloc()`は、指定されたメモリ空間を予約します。この場合、これは`person`のバイト単位のサイズです。 - The reason we write `(person *)` before the call to `malloc()` is that `malloc()` returns a "void pointer," which is a pointer that doesn't have a type. Writing `(person *)` in front of it is called *typecasting*, and changes the type of the pointer returned from `malloc()` to be `person`. However, it isn't strictly necessary to write it like this as C will implicitly convert the type of the returned pointer to that of the pointer it is assigned to (in this case, `myperson`) if you don't typecast it. +`malloc()` の呼び出しの前に `(person *)` と書くのは、`malloc()` が「void ポインタ」、つまり型を持たないポインタを返すためです。`(person *)` を前に書くことは *型キャスト* と呼ばれ、`malloc()` から返されるポインタの型を `person` に変更します。ただし、型キャストしない場合、C 言語は返されるポインタの型を、代入先のポインタ(この場合は `myperson`)の型に暗黙的に変換するため、必ずしもこのように書く必要はありません。 -Note that `sizeof` is not an actual function, because the compiler interprets it and translates it to the actual memory size of the person struct. +`sizeof` は実際の関数ではないことに注意してください。コンパイラがこれを解釈し、 person 構造体の実際のメモリ サイズに変換するためです。 -To access the person's members, we can use the `->` notation: +person 構造体のメンバーにアクセスするには、`->` 表記を使用できます。 myperson->name = "John"; myperson->age = 27; -After we are done using the dynamically allocated struct, we can release it using `free`: +動的に割り当てられた構造体の使用が終わったら、`free` を使用して解放できます。 free(myperson); -Note that the free does not delete the `myperson` variable itself, it simply releases the data that it points to. The `myperson` variable will still point to somewhere in the memory - but after calling `free(myperson)` we are not allowed to access that area anymore. We must not use that pointer again until we allocate new data using it. +free は `myperson` 変数自体を削除するのではなく、その変数が指しているデータを解放するだけであることに注意してください。`myperson` 変数はメモリ内のどこかを指したままになりますが、`free(myperson)` を呼び出した後は、その領域にアクセスできなくなります。新しいデータを割り当てるまでは、そのポインタを再び使用してはいけません。 演習 ---- -Use `malloc` to dynamically allocate a point structure. +`malloc` を使用してポイント構造体を動的に割り当てます。 チュートリアル コード ------------------- @@ -52,8 +52,8 @@ Use `malloc` to dynamically allocate a point structure. int main() { point * mypoint = NULL; - /* Dynamically allocate a new point - struct which mypoint points to here */ + /* ここで、mypoint が指す新しいポイント構造体を + 動的に割り当てます */ mypoint->x = 10; mypoint->y =5 ; From c258b3063e77462bd38662e5393a6c7f0dbc7585 Mon Sep 17 00:00:00 2001 From: osamu377 Date: Fri, 28 Nov 2025 21:00:58 +0900 Subject: [PATCH 09/20] =?UTF-8?q?=E9=85=8D=E5=88=97=E3=81=A8=E3=83=9D?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=BF=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Arrays and Pointers.md | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/tutorials/learn-c.org/ja/Arrays and Pointers.md b/tutorials/learn-c.org/ja/Arrays and Pointers.md index bde3c6c9b..303b7fba8 100644 --- a/tutorials/learn-c.org/ja/Arrays and Pointers.md +++ b/tutorials/learn-c.org/ja/Arrays and Pointers.md @@ -1,30 +1,30 @@ チュートリアル ------------- -In a previous tutorial on [[Pointers]], you learned that a pointer to a given data type can store the address of any variable of that particular data type. For example, in the following code, the pointer variable `pc` stores the address of the character variable `c`. +以前の[[ポインタ]]に関するチュートリアルでは、特定のデータ型へのポインタは、そのデータ型の任意の変数のアドレスを格納できることを学びました。例えば、次のコードでは、ポインタ変数 `pc` は文字変数 `c` のアドレスを格納しています。 char c = 'A'; char *pc = &c; -Here, `c` is a scalar variable that can store only a single value. However, you are already familiar with arrays that can hold multiple values of the same data type in a contiguously allocated memory block. So, you might wonder, can we have pointers to arrays too? Indeed, we can. +ここで、`c` は単一の値しか格納できないスカラー変数です。しかし、連続して割り当てられたメモリブロックに同じデータ型の複数の値を保持できる配列については既にご存知でしょう。では、配列へのポインタも使用できるのだろうかと疑問に思うかもしれません。はい、可能です。 -Let us start with an example code and look at its output. We will discuss its behavior subsequently. +まずはサンプルコードとその出力を見てみましょう。その後、その動作について説明します。 char vowels[] = {'A', 'E', 'I', 'O', 'U'}; char *pvowels = vowels; int i; - // Print the addresses + // 住所を印刷する for (i = 0; i < 5; i++) { printf("&vowels[%d]: %p, pvowels + %d: %p, vowels + %d: %p\n", i, &vowels[i], i, pvowels + i, i, vowels + i); } - // Print the values + // 値を印刷する for (i = 0; i < 5; i++) { printf("vowels[%d]: %c, *(pvowels + %d): %c, *(vowels + %d): %c\n", i, vowels[i], i, *(pvowels + i), i, *(vowels + i)); } -A typical output of the above code is shown below. +上記コードの典型的な出力を以下に示します。 >&vowels[0]: 0x7ffee146da17, pvowels + 0: 0x7ffee146da17, vowels + 0: 0x7ffee146da17 > @@ -48,19 +48,18 @@ A typical output of the above code is shown below. -As you rightly guessed, `&vowels[i]` gives the memory location of the *i*th element of the array `vowels`. Moreover, since this is a character array, each element occupies one byte so that the consecutive memory addresses are separated by a single byte. We also created a pointer, `pvowels`, and assigned the address of the array `vowels` to it. `pvowels + i` is a valid operation; although in general, this may not always be meaningful (explored further in [[Pointer Arithmetics]] ). In particular, the output shown above indicates that `&vowels[i]` and `pvowels + i` are equivalent. Feel free to alter the data types of the array and pointer variables to test this out. +ご想像のとおり、`&vowels[i]` は配列 `vowels` の *i* 番目の要素のメモリ位置を表します。さらに、これは文字配列であるため、各要素は 1 バイトを占有し、連続するメモリアドレスは 1 バイトで区切られます。また、ポインタ `pvowels` を作成し、配列 `vowels` のアドレスをそれに代入しました。`pvowels + i` は有効な演算ですが、一般的には必ずしも意味を持つとは限りません ([[ポインタ演算]] で詳しく説明します)。特に、上記の出力は `&vowels[i]` と `pvowels + i` が同等であることを示しています。配列変数とポインタ変数のデータ型を自由に変更して、これをテストしてみてください。 -If you look carefully at the previous code, you will notice that we also used another apparently surprising notation: `vowels + i`. Moreover, `pvowels + i` and `vowels + i` returns the same thing — address of the *i*th element of the array `vowels`. On the other hand, `*(pvowels + i)` and `*(vowels + i)` both return the *i*th element of the array `vowels`. Why is that so? +前のコードをよく見ると、`vowels + i` という、一見意外な記法も使われていることに気づくでしょう。さらに、`pvowels + i` と `vowels + i` は同じもの、つまり配列 `vowels` の *i* 番目の要素のアドレスを返します。一方、`*(pvowels + i)` と `*(vowels + i)` はどちらも配列 `vowels` の *i* 番目の要素を返します。なぜでしょうか? -This is because the name of an array itself is a (constant) pointer to the first element of the array. In other words, the notations `vowels`, `&vowels[0]`, and `vowels + 0` all point to the same location. +これは、配列の名前自体が配列の最初の要素への(定数)ポインタであるためです。つまり、`vowels`、`&vowels[0]`、`vowels + 0` という記法はすべて同じ位置を指しているのです。 +配列の動的メモリ割り当て +------------------------ -Dynamic Memory Allocation for Arrays ------------------------------------- +ここまでで、ポインタを使って配列を走査できることをご理解いただけたと思います。さらに、ブロックポインタを使って(連続した)メモリを動的に確保できることも分かりました。これら2つの要素を組み合わせることで、配列のメモリを動的に確保することができます。これは以下のコードで示されています。 -By now we know that we can traverse an array using pointers. Moreover, we also know that we can dynamically allocate (contiguous) memory using blocks pointers. These two aspects can be combined to dynamically allocate memory for an array. This is illustrated in the following code. - - // Allocate memory to store five characters + // 5文字を格納するためのメモリを割り当てる int n = 5; char *pvowels = (char *) malloc(n * sizeof(char)); int i; @@ -79,20 +78,20 @@ By now we know that we can traverse an array using pointers. Moreover, we also k free(pvowels); -In the above code, we allocated five contiguous bytes of memory to store five characters. Subsequently, we used array notations to traverse the blocks of memory as if `pvowels` is an array. However, remember that `pvowels` actually is a pointer. Pointers and arrays, in general, are not the same thing. +上記のコードでは、5文字を格納するために連続する5バイトのメモリを割り当てました。その後、配列表記法を使用して、`pvowels` が配列であるかのようにメモリブロックを走査しました。ただし、`pvowels` は実際にはポインタであることに注意してください。一般的に、ポインタと配列は同じではありません。 -So when is this useful? Remember that while declaring an array, the number of elements that it would contain must be known beforehand. Therefore, in some scenarios it might happen that the space allocated for an array is either less than the desired space or more. However, by using dynamic memory allocation, one can allocate just as much memory as required by a program. Moreover, unused memory can be freed as soon as it is no longer required by invoking the `free()` function. On the down side, with dynamic memory allocation, one must responsibly call `free()` wherever relevant. Otherwise, memory leaks would occur. +では、これはどのような場合に便利なのでしょうか? 配列を宣言する際には、その要素数を事前に把握しておく必要があることを覚えておいてください。そのため、場合によっては、配列に割り当てられる領域が目的の領域よりも少なかったり、多すぎたりすることがあります。しかし、動的メモリ割り当てを使用すれば、プログラムに必要なだけのメモリを割り当てることができます。さらに、未使用のメモリは不要になったらすぐに「free()」関数を呼び出して解放できます。ただし、動的メモリ割り当ての欠点は、必要な場合に「free()」を責任を持って呼び出す必要があることです。そうしないと、メモリリークが発生します。 -We conclude this tutorial by looking at dynamic memory allocation for a two-dimensional array. This can be generalized to *n*-dimensions in a similar way. Unlike one-dimensional arrays, where we used a pointer, in this case we require a pointer to a pointer, as shown below. +このチュートリアルの最後に、2次元配列の動的メモリ割り当てについて見ていきます。これは同様の方法でn次元配列にも一般化できます。1次元配列ではポインタを使用していましたが、この場合は以下に示すように、ポインタへのポインタが必要になります。 int nrows = 2; int ncols = 5; int i, j; - // Allocate memory for nrows pointers + // nrowsポインタにメモリを割り当てる char **pvowels = (char **) malloc(nrows * sizeof(char *)); - // For each row, allocate memory for ncols elements + // 各行にncols要素のメモリを割り当てる pvowels[0] = (char *) malloc(ncols * sizeof(char)); pvowels[1] = (char *) malloc(ncols * sizeof(char)); @@ -116,18 +115,18 @@ We conclude this tutorial by looking at dynamic memory allocation for a two-dime printf("\n"); } - // Free individual rows + // 個々の行を解放する free(pvowels[0]); free(pvowels[1]); - // Free the top-level pointer + // トップレベルのポインタを解放する free(pvowels); 演習 ---- -The first seven rows of [Pascal's triangle](http://mathworld.wolfram.com/PascalsTriangle.html) are shown below. Note that row *i* contains *i* elements. Therefore, to store the numbers from the first three rows, one would require 1 + 2 + 3 = 6 memory slots. +[パスカルの三角形](http://mathworld.wolfram.com/PascalsTriangle.html)の最初の7行を以下に示します。行*i*には*i*個の要素が含まれていることに注意してください。したがって、最初の3行の数値を格納するには、1 + 2 + 3 = 6個のメモリスロットが必要になります。 >1 > @@ -143,7 +142,7 @@ The first seven rows of [Pascal's triangle](http://mathworld.wolfram.com/Pascals > >1 6 15 20 15 6 1 -Complete the skeleton code given below to store the numbers from the first three rows of Pascal's triangle in a two-dimensional "array" using dynamic memory allocation. Note that you must allocate exactly six memory slots to store those six numbers. No extra memory should be allocated. At the end of your program, free all the memory blocks used in this program. +パスカルの三角形の最初の3行の数値を、動的メモリ割り当てを用いて、2次元「配列」に格納するためのスケルトンコードを以下に示す。これらの6つの数値を格納するには、正確に6つのメモリスロットを割り当てる必要があることに注意する。余分なメモリは割り当てない。プログラムの最後に、このプログラムで使用したすべてのメモリブロックを解放する。 チュートリアル コード @@ -154,12 +153,12 @@ Complete the skeleton code given below to store the numbers from the first three int main() { int i, j; - /* TODO: define the 2D pointer variable here */ + /* TODO: ここで2Dポインタ変数を定義する */ - /* TODO: complete the following line to allocate memory for holding three rows */ + /* TODO: 3行を保持するためのメモリを割り当てるには、次の行を完成させる */ pnumbers = (int **) malloc(); - /* TODO: allocate memory for storing the individual elements in a row */ + /* TODO: 行内の個々の要素を格納するためのメモリを割り当てる */ pnumbers[0] = (int *) malloc(1 * sizeof(int)); pnumbers[0][0] = 1; @@ -177,10 +176,10 @@ Complete the skeleton code given below to store the numbers from the first three } for (i = 0; i < 3; i++) { - /* TODO: free memory allocated for each row */ + /* TODO: 各行に割り当てられたメモリを解放する */ } - /* TODO: free the top-level pointer */ + /* TODO: トップレベルのポインタを解放する */ return 0; } @@ -200,13 +199,13 @@ Complete the skeleton code given below to store the numbers from the first three int main() { int i, j; - /* TODO: define the 2D pointer variable here */ + /* TODO: 行内の個々の要素を格納するためのメモリを割り当てる */ int **pnumbers; - /* TODO: Complete the following line to allocate memory for holding three rows */ + /* TODO: 3行を保持するためのメモリを割り当てるには、次の行を完成させる */ pnumbers = (int **) malloc(3 *sizeof(int *)); - /* TODO: Allocate memory for storing the individual elements in a row */ + /* TODO: 行内の個々の要素を格納するためのメモリを割り当てる */ pnumbers[0] = (int *) malloc(1 * sizeof(int)); pnumbers[1] = (int *) malloc(2 * sizeof(int)); pnumbers[2] = (int *) malloc(3 * sizeof(int)); From 1cd2d7d66e619ce18a83aacb1c476315d69db9c0 Mon Sep 17 00:00:00 2001 From: osamu377 Date: Sat, 29 Nov 2025 20:41:38 +0900 Subject: [PATCH 10/20] =?UTF-8?q?=E5=86=8D=E8=B5=B7=E3=81=A8=E9=80=A3?= =?UTF-8?q?=E7=B5=90=E3=83=AA=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Linked lists.md | 130 ++++++++++------------- tutorials/learn-c.org/ja/Recursion.md | 30 +++--- 2 files changed, 71 insertions(+), 89 deletions(-) diff --git a/tutorials/learn-c.org/ja/Linked lists.md b/tutorials/learn-c.org/ja/Linked lists.md index fa771e665..1f72496e3 100644 --- a/tutorials/learn-c.org/ja/Linked lists.md +++ b/tutorials/learn-c.org/ja/Linked lists.md @@ -1,37 +1,29 @@ チュートリアル ------------- -### Introduction +### イントロダクション -Linked lists are the best and simplest example of a dynamic data structure that uses pointers for its implementation. -However, understanding pointers is crucial to understanding how linked lists work, so if you've skipped the pointers -tutorial, you should go back and redo it. You must also be familiar with dynamic memory allocation and structures. +連結リストは、ポインタを用いて実装する動的データ構造の最も優れた、そして最もシンプルな例です。 +しかし、連結リストの仕組みを理解するには、ポインタを理解することが重要です。そのため、ポインタのチュートリアルをスキップした場合は、戻ってもう一度学習してください。また、動的メモリ割り当てと構造についても理解しておく必要があります。 -Essentially, linked lists function as an array that can grow and shrink as needed, from any point in the array. +基本的に、連結リストは、配列内の任意の位置から必要に応じて拡大または縮小できる配列として機能します。 -Linked lists have a few advantages over arrays: +連結リストには、配列に比べていくつかの利点があります。 -1. Items can be added or removed from the middle of the list -2. There is no need to define an initial size +1. リストの途中から項目を追加または削除できます。 +2. 初期サイズを定義する必要はありません。 -However, linked lists also have a few disadvantages: +しかし、連結リストにはいくつかの欠点もあります。 -1. There is no "random" access - it is impossible to reach the nth item in the array without first iterating over -all items up until that item. This means we have to start from the beginning of the list and count how many times -we advance in the list until we get to the desired item. -2. Dynamic memory allocation and pointers are required, which complicates the code and increases the risk of -memory leaks and segment faults. -3. Linked lists have a much larger overhead over arrays, since linked list items are dynamically allocated (which -is less efficient in memory usage) and each item in the list also must store an additional pointer. +1. 「ランダム」アクセスが不可能です。配列のn番目の項目に到達するには、まずその項目までのすべての項目を反復処理する必要があります。つまり、リストの先頭から始めて、目的の項目に到達するまでリスト内を何回進んだかを数える必要があります。 +2. 動的なメモリ割り当てとポインタが必要となるため、コードが複雑になり、メモリリークやセグメントフォールトのリスクが高まります。 +3. 連結リストは配列よりもオーバーヘッドがはるかに大きくなります。これは、連結リストの項目が動的に割り当てられるため(メモリ使用効率が低い)、リスト内の各項目に追加のポインタを格納する必要があるためです。 -### What is a linked list? +### リンクリストとは何か? -A linked list is a set of dynamically allocated nodes, arranged in such a way that each node contains one value -and one pointer. The pointer always points to the next member of the list. If the pointer is NULL, then it is -the last node in the list. +リンクリストは、動的に割り当てられたノードの集合であり、各ノードが1つの値と1つのポインタを持つように配置されています。ポインタは常にリストの次のメンバーを指します。ポインタがNULLの場合、リストの最後のノードを指します。 -A linked list is held using a local pointer variable which points to the first item of the list. If that pointer -is also NULL, then the list is considered to be empty. +リンクリストは、リストの最初の項目を指すローカルポインタ変数を使用して保持されます。そのポインタもNULLの場合、リストは空であるとみなされます。 ------------------------------ ------------------------------ | | | \ | | | @@ -39,16 +31,16 @@ is also NULL, then the list is considered to be empty. | | | / | | | ------------------------------ ------------------------------ -Let's define a linked list node: +リンク リスト ノードを定義する: typedef struct node { int val; struct node * next; } node_t; -Notice that we are defining the struct in a recursive manner, which is possible in C. Let's name our node type `node_t`. +構造体を再帰的に定義していることに注意してください。これはC言語でも可能です。ノード型の名前を「node_t」としましょう。 -Now we can use the nodes. Let's create a local variable which points to the first item of the list (called `head`). +これでノードを使用できるようになりました。リストの最初の項目を指すローカル変数(「head」)を作成しましょう。 node_t * head = NULL; head = (node_t *) malloc(sizeof(node_t)); @@ -59,10 +51,10 @@ Now we can use the nodes. Let's create a local variable which points to the firs head->val = 1; head->next = NULL; -We've just created the first variable in the list. We must set the value, and the next item to be empty, if we want -to finish populating the list. Notice that we should always check if malloc returned a NULL value or not. +リストの最初の変数を作成しました。リストへのデータの追加を完了するには、値を設定し、次の項目を空にする必要があります。 +malloc が NULL 値を返したかどうかを常に確認する必要があることに注意してください。 -To add a variable to the end of the list, we can just continue advancing to the next pointer: +リストの末尾に変数を追加するには、次のポインタまで進み続けます。 node_t * head = NULL; head = (node_t *) malloc(sizeof(node_t)); @@ -71,14 +63,11 @@ To add a variable to the end of the list, we can just continue advancing to the head->next->val = 2; head->next->next = NULL; -This can go on and on, but what we should actually do is advance to the last item of the list, until the `next` variable -will be `NULL`. +これを延々と続けることもできますが、実際に行うべきことは、`next` 変数が `NULL` になるまで、リストの最後の項目まで進むことです。 -### Iterating over a list +### リストの反復処理 -Let's build a function that prints out all the items of a list. To do this, we need to use a `current` pointer -that will keep track of the node we are currently printing. After printing the value of the node, we set the `current` -pointer to the next node, and print again, until we've reached the end of the list (the next node is NULL). +リストのすべての項目を出力する関数を作成しましょう。そのためには、現在出力中のノードを追跡する「current」ポインタを使用する必要があります。ノードの値を出力した後、「current」ポインタを次のノードに設定し、リストの末尾に達するまで(次のノードはNULLです)、再度出力します。 void print_list(node_t * head) { node_t * current = head; @@ -89,10 +78,9 @@ pointer to the next node, and print again, until we've reached the end of the li } } -### Adding an item to the end of the list +### リストの最後に項目を追加する -To iterate over all the members of the linked list, we use a pointer called `current`. We set it to start from the head -and then in each step, we advance the pointer to the next item in the list, until we reach the last item. +連結リストのすべてのメンバーを反復処理するには、「current」というポインタを使用します。このポインタを先頭から開始するように設定し、各ステップでポインタをリストの次の項目に進め、最後の項目に到達するまで続けます。 void push(node_t * head, int val) { node_t * current = head; @@ -100,27 +88,25 @@ and then in each step, we advance the pointer to the next item in the list, unti current = current->next; } - /* now we can add a new variable */ + /* 新しい変数を追加できるようになった */ current->next = (node_t *) malloc(sizeof(node_t)); current->next->val = val; current->next->next = NULL; } -The best use cases for linked lists are stacks and queues, which we will now implement: +リンク リストの最適な使用例はスタックとキューです。これらをここで実装します。 -### Adding an item to the beginning of the list (pushing to the list) +### リストの先頭にアイテムを追加する(リストにプッシュする) -To add to the beginning of the list, we will need to do the following: +リストの先頭に追加するには、以下の手順が必要です。 -1. Create a new item and set its value -2. Link the new item to point to the head of the list -3. Set the head of the list to be our new item +1. 新しい項目を作成し、その値を設定します。 +2. 新しい項目をリストの先頭にリンクします。 +3. リストの先頭を新しい項目に設定します。 -This will effectively create a new head to the list with a new value, and keep the rest of the list linked to it. - -Since we use a function to do this operation, we want to be able to modify the head variable. To do this, we must -pass a pointer to the pointer variable (a double pointer) so we will be able to modify the pointer itself. +これにより、リストに新しい値を持つ新しいヘッドが作成され、リストの残りの部分はそれにリンクされたままになります。 +この操作には関数を使用するため、ヘッド変数を変更できるようにする必要があります。そのためには、ポインタ変数へのポインタ(ダブルポインタ)を渡す必要があります。これにより、ポインタ自体を変更できるようになります。 void push(node_t ** head, int val) { node_t * new_node; @@ -132,15 +118,15 @@ pass a pointer to the pointer variable (a double pointer) so we will be able to } -### Removing the first item (popping from the list) +### 最初の項目を削除する(リストからポップする) -To pop a variable, we will need to reverse this action: +変数をポップするには、このアクションを逆に実行する必要があります。 -1. Take the next item that the head points to and save it -2. Free the head item -3. Set the head to be the next item that we've stored on the side +1. ヘッドが指している次の項目を取得して保存します。 +2. ヘッド項目を解放します。 +3. サイドに格納した次の項目をヘッドに設定します。 -Here is the code: +コードは次のとおりです。 int pop(node_t ** head) { int retval = -1; @@ -159,29 +145,27 @@ Here is the code: } -### Removing the last item of the list +### リストの最後の項目を削除する -Removing the last item from a list is very similar to adding it to the end of the list, but with one big exception - -since we have to change one item before the last item, we actually have to look two items ahead and see if the next -item is the last one in the list: +リストから最後の項目を削除することは、リストの末尾に追加することと非常に似ていますが、大きな違いが 1 つあります。最後の項目の 1 つ前の項目を変更する必要があるため、実際には 2 つ先の項目を調べて、次の項目がリストの最後の項目であるかどうかを確認する必要があります。: int remove_last(node_t * head) { int retval = 0; - /* if there is only one item in the list, remove it */ + /* リストに項目が1つしかない場合はそれを削除します */ if (head->next == NULL) { retval = head->val; free(head); return retval; } - /* get to the second to last node in the list */ + /* リストの最後から2番目のノードに移動する */ node_t * current = head; while (current->next->next != NULL) { current = current->next; } - /* now current points to the second to last item of the list, so let's remove current->next */ + /* 現在、currentはリストの最後から2番目の項目を指しているので、current->nextを削除します */ retval = current->next->val; free(current->next); current->next = NULL; @@ -190,20 +174,19 @@ item is the last one in the list: } -### Removing a specific item +### 特定のアイテムを削除する -To remove a specific item from the list, either by its index from the beginning of the list or by its value, we will -need to go over all the items, continuously looking ahead to find out if we've reached the node before the item -we wish to remove. This is because we need to change the location to where the previous node points to as well. +リストから特定の項目を削除するには、リストの先頭からのインデックス、または値のいずれかで、すべての項目を調べ、削除したい項目の前のノードに到達しているかどうかを確認する必要があります。 +これは、前のノードが指している場所にも位置を変更する必要があるためです。 -Here is the algorithm: +アルゴリズムは次のとおり: -1. Iterate to the node before the node we wish to delete -2. Save the node we wish to delete in a temporary pointer -3. Set the previous node's next pointer to point to the node after the node we wish to delete -4. Delete the node using the temporary pointer +1. 削除したいノードの前のノードまで反復処理する +2. 削除したいノードを一時ポインタに保存する +3. 前のノードの次のポインタを、削除したいノードの次のノードを指すように設定する +4. 一時ポインタを使ってノードを削除する -There are a few edge cases we need to take care of, so make sure you understand the code. +いくつかのエッジケースに対処する必要があるので、コードの内容を理解しておいてください。 int remove_by_index(node_t ** head, int n) { int i = 0; @@ -240,8 +223,7 @@ There are a few edge cases we need to take care of, so make sure you understand 演習 ---- -You must implement the function `remove_by_value` which receives a double pointer to the head and removes the first -item in the list which has the value `val`. +先頭へのダブルポインタを受け取り、リスト内の値 `val` を持つ最初の項目を削除する関数 `remove_by_value` を実装する必要があります。 チュートリアル コード ------------------- @@ -280,7 +262,7 @@ item in the list which has the value `val`. } int remove_by_value(node_t ** head, int val) { - /* TODO: fill in your code here */ + /* TODO: ここにコードを入力してください */ } int main() { diff --git a/tutorials/learn-c.org/ja/Recursion.md b/tutorials/learn-c.org/ja/Recursion.md index 3bfd26adf..f76048da1 100644 --- a/tutorials/learn-c.org/ja/Recursion.md +++ b/tutorials/learn-c.org/ja/Recursion.md @@ -1,16 +1,16 @@ チュートリアル ------------- -Recursion occurs when a function contains within it a call to itself. Recursion can result in very neat, elegant code that is intuitive to follow. It can also result in a very large amount of memory being used if the recursion gets too deep. +再帰は、関数内に自分自身の呼び出しが含まれている場合に発生します。再帰により、非常に簡潔で洗練された、直感的に理解しやすいコードを作成できます。ただし、再帰が深くなりすぎると、大量のメモリが消費される可能性があります。 -Common examples of where recursion is used : - -* Walking recursive data structures such as linked lists, binary trees, etc. -* Exploring possible scenarios in games such as chess - -Recursion always consists of two main parts. A terminating case that indicates when the recursion will finish and a call to itself that must make progress towards the terminating case. +再帰が使用される一般的な例: + +* リンクリスト、二分木などの再帰データ構造の探索 +* チェスなどのゲームにおけるシナリオの探索 -For example, this function will perform multiplication by recursively adding : +再帰は常に2つの主要な部分から構成されます。再帰がいつ終了するかを示す終了ケースと、終了ケースに向かって進む必要がある自分自身の呼び出しです。 + +例えば、この関数は再帰的に加算することで乗算を実行します。 #include @@ -18,16 +18,16 @@ For example, this function will perform multiplication by recursively adding : { if (x == 1) { - /* Terminating case */ + /* 終了させる */ return y; } else if (x > 1) { - /* Recursive step */ + /* 再帰ステップ */ return y + multiply(x-1, y); } - /* Catch scenario when x is zero */ + /* xが0のとき */ return 0; } @@ -39,7 +39,7 @@ For example, this function will perform multiplication by recursively adding : 演習 ---- -Define a new function called `factorial()` that will compute the factorial by recursive multiplication (5! = 5 x 4 x 3 x 2 x 1). Note that by convention, the factorial of 0 is equal to 1 (0! = 1). +再帰乗算によって階乗を計算する `factorial()` という新しい関数を定義します (5! = 5 x 4 x 3 x 2 x 1)。慣例により、0の階乗は1に等しい (0! = 1) ことに注意してください。 チュートリアル コード ------------------- @@ -47,14 +47,14 @@ Define a new function called `factorial()` that will compute the factorial by re #include int main() { - /* testing code */ + /* テスト用コード */ printf("0! = %i\n", factorial(0)); printf("1! = %i\n", factorial(1)); printf("3! = %i\n", factorial(3)); printf("5! = %i\n", factorial(5)); } - /* define your function here (don't forget to declare it) */ + /* ここで関数を定義します(宣言することを忘れないでください) */ 期待している出力 --------------- @@ -72,7 +72,7 @@ Define a new function called `factorial()` that will compute the factorial by re int factorial(int number); int main() { - /* testing code */ + /* テスト用コード */ printf("0! = %i\n", factorial(0)); printf("1! = %i\n", factorial(1)); printf("3! = %i\n", factorial(3)); From 246cbe0ed59b214d3ac09105c17e20117b93ddb8 Mon Sep 17 00:00:00 2001 From: osamu377 Date: Sun, 30 Nov 2025 11:50:01 +0900 Subject: [PATCH 11/20] =?UTF-8?q?B=E6=9C=A8=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Binary trees.md | 38 ++++++++++++------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index 99fd5f75f..a5abaf40f 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -3,7 +3,7 @@ ### Introduction -A Binary Tree is a type of data structure in which each node has at most two children (left child and right child). Binary trees are used to implement binary search trees and binary heaps, and are used for efficient searching and sorting. A binary tree is a special case of a K-ary tree, where k is 2. Common operations for binary trees include insertion, deletion, and traversal. The difficulty of performing these operations varies if the tree is balanced and also whether the nodes are leaf nodes or branch nodes. For **balanced trees** the depth of the left and right subtrees of every node differ by 1 or less. This allows for a predictable **depth** also known as **height**. This is the measure of a node from root to leaf, where root is 0 and sebsequent nodes are (1,2..n). This can be expressed by the integer part of log2(n) where n is the number of nodes in the tree. +二分木は、各ノードが最大 2 つの子 (左の子と右の子) を持つデータ構造のタイプです。二分木は、二分探索木と二分ヒープの実装に使用され、効率的な検索とソートに使用されます。二分木は、k が 2 である K 分木の特殊なケースです。二分木の一般的な操作には、挿入、削除、トラバーサルがあります。これらの操作を実行する難易度は、木がバランスが取れているかどうか、またノードがリーフ ノードであるかブランチ ノードであるかによって異なります。**バランスの取れた木** では、各ノードの左と右のサブツリーの深さの差は 1 以下です。これにより、**深さ** (高さ** とも呼ばれる) を予測できるようになります。これは、ルートからリーフまでのノードの測定値で、ルートが 0、後続のノードが (1,2..n) です。これは、log2(n) の整数部分で表すことができます。ここで、n は木内のノードの数です。 g s 9 / \ / \ / \ @@ -11,15 +11,15 @@ A Binary Tree is a type of data structure in which each node has at most two chi / \ / \ / \ c d t y 11 15 -The operations performed on trees requires searching in one of two main ways: Depth First Search and Breadth-first search. **Depth-first search (DFS)** is an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking. There are three types of depth first search traversal: **pre-order** visit, left, right, **in-order** left, visit, right, **post-order** left, right, visit. **Breadth-first search (BFS)** is an algorithm for traversing or searching tree or graph structures. In level-order, where we visit every node on a level before going to a lower level.
+ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿って可能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
演習 ---- -Below is an implementation of a binary tree that has insertion and printing capabilities. This tree is ordered but not balanced. This example maintains its ordering at insertion time. +以下は、挿入と出力の機能を持つ二分木の実装です。この木は順序付けされていますが、バランスはとれていません。この例では、挿入時に順序付けが維持されます。 -Change the print routine to depth-first search **pre-order**. +出力ルーチンを深さ優先探索の **pre-order** に変更してください。 チュートリアル コード @@ -42,7 +42,7 @@ Change the print routine to depth-first search **pre-order**. int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -75,7 +75,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -85,7 +85,7 @@ Change the print routine to depth-first search **pre-order**. { if (val >= tree->val) { - /* insert right */ + /* 右に挿入 */ if (tree->right != NULL) { insert(tree->right,val); @@ -93,7 +93,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -103,11 +103,11 @@ Change the print routine to depth-first search **pre-order**. } } - /* depth-first search */ + /* 深さ優先探索 */ void printDFS(node_t * current) { - /* change the code here */ - if (current == NULL) return; /* security measure */ + /* ここでコードを変更してください */ + if (current == NULL) return; /* セキュリティ対策 */ if (current->left != NULL) printDFS(current->left); if (current != NULL) printf("%d ", current->val); if (current->right != NULL) printDFS(current->right); @@ -139,7 +139,7 @@ Change the print routine to depth-first search **pre-order**. int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -157,14 +157,14 @@ Change the print routine to depth-first search **pre-order**. { if (tree->val == 0) { - /* insert on current (empty) position */ + /* 現在の(空の)位置に挿入する */ tree->val = val; } else { if (val < tree->val) { - /* insert left */ + /* 左に挿入 */ if (tree->left != NULL) { insert(tree->left, val); @@ -172,7 +172,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -182,7 +182,7 @@ Change the print routine to depth-first search **pre-order**. { if (val >= tree->val) { - /* insert right */ + /* 右に挿入 */ if (tree->right != NULL) { insert(tree->right,val); @@ -190,7 +190,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -200,11 +200,11 @@ Change the print routine to depth-first search **pre-order**. } } - /* depth-first search */ + /* 深さ優先探索 */ void printDFS(node_t * current) { /* change the code here */ - if (current == NULL) return; /* security measure */ + if (current == NULL) return; /* セキュリティ対策 */ if (current != NULL) printf("%d ", current->val); if (current->left != NULL) printDFS(current->left); if (current->right != NULL) printDFS(current->right); From c2850351681a160d10a766be3cf4bca95cc0cf5c Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Tue, 2 Dec 2025 11:41:46 +0900 Subject: [PATCH 12/20] =?UTF-8?q?=E4=BA=8C=E5=88=86=E6=9C=A8=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Binary trees.md | 40 ++++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index 99fd5f75f..dae006340 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -3,7 +3,7 @@ ### Introduction -A Binary Tree is a type of data structure in which each node has at most two children (left child and right child). Binary trees are used to implement binary search trees and binary heaps, and are used for efficient searching and sorting. A binary tree is a special case of a K-ary tree, where k is 2. Common operations for binary trees include insertion, deletion, and traversal. The difficulty of performing these operations varies if the tree is balanced and also whether the nodes are leaf nodes or branch nodes. For **balanced trees** the depth of the left and right subtrees of every node differ by 1 or less. This allows for a predictable **depth** also known as **height**. This is the measure of a node from root to leaf, where root is 0 and sebsequent nodes are (1,2..n). This can be expressed by the integer part of log2(n) where n is the number of nodes in the tree. +二分木は、各ノードが最大 2 つの子 (左の子と右の子) を持つデータ構造のタイプです。二分木は、二分探索木と二分ヒープの実装に使用され、効率的な検索とソートに使用されます。二分木は、k が 2 である K 分木の特殊なケースです。二分木の一般的な操作には、挿入、削除、トラバーサルがあります。これらの操作を実行する難易度は、木がバランスが取れているかどうか、またノードがリーフ ノードであるかブランチ ノードであるかによって異なります。**バランスの取れた木** では、各ノードの左と右のサブツリーの深さの差は 1 以下です。これにより、**深さ** (高さ** とも呼ばれる) を予測できるようになります。これは、ルートからリーフまでのノードの測定値で、ルートが 0、後続のノードが (1,2..n) です。これは、log2(n) の整数部分で表すことができます。ここで、n は木内のノードの数です。 g s 9 / \ / \ / \ @@ -11,15 +11,15 @@ A Binary Tree is a type of data structure in which each node has at most two chi / \ / \ / \ c d t y 11 15 -The operations performed on trees requires searching in one of two main ways: Depth First Search and Breadth-first search. **Depth-first search (DFS)** is an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking. There are three types of depth first search traversal: **pre-order** visit, left, right, **in-order** left, visit, right, **post-order** left, right, visit. **Breadth-first search (BFS)** is an algorithm for traversing or searching tree or graph structures. In level-order, where we visit every node on a level before going to a lower level.
+ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿って可能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
演習 ---- -Below is an implementation of a binary tree that has insertion and printing capabilities. This tree is ordered but not balanced. This example maintains its ordering at insertion time. +以下は、挿入と出力の機能を持つ二分木の実装です。この木は順序付けされていますが、バランスはとれていません。この例では、挿入時に順序付けが維持されます。 -Change the print routine to depth-first search **pre-order**. +出力ルーチンを深さ優先探索の **pre-order** に変更してください。 チュートリアル コード @@ -42,7 +42,7 @@ Change the print routine to depth-first search **pre-order**. int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -60,7 +60,7 @@ Change the print routine to depth-first search **pre-order**. { if (tree->val == 0) { - /* insert on current (empty) position */ + /* 現在の(空の)位置に挿入する */ tree->val = val; } else @@ -75,7 +75,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -85,7 +85,7 @@ Change the print routine to depth-first search **pre-order**. { if (val >= tree->val) { - /* insert right */ + /* 右に挿入 */ if (tree->right != NULL) { insert(tree->right,val); @@ -93,7 +93,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -103,11 +103,11 @@ Change the print routine to depth-first search **pre-order**. } } - /* depth-first search */ + /* 深さ優先探索 */ void printDFS(node_t * current) { - /* change the code here */ - if (current == NULL) return; /* security measure */ + /* ここでコードを変更してください */ + if (current == NULL) return; /* セキュリティ対策 */ if (current->left != NULL) printDFS(current->left); if (current != NULL) printf("%d ", current->val); if (current->right != NULL) printDFS(current->right); @@ -139,7 +139,7 @@ Change the print routine to depth-first search **pre-order**. int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -157,7 +157,7 @@ Change the print routine to depth-first search **pre-order**. { if (tree->val == 0) { - /* insert on current (empty) position */ + /* 現在の(空の)位置に挿入する */ tree->val = val; } else @@ -172,7 +172,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -182,7 +182,7 @@ Change the print routine to depth-first search **pre-order**. { if (val >= tree->val) { - /* insert right */ + /* 右に挿入 */ if (tree->right != NULL) { insert(tree->right,val); @@ -190,7 +190,7 @@ Change the print routine to depth-first search **pre-order**. else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* set values explicitly, alternative would be calloc() */ + /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -200,11 +200,11 @@ Change the print routine to depth-first search **pre-order**. } } - /* depth-first search */ + /* 深さ優先探索 */ void printDFS(node_t * current) { - /* change the code here */ - if (current == NULL) return; /* security measure */ + /* ここでコードを変更してください */ + if (current == NULL) return; /* セキュリティ対策 */ if (current != NULL) printf("%d ", current->val); if (current->left != NULL) printDFS(current->left); if (current->right != NULL) printDFS(current->right); From 7e8ce1b049d01e2076ed093ce414ea7a8b68877f Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Wed, 3 Dec 2025 12:10:15 +0900 Subject: [PATCH 13/20] =?UTF-8?q?=E8=8B=A5=E5=B9=B2=E3=81=AE=E8=A8=82?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Binary trees.md | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index fa37928c8..c0769e223 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -3,7 +3,6 @@ ### Introduction -二分木は、各ノードが最大 2 つの子 (左の子と右の子) を持つデータ構造のタイプです。二分木は、二分探索木と二分ヒープの実装に使用され、効率的な検索とソートに使用されます。二分木は、k が 2 である K 分木の特殊なケースです。二分木の一般的な操作には、挿入、削除、トラバーサルがあります。これらの操作を実行する難易度は、木がバランスが取れているかどうか、またノードがリーフ ノードであるかブランチ ノードであるかによって異なります。**バランスの取れた木** では、各ノードの左と右のサブツリーの深さの差は 1 以下です。これにより、**深さ** (高さ** とも呼ばれる) を予測できるようになります。これは、ルートからリーフまでのノードの測定値で、ルートが 0、後続のノードが (1,2..n) です。これは、log2(n) の整数部分で表すことができます。ここで、n は木内のノードの数です。 二分木は、各ノードが最大 2 つの子 (左の子と右の子) を持つデータ構造のタイプです。二分木は、二分探索木と二分ヒープの実装に使用され、効率的な検索とソートに使用されます。二分木は、k が 2 である K 分木の特殊なケースです。二分木の一般的な操作には、挿入、削除、トラバーサルがあります。これらの操作を実行する難易度は、木がバランスが取れているかどうか、またノードがリーフ ノードであるかブランチ ノードであるかによって異なります。**バランスの取れた木** では、各ノードの左と右のサブツリーの深さの差は 1 以下です。これにより、**深さ** (高さ** とも呼ばれる) を予測できるようになります。これは、ルートからリーフまでのノードの測定値で、ルートが 0、後続のノードが (1,2..n) です。これは、log2(n) の整数部分で表すことができます。ここで、n は木内のノードの数です。 g s 9 @@ -13,17 +12,14 @@ c d t y 11 15 ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿って可能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
-ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿って可能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
演習 ---- -以下は、挿入と出力の機能を持つ二分木の実装です。この木は順序付けされていますが、バランスはとれていません。この例では、挿入時に順序付けが維持されます。 以下は、挿入と出力の機能を持つ二分木の実装です。この木は順序付けされていますが、バランスはとれていません。この例では、挿入時に順序付けが維持されます。 出力ルーチンを深さ優先探索の **pre-order** に変更してください。 -出力ルーチンを深さ優先探索の **pre-order** に変更してください。 チュートリアル コード @@ -47,7 +43,6 @@ { node_t * test_list = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -81,7 +76,6 @@ { tree->left = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -101,7 +95,6 @@ { tree->right = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -115,8 +108,6 @@ /* 深さ優先探索 */ void printDFS(node_t * current) { - /* ここでコードを変更してください */ - if (current == NULL) return; /* セキュリティ対策 */ /* ここでコードを変更してください */ if (current == NULL) return; /* セキュリティ対策 */ if (current->left != NULL) printDFS(current->left); @@ -151,7 +142,6 @@ { node_t * test_list = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -169,7 +159,6 @@ { if (tree->val == 0) { - /* 現在の(空の)位置に挿入する */ /* 現在の(空の)位置に挿入する */ tree->val = val; } @@ -186,7 +175,6 @@ { tree->left = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -196,7 +184,6 @@ { if (val >= tree->val) { - /* 右に挿入 */ /* 右に挿入 */ if (tree->right != NULL) { @@ -206,7 +193,6 @@ { tree->right = (node_t *) malloc(sizeof(node_t)); /* 値を明示的に設定する代わりにcalloc()を使用する */ - /* 値を明示的に設定する代わりにcalloc()を使用する */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -220,7 +206,7 @@ void printDFS(node_t * current) { /* change the code here */ - if (current == NULL) return; /* security measure */ + if (current == NULL) return; /* セキュリティ対策 */ if (current != NULL) printf("%d ", current->val); if (current->left != NULL) printDFS(current->left); if (current->right != NULL) printDFS(current->right); From 686a8441fb178fa9f1b0e625385be1d96893133a Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Wed, 3 Dec 2025 12:12:20 +0900 Subject: [PATCH 14/20] =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E9=96=93=E9=81=95?= =?UTF-8?q?=E3=81=84=E3=82=92=E8=A8=82=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Binary trees.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index c0769e223..1d62dec4c 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -42,7 +42,7 @@ int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -75,7 +75,7 @@ else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -94,7 +94,7 @@ else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -104,7 +104,6 @@ } } - /* 深さ優先探索 */ /* 深さ優先探索 */ void printDFS(node_t * current) { @@ -141,7 +140,7 @@ int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -174,7 +173,7 @@ else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -192,7 +191,7 @@ else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりにcalloc()を使用する */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; From c93ea99d6c4d35d43c6bb482e88d585cbe7e1c32 Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Thu, 4 Dec 2025 07:50:22 +0900 Subject: [PATCH 15/20] =?UTF-8?q?=E8=A8=B3=E6=96=87=E5=B0=8F=E3=82=A2?= =?UTF-8?q?=E3=83=83=E3=83=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Binary trees.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index 1d62dec4c..00aa732b7 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -11,7 +11,7 @@ / \ / \ / \ c d t y 11 15 -ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿って可能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
+ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿ってあり能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
演習 @@ -42,7 +42,7 @@ int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -75,7 +75,7 @@ else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -94,7 +94,7 @@ else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; @@ -140,7 +140,7 @@ int main() { node_t * test_list = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ test_list->val = 0; test_list->left = NULL; test_list->right = NULL; @@ -173,7 +173,7 @@ else { tree->left = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ tree->left->val = val; tree->left->left = NULL; tree->left->right = NULL; @@ -191,7 +191,7 @@ else { tree->right = (node_t *) malloc(sizeof(node_t)); - /* 値を明示的に設定する代わりに、calloc()を使用する方法も可 */ + /* 値を明示的に設定する代わりに、calloc()を使用する方法もあり */ tree->right->val = val; tree->right->left = NULL; tree->right->right = NULL; From 562ecb89ae3d04fcd1e29ddea7157e4b49d8f296 Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Wed, 17 Dec 2025 17:48:54 +0900 Subject: [PATCH 16/20] =?UTF-8?q?=E5=85=B1=E7=94=A8=E4=BD=93=E3=81=AE?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Unions.md | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tutorials/learn-c.org/ja/Unions.md b/tutorials/learn-c.org/ja/Unions.md index 82e35a0b4..ab44fba17 100644 --- a/tutorials/learn-c.org/ja/Unions.md +++ b/tutorials/learn-c.org/ja/Unions.md @@ -1,37 +1,37 @@ チュートリアル ------------- -C Unions are essentially the same as C Structures, except that instead of containing multiple variables each with their own memory a Union allows for multiple names to the same variable. These names can treat the memory as different types (and the size of the union will be the size of the largest type, + any padding the compiler might decide to give it) +Cの共用体はCの構造体と基本的に同じですが、複数の変数(それぞれが別々のメモリ領域に割り当てられる)を格納する代わりに、共用体では同じ一つの変数に、複数の名前を付けることができます。それにより、その変数に割り当てられたメモリ領域を、名前ごとに異なる型として扱うことができます(共用体のサイズは、最大の型のサイズに、コンパイラが決定するパディングを加えたものになります)。 -So if you wanted to be able to read a variable's memory in different ways, for example read an integer one byte at a time, you could have something like this: +したがって、変数に割り当てられたメモリをさまざまな方法で読み取りたい場合、たとえば整数を入れた 4 バイトのメモリ領域を 1 バイトずつに分けて読み取りたい場合、次のようにすれば可能です。 union intParts { int theInt; char bytes[sizeof(int)]; }; -Allowing you to look at each byte individually without casting a pointer and using pointer arithmetic: +ポインタをキャストしたりポインタ演算を使用したりせずに、各バイトを個別に確認できるようになります。 union intParts parts; - parts.theInt = 5968145; // arbitrary number > 255 (1 byte) + parts.theInt = 5968145; // 任意の数 > 255 (1 byte) printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", - parts.theInt, parts.bytes[0], parts.bytes[1], parts.bytes[2], parts.bytes[3]); + parts.theInt, parts.bytes[0], parts.bytes[1], parts.bytes[2], parts.bytes[3]); // vs int theInt = parts.theInt; printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", - theInt, *((char*)&theInt+0), *((char*)&theInt+1), *((char*)&theInt+2), *((char*)&theInt+3)); + theInt, *((char*)&theInt+0), *((char*)&theInt+1), *((char*)&theInt+2), *((char*)&theInt+3)); - // or with array syntax which can be a tiny bit nicer sometimes + // あるいは配列構文を使ってもよい……その方が、少しだけわかりやすくなるかも printf("The int is %i\nThe bytes are [%i, %i, %i, %i]\n", theInt, ((char*)&theInt)[0], ((char*)&theInt)[1], ((char*)&theInt)[2], ((char*)&theInt)[3]); -Combining this with a structure allows you to create a "tagged" union which can be used to store multiple different types, one at a time. +これを構造体と組み合わせることで、「タグ付き」共用体を作成し、複数の異なる型を1つずつ格納できるようになります。 -For example, you might have a "number" struct, but you don't want to use something like this: +例えば、「数」の構造体が必要だけれど、次のようにはしたくないとします。 struct operator { int intNum; @@ -40,7 +40,7 @@ For example, you might have a "number" struct, but you don't want to use somethi double doubleNum; }; -Because your program has a lot of them and it takes a bit too much memory for all of the variables, so you could use this: +なぜなら、プログラムには変数が多数含まれており、そのすべての変数に必要なメモリは、ちょっとばかり多すぎるから。で、そういうことなら、次のような構造体が使えるかもしれません。 struct operator { int type; @@ -51,15 +51,15 @@ Because your program has a lot of them and it takes a bit too much memory for al } types; }; -Like this the size of the struct is just the size of the int `type` + the size of the largest type in the union (the double). Not a huge gain, only 8 or 16 bytes, but the concept can be applied to similar structs. +この場合、構造体のサイズは int `type` のサイズ + 共用体内の最大の型(double)のサイズになります。ここでは8バイトか16バイトしか節約できませんが、このコンセプトを同様の構造体に適用することはできます。 -use: +使ってみる: operator op; - op.type = 0; // int, probably better as an enum or macro constant + op.type = 0; // この0は「数」の型がintであること示します。本来は、列挙型またはマクロ定数を使って、0がintであることを、わかりやすくするべきです。 op.types.intNum = 352; -Also, if you don't give the union a name then it's members are accessed directly from the struct: +また、ユニオンに名前を付けない場合、そのメンバーは構造体から直接アクセスされます。 struct operator { int type; @@ -72,10 +72,10 @@ Also, if you don't give the union a name then it's members are accessed directly operator op; op.type = 0; // int - // intNum is part of the union, but since it's not named you access it directly off the struct itself + // intNum は共用体の一部ですが、名前が付いていないため、構造体自体から直接アクセスします。 op.intNum = 352; -Another, perhaps more useful feature, is when you always have multiple variables of the same type, and you want to be able to use both names (for readability) and indexes (for ease of iteration), in that case you can do something like this: +もう 1 つの、おそらくより便利な機能は、常に同じ型の複数の変数があり、名前 (読みやすさのため) とインデックス (反復処理を容易にするため) の両方を使用できるようにしたい場合です。その場合は、次のようにすることができます。 union Coins { struct { @@ -83,18 +83,18 @@ Another, perhaps more useful feature, is when you always have multiple variables int dime; int nickel; int penny; - }; // anonymous struct acts the same way as an anonymous union, members are on the outer container + }; // 匿名の構造体は匿名の共用体と同じように動作し、メンバーには外側のコンテナから直接アクセスします。 int coins[4]; }; -In that example you can see that there is a struct which contains the four (common) coins in the United States. +この例では、アメリカ合衆国で一般的に使用される4種類のコインを含む構造体があることがわかります。 -since the union makes the variables share the same memory the coins array matches with each int in the struct (in order): +union によって変数が同じメモリを共有するため、coins 配列は構造体内の各 int と(順番に)一致します。 union Coins change; for(int i = 0; i < sizeof(change) / sizeof(int); ++i) { - scanf("%i", change.coins + i); // BAD code! input is always suspect! + scanf("%i", change.coins + i); // これはよくないコードです! 入力値が常に正当とは限りません! } printf("There are %i quarters, %i dimes, %i nickels, and %i pennies\n", change.quarter, change.dime, change.nickel, change.penny); @@ -103,24 +103,24 @@ since the union makes the variables share the same memory the coins array matche 演習 ---- -Create a union that stores an array of 21 characters and 6 ints (6 since 21 / 4 == 5, but 5 * 4 == 20 so you need 1 more for the purpose of this exercise), you will set the integers to 6 given values and then print out the character array both as a series of chars and as a string. +21 個の文字と 6 個の整数 (21 / 4 == 5 なので 6 個ですが、5 * 4 == 20 なので、この演習ではあと 1 個必要です) の配列を格納する共用体を作成し、整数を 6 つの指定された値に設定してから、文字配列を一連の文字と文字列の両方として出力します。 チュートリアル コード ------------------- #include - /* define the union here */ + /* ここで共用体を定義します */ int main() { - // initializer lists like this are assigned to the first member of the union/struct! - // There are 6 ints here so... + // このような初期化リストは、共用体/構造体の最初のメンバーに割り当てられます。 + // ここには 6 つの int があるので... intCharacters = {{1853169737, 1936876900, 1684955508, 1768838432, 561213039, 0}}; - /* testing code */ + /* テストコード */ printf("["); - // only go to 18 because 1 byte is for the terminating 0 and we don't print the last in the loop + // 1バイトは終了の0用であり、ループの最後は出力しないため、18までしか進みません。 for(int i = 0; i < 19; ++i) printf("%c, ", intCharacters.chars[i]); printf("%c]\n", intCharacters.chars[19]); @@ -148,7 +148,7 @@ Create a union that stores an array of 21 characters and 6 ints (6 since 21 / 4 union hiddenMessage intCharacters = {{1853169737, 1936876900, 1684955508, 1768838432, 561213039, 0}}; printf("["); - // only go to 18 because 1 byte is for the terminating 0 and we don't print the last in the loop + // 1バイトは終了の0用であり、ループの最後は出力しないため、18までしか進みません。 for(int i = 0; i < 19; ++i) printf("%c, ", intCharacters.chars[i]); printf("%c]\n", intCharacters.chars[19]); From e4f6b384dc9a337c2b3d23eb8a0878e74b363816 Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Wed, 17 Dec 2025 21:36:48 +0900 Subject: [PATCH 17/20] =?UTF-8?q?=E3=83=9D=E3=82=A4=E3=83=B3=E3=82=BF?= =?UTF-8?q?=E6=BC=94=E7=AE=97=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Pointer Arithmetics.md | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/tutorials/learn-c.org/ja/Pointer Arithmetics.md b/tutorials/learn-c.org/ja/Pointer Arithmetics.md index 616487623..10f64f648 100644 --- a/tutorials/learn-c.org/ja/Pointer Arithmetics.md +++ b/tutorials/learn-c.org/ja/Pointer Arithmetics.md @@ -1,12 +1,12 @@ チュートリアル ------------- -You previously learned what is a pointer and how to manipulate pointers. In this tutorial you will be learning the arithmetic operations on pointers. -There are multiple arithmetic operations that can be applied on C pointers: ++, --, -, + +以前、ポインタとは何か、そしてポインタの操作方法を学びました。このチュートリアルでは、ポインタに対する算術演算について学習します。 +C言語のポインタには、++、--、-、+ といった複数の算術演算を適用できます。 -### Incrementing a Pointer with (++) +### (++) によるポインタのインクリメント -Just like any variable the ++ operation increases the value of that variable. In our case here the variable is a pointer hence when we increase its value we are increasing the address in the memory that pointer points to. -Let's combine this operation with an array in our example: +他の変数と同様に、++ 演算はその変数の値を増加させます。今回の場合、変数はポインタなので、その値を増加させると、ポインタが指すメモリ内のアドレスが増加します。 +この演算を例に、配列と組み合わせてみましょう: #include @@ -19,19 +19,19 @@ Let's combine this operation with an array in our example: printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]); - int *intpointer = &intarray[3]; //point to the 4th element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 4th element + int *intpointer = &intarray[3]; // 配列の4番目の要素を指す + printf("address: %x - has value %d\n", intpointer, *intpointer); // 4番目の要素のアドレスを出力する - intpointer++; //now increase the pointer's address so it points to the 5th elemnt in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + intpointer++; // ポインタのアドレスを増やして配列の5番目の要素を指すようにします。 + printf("address: %x - has value %d\n", intpointer, *intpointer); // 5番目の要素のアドレスを出力する return 0; } -### Decreasing a Pointer with (--) +### (--) によるポインタの減少 -Just like in our previous example we increased the pointer's pointed-to address by one using the ++ operator, we can decrease the address pointed-to by one using the decrement operator (--). +前の例で ++ 演算子を使用してポインターの指すアドレスを 1 つ増やしたのと同様に、デクリメント演算子 (--) を使用して、指すアドレスを 1 つ減らすことができます。 #include @@ -44,17 +44,18 @@ Just like in our previous example we increased the pointer's pointed-to address printf("intarray[%d] has value %d - and address @ %x\n", i, intarray[i], &intarray[i]); - int *intpointer = &intarray[4]; //point to the 5th element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + int *intpointer = &intarray[4]; // 配列の5番目の要素を指す + printf("address: %x - has value %d\n", intpointer, *intpointer); // 5番目の要素のアドレスを出力する - intpointer--; //now decrease the point's address so it points to the 4th element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 4th element + intpointer--; // ポイントのアドレスを減らして、配列の4番目の要素を指すようにする + printf("address: %x - has value %d\n", intpointer, *intpointer); // 4番目の要素のアドレスを出力する return 0; } -### Adding Pointers with (+) -We previously increased a pointer's pointed-to address by one. We can also increase it by an integer value such: +### (+) でポインタを追加する + +先ほど、ポインタの指すアドレスを1ずつ増やしました。次のように整数値で増やすこともできます。 #include @@ -67,22 +68,22 @@ We previously increased a pointer's pointed-to address by one. We can also incre printf("intarray[%d] has value: %d - and address @ %x\n", i, intarray[i], &intarray[i]); - int *intpointer = &intarray[1]; //point to the 2nd element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 2nd element + int *intpointer = &intarray[1]; // 配列の2番目の要素を指す + printf("address: %x - has value %d\n", intpointer, *intpointer); // 2番目の要素のアドレスを出力する - intpointer += 2; //now shift by two the point's address so it points to the 4th element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the addres of the 4th element + intpointer += 2; // ポイントのアドレスを2つシフトして、配列の4番目の要素を指すようにします。 + printf("address: %x - has value %d\n", intpointer, *intpointer); // 4番目の要素のアドレスを出力する return 0; } -Note how in the output the address shifted by 8 steps in the memory. You might be wondering why? -The answer is simple: Because our pointer is an int-pointer and the size of an int variable is 4 bytes the memory is shift-able by 4 blocks. -In our code we shifted by 2 (added +2) to the initial address so that makes them 2 x 4 byte = 8. +出力では、メモリ内でアドレスが8ステップシフトしていることに注目してください。なぜだろうと疑問に思うかもしれません。 +答えは簡単です。ポインタがint型ポインタであり、int型変数のサイズが4バイトであるため、メモリは4ブロックシフト可能です。 +コードでは、最初のアドレスに2(+2)シフトしたので、2 x 4バイト = 8になります。 -### Subtracting Pointers with (-) +### (-) によるポインタの減算 -Similarly we can subtract: +同様に引き算もできます: #include @@ -95,23 +96,24 @@ Similarly we can subtract: printf("intarray[%d] has value: %d - and address @ %x\n", i, intarray[i], &intarray[i]); - int *intpointer = &intarray[4]; //point to the 5th element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 5th element + int *intpointer = &intarray[4]; // 配列の5番目の要素を指す + printf("address: %x - has value %d\n", intpointer, *intpointer); // 5番目の要素のアドレスを出力する - intpointer -= 2; //now shift by two the point's address so it points to the 3rd element in the array - printf("address: %x - has value %d\n", intpointer, *intpointer); //print the address of the 3rd element + intpointer -= 2; // ポイントのアドレスを2つシフトして、配列の3番目の要素を指すようにする + printf("address: %x - has value %d\n", intpointer, *intpointer); // 3番目の要素のアドレスを出力する return 0; } -again the address is shifted by blocks of 4bytes (in case of int). +ここでも、アドレスは 4 バイトのブロック単位でシフトされます (int の場合)。 + +### その他の操作 -### Other Operations -There are more operations such as comparison >, <, ==. The idea is very similar of comparing variables, but in this case we are comparing memory address. +他にも比較演算(>、<、==など)があります。考え方は変数の比較と非常に似ていますが、この場合はメモリアドレスを比較します。 演習 ---- -Copy last three addresses of intarray into parray which is an array of pointers to an int. +intarray の最後の 3 つのアドレスを、int へのポインターの配列である parray にコピーします。 チュートリアル コード ------------------- @@ -122,17 +124,17 @@ Copy last three addresses of intarray into parray which is an array of pointers //-----------------------^ int *pointer = &intarray[2]; - // Array of 3 pointers + // 3つのポインタの配列 int *parray[3]; - // Copy last three addresses of intarray into parray - // Use parray and pointer + // intarray の最後の3つのアドレスを parray にコピーします + // parray とポインタを使用します int i; for (i = 0; i < 3; i++) { // Insert code here } - // Test code + // テスト コード for (i = 0; i < 3; i++) { if (parray[i] == &pointer[i]) { printf("Matched!\n"); From 64b6649adb9924198b954d3c8ac1d76d61533b27 Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Fri, 19 Dec 2025 21:28:50 +0900 Subject: [PATCH 18/20] =?UTF-8?q?=E6=9C=80=E5=BE=8C=E3=81=BE=E3=81=A7?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Bitmasks.md | 66 ++++++++++--------- tutorials/learn-c.org/ja/Function Pointers.md | 43 ++++++------ 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/tutorials/learn-c.org/ja/Bitmasks.md b/tutorials/learn-c.org/ja/Bitmasks.md index 47d66dbc0..5af870066 100644 --- a/tutorials/learn-c.org/ja/Bitmasks.md +++ b/tutorials/learn-c.org/ja/Bitmasks.md @@ -1,28 +1,28 @@ チュートリアル ------------- -Bit masking is simply the process of storing data truly as bits, as opposed to storing it as chars/ints/floats. It is incredibly useful for storing certain types of data compactly and efficiently. +ビットマスクとは、データをchar/int/float型ではなく、ビットとして保存するプロセスです。特定の種類のデータをコンパクトかつ効率的に保存するのに非常に便利です。 -The idea for bit masking is based on boolean logic. For those not familiar, boolean logic is the manipulation of 'true' (1) and 'false' (0) through logical operations (that take 0s and 1s as their argument). We are concerned with the following operations: +ビットマスクの考え方はブール論理に基づいています。ブール論理とは、論理演算(0と1を引数として取る)によって「真」(1)と「偽」(0)を操作することです。ここでは以下の演算について考えます。 -* NOT a - the final value is the opposite of the input value (1 -> 0, 0 -> 1) -* a AND b - if both values are 1, the final value is 1, otherwise the final value is 0 -* a OR b - if either value is 1, the final value is 1, otherwise the final value is 0 -* a XOR b - if one value is 1 and the other value is 0, the final value is 1, otherwise the final value is 0 +* NOT a - 最終値は入力値の反対になります (1 -> 0、0 -> 1) +* a AND b - 両方の値が 1 の場合、最終値は 1 になります。それ以外の場合は、最終値は 0 になります。 +* a OR b - どちらかの値が 1 の場合、最終値は 1 になります。それ以外の場合は、最終値は 0 になります。 +* a XOR b - 一方の値が 1 でもう一方の値が 0 の場合、最終値は 1 になります。それ以外の場合は、最終値は 0 になります。 -In computing, one of these true/false values is a *bit*. Primitives in C (`int`, `float`, etc) are made up of some number of bits, where that number is a multiple of 8. For example, an `int` may be at least 16 bits in size, where a `char` may be 8 bits. 8 bits is typically referred to as a *byte*. C guarantees that certain primitives are [at least some number](http://en.wikipedia.org/wiki/C_data_types#Basic_types) of bytes in size. The introduction of `stdint.h` in C11 allows the programmer to specify integer types that are exactly some number of bytes, which is extremely useful when using masks. +コンピューティングにおいて、これらの真偽値の1つは*ビット*です。C言語のプリミティブ(`int`、`float`など)は、8の倍数のビット数で構成されます。例えば、`int`は少なくとも16ビットのサイズであるのに対し、`char`は8ビットです。8ビットは通常*バイト*と呼ばれます。C言語では、特定のプリミティブが[少なくともあるバイト数](http://en.wikipedia.org/wiki/C_data_types#Basic_types)バイトのサイズであることが保証されています。C11で導入された`stdint.h`により、プログラマーは正確にあるバイト数の整数型を指定できるようになりました。これはマスクを使用する際に非常に便利です。 -Bit masks are often used when setting flags. Flags are values that can be in two states, such as 'on/off' and 'moving/stationary'. +ビットマスクはフラグを設定する際によく使用されます。フラグとは、「オン/オフ」や「移動/静止」など、2つの状態を取ることができる値です。 -### Setting bit n +### ビット n の設定 -Setting bit `n` is as simple as ORing the value of the storage variable with the value `2^n`. +ビット `n` を設定するのは、ストレージ変数の値と、値 `2^n` の OR 演算を行うのと同じくらい簡単です。 ``` storage |= 1 << n; ``` -As an example, here is the setting of bit 3 where `storage` is a char (8 bits): +例として、`storage` が char (8 ビット) の場合のビット 3 の設定を次に示します。 ``` 01000010 @@ -32,17 +32,17 @@ As an example, here is the setting of bit 3 where `storage` is a char (8 bits): 01001010 ``` -The `2^n` logic places the '1' value at the proper bit in the mask itself, allowing access to that same bit in the storage variable. +`2^n` ロジックは、マスク自体の適切なビットに '1' の値を配置し、ストレージ変数内の同じビットにアクセスできるようにします。 -### Clearing bit n +### ビット n をクリアする -Clearing bit `n` is the result of ANDing the value of the storage variable with the inverse (NOT) of the value `2^n`: +ビット `n` をクリアすることは、ストレージ変数の値と値 `2^n` の逆数 (NOT) との AND 演算の結果です。 ``` storage &= ~(1 << n); ``` -Here's the example again: +もう一度例を挙げます。 ``` 01001010 @@ -52,9 +52,9 @@ Here's the example again: 01000010 ``` -### Flipping bit n +### 反転ビット n -Flipping bit `n` is the result of XORing the value of the storage variable with `2^n`: +ビット `n` の反転は、ストレージ変数の値と `2^n` の XOR 演算の結果です。 ``` storage ^= 1 << n; @@ -68,9 +68,9 @@ storage ^= 1 << n; 01001010 01000010 ``` -### Checking bit n +### ビット n をチェック -Checking a bit is ANDing the value of `2^n` with the bit storage: +ビットをチェックすることは、ビットストレージと `2^n` の値の AND 演算です。 ``` bit = storage & (1 << n); @@ -87,7 +87,7 @@ bit = storage & (1 << n); 演習 ---- -Use bit masks to manipulate some flags. +ビットマスクを使用して一部のフラグを操作します。 チュートリアル コード @@ -95,7 +95,7 @@ Use bit masks to manipulate some flags. #include #include - /* Finish initializing the flags */ + /* フラグの初期化を完了する */ const short FLAG_ON = 1 << 0; // 1 (0x01) const short FLAG_MOVEMENT = 1 << 1; // 2 (0x02) @@ -107,21 +107,21 @@ Use bit masks to manipulate some flags. int main() { short attributes = 0; - /* Set the attributes ON, TRANSPARENT, and BROKEN */ + /* 属性をON、TRANSPARENT、BROKENに設定する */ assert(attributes == (FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN)); - /* Modify (set/clear/toggle) so the only attributes are ON and ALIVE */ + /* 属性がONとALIVEのみになるように変更(設定/クリア/切り替え)します */ assert(attributes == (FLAG_ON | FLAG_ALIVE)); - /* Check if the ALIVE flag is set */ + /* ALIVEフラグが設定されているかどうかを確認する */ assert(/* ??? */); - /* Check if the BROKEN flag is not set */ + /* BROKENフラグが設定されていないか確認する */ assert(/* ??? */); - /* Modify so only the EDIBLE attribute is set */ + /* EDIBLE属性のみが設定されるように変更します */ assert(attributes == FLAG_EDIBLE); @@ -138,7 +138,7 @@ Use bit masks to manipulate some flags. #include #include - /* Finish initializing the flags */ + /* フラグの初期化を完了する */ const short FLAG_ON = 1 << 0; // 1 (0x01) const short FLAG_MOVEMENT = 1 << 1; // 2 (0x02) @@ -150,7 +150,8 @@ Use bit masks to manipulate some flags. int main() { short attributes = 0; - /* Set the attributes ON, TRANSPARENT, and BROKEN */ + /* 属性をON、TRANSPARENT、BROKENに設定する */ + attributes |= FLAG_ON; attributes |= FLAG_TRANSPARENT; attributes |= FLAG_BROKEN; @@ -160,7 +161,8 @@ Use bit masks to manipulate some flags. assert(attributes == (FLAG_ON | FLAG_TRANSPARENT | FLAG_BROKEN)); - /* Modify (set/clear/toggle) so the only attributes are ON and ALIVE */ + /* 属性がONとALIVEのみになるように変更(設定/クリア/切り替え)します */ + attributes &= ~FLAG_TRANSPARENT; // possible: attributes ^= FLAG_TRANSPARENT; attributes ^= FLAG_BROKEN; @@ -169,13 +171,13 @@ Use bit masks to manipulate some flags. assert(attributes == (FLAG_ON | FLAG_ALIVE)); - /* Check if the ALIVE flag is set */ + /* ALIVEフラグが設定されているかどうかを確認する */ assert(attributes & FLAG_ALIVE); - /* Check if the BROKEN flag is not set */ + /* BROKENフラグが設定されていないか確認する */ assert(!(attributes & FLAG_BROKEN)); - /* Modify so only the EDIBLE attribute is set */ + /* EDIBLE属性のみが設定されるように変更します */ attributes = FLAG_EDIBLE; assert(attributes == FLAG_EDIBLE); diff --git a/tutorials/learn-c.org/ja/Function Pointers.md b/tutorials/learn-c.org/ja/Function Pointers.md index 08ad4e0dc..5cbc40c1f 100644 --- a/tutorials/learn-c.org/ja/Function Pointers.md +++ b/tutorials/learn-c.org/ja/Function Pointers.md @@ -1,32 +1,31 @@ チュートリアル ------------- -Remember pointers? We used them to point to an array of chars then make a string out of them. -Then things got more interesting when we learned how to control these pointers. -Now it is time to do something even more interesting with pointers, using them to point to and call functions. +ポインタを覚えていますか?文字の配列を指し示し、それを文字列に変換するためにポインタを使いました。 +その後、ポインタの制御方法を学んだことで、さらに面白くなりました。 +次は、ポインタを使って関数を指し示したり呼び出したりしてみましょう。 -### Why point to a function? +### なぜ関数を指すのか? -The first question that may come to your mind is why would we use pointers to call a function when we can simply call a function by its name: `function();` - that's a great question! Now imagine the `sort` function where you need to sort an array. Sometimes you want to order array elements in an ascending order or descending order. How would you choose? Function pointers! +おそらく最初に頭に浮かぶ疑問は、関数名で関数を呼び出すのになぜポインタを使うのか、ということでしょう。`function();` と書けばいいのですから。これは良い質問です! では、配列をソートする必要がある `sort` 関数を想像してみてください。配列の要素を昇順または降順に並べ替えたい場合があります。どちらを選びますか? 関数ポインタです! - -### Function Pointer Syntax +### 関数ポインタ構文 void (*pf)(int); -I agree with you. This definitely is very complicated, or so you may think. Let's re-read that code and try to understand it point by point. Read it inside-out. `*pf` is the pointer to a function. `void` is the return type of that function, and finally `int` is the argument type of that function. Got it? Good. +ま、そうでしょうね。確かに非常に複雑ですね。少なくとも、そう思われるかもしれません。では、コードをもう一度読んで、一つ一つ理解してみましょう。隅々まで読んでみてください。`*pf` は関数へのポインタです。`void` はその関数の戻り値の型、そして `int` はその関数の引数の型です。分かりましたか?いいですね。 -Let's insert pointers into the function pointer and try to read it again: +関数ポインタにポインタを挿入して、もう一度読み取ってみます: char* (*pf)(int*) -Again: -1. `*pf` is the function pointer. -2. `char*` is the return type of that function. -3. `int*` is the type of the argument. +繰り返します +1. `*pf` は関数ポインタです。 +2. `char*` はその関数の戻り値の型です。 +3. `int*` は引数の型です。 -Ok enough with theory. Let's get our hands dirty with some real code. -See this example: +理論はもう十分です。実際のコードで実際に試してみましょう。 +次の例をご覧ください。 #include void someFunction(int arg) @@ -44,16 +43,16 @@ See this example: printf("Wow that was cool. Back to main now!\n\n"); } -Remember `sort()` we talked about earlier? We can do the same with it. -Instead of ordering a set in an ascending way we can do the opposite using our own comparison function as follows: +先ほど説明した `sort()` を覚えていますか?これを使って同じことができます。 +集合を昇順で並べ替える代わりに、次のように独自の比較関数を使って逆の順序付けを行うことができます。 #include - #include //for qsort() + #include //for qsort() int compare(const void* left, const void* right) { return (*(int*)right - *(int*)left); - // go back to ref if this seems complicated: http://www.cplusplus.com/reference/cstdlib/qsort/ + // 複雑に思える場合は、ref に戻ってください: http://www.cplusplus.com/reference/cstdlib/qsort/ } main() { @@ -71,13 +70,13 @@ Instead of ordering a set in an ascending way we can do the opposite using our o } } -Let's remember again. Why do we use function pointers? -1. To allow programmers to use libraries for different usages -> "Flexibility" +もう一度思い出してみましょう。なぜ関数ポインタを使うのでしょうか? +1. プログラマーが様々な用途でライブラリを利用できるようにするため -> 「柔軟性」 演習 ---- -Complete the array of pointers to functions and call each function using its pointer from the array. Array of pointers to functions? Yes you can do that! +関数へのポインタの配列を完成させ、配列内のポインタを使って各関数を呼び出します。関数へのポインタの配列? はい、できますよ! チュートリアル コード ------------------- From 62742edf8b1dc6938c2ec81a73b26b552cd9aa7a Mon Sep 17 00:00:00 2001 From: ochi Date: Sat, 20 Dec 2025 16:42:32 +0900 Subject: [PATCH 19/20] =?UTF-8?q?Welcome=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/learn-c.org/ja/Welcome.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tutorials/learn-c.org/ja/Welcome.md b/tutorials/learn-c.org/ja/Welcome.md index 88fe1b447..b94848499 100644 --- a/tutorials/learn-c.org/ja/Welcome.md +++ b/tutorials/learn-c.org/ja/Welcome.md @@ -1,14 +1,14 @@ -# Welcome +# ようこそ -Welcome to the learn-c.org free interactive C tutorial. This website is proudly supported by [Boot.dev's Learn Memory Management in C course](https://www.boot.dev/courses/learn-memory-management-c?promo=LEARNXORG). If you'd like to learn how to manage memory in C from start to finish, [become a member and use code LEARNXORG](https://www.boot.dev/pricing?promo=LEARNXORG) for 25% off your first year! +learn-c.org の無料インタラクティブ C チュートリアルへようこそ。このウェブサイトは、[Boot.dev の C 言語におけるメモリ管理を学ぶコース](https://www.boot.dev/courses/learn-memory-management-c?promo=LEARNXORG) によってサポートされています。C 言語でのメモリ管理を最初から最後まで学びたい方は、[メンバー登録をしてコード LEARNXORG を入力する](https://www.boot.dev/pricing?promo=LEARNXORG) で初年度の受講料が 25% オフになります! -Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the C programming language. +経験豊富なプログラマーの方にもそうでない方にも、このウェブサイトは C プログラミング言語を学びたいすべての方を対象としています。 -There is no need to download anything - Just click on the chapter you wish to begin from, and follow the instructions. Good luck! +ダウンロードは不要です。開始したい章をクリックして、指示に従ってください。頑張ってください! -learn-c.org is still under construction - If you wish to contribute tutorials, please click on `Contributing Tutorials` down below. +learn-c.org は現在構築中です。チュートリアルを投稿していただける場合は、下部の「投稿チュートリアル」をクリックしてください。 -### Learn the Basics +### 基本を学ぶ - [[Hello, World!]] - [[Variables and Types]] @@ -21,7 +21,7 @@ learn-c.org is still under construction - If you wish to contribute tutorials, p - [[Functions]] - [[Static]] -### Advanced +### 一歩進んで - [[Pointers]] - [[Structures]] @@ -36,7 +36,7 @@ learn-c.org is still under construction - If you wish to contribute tutorials, p - [[Function Pointers]] - [[Bitmasks]] -### Contributing Tutorials +### チュートリアルへの貢献 -Read more here: [[Contributing Tutorials]] +詳細はこちら: [[Contributing Tutorials]] From b863dbc19be8d07c6b1e5012a7532019671c9b31 Mon Sep 17 00:00:00 2001 From: Osamu377 Date: Tue, 6 Jan 2026 21:21:37 +0900 Subject: [PATCH 20/20] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E6=A7=8B=E6=88=90=E3=81=AE=E3=83=AA=E3=83=BC=E3=83=89=E6=96=87?= =?UTF-8?q?=E3=82=92=E8=8B=B1=E8=AA=9E=E3=81=AB=E6=88=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../learn-c.org/ja/Arrays and Pointers.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Arrays.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Binary trees.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Bitmasks.md | 17 +++++++++-------- tutorials/learn-c.org/ja/Conditions.md | 16 ++++++++-------- .../learn-c.org/ja/Dynamic allocation.md | 16 ++++++++-------- tutorials/learn-c.org/ja/For loops.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Function Pointers.md | 17 +++++++++-------- .../ja/Function arguments by reference.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Functions.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Hello, World!.md | 12 ++++++------ tutorials/learn-c.org/ja/Linked lists.md | 16 ++++++++-------- .../learn-c.org/ja/Multidimensional Arrays.md | 16 ++++++++-------- .../learn-c.org/ja/Pointer Arithmetics.md | 19 +++++++++++-------- tutorials/learn-c.org/ja/Pointers.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Recursion.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Static.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Strings.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Structures.md | 16 ++++++++-------- tutorials/learn-c.org/ja/Unions.md | 16 ++++++++-------- .../learn-c.org/ja/Variables and Types.md | 16 ++++++++-------- tutorials/learn-c.org/ja/While loops.md | 16 ++++++++-------- 22 files changed, 179 insertions(+), 174 deletions(-) diff --git a/tutorials/learn-c.org/ja/Arrays and Pointers.md b/tutorials/learn-c.org/ja/Arrays and Pointers.md index 303b7fba8..4dc30cd3e 100644 --- a/tutorials/learn-c.org/ja/Arrays and Pointers.md +++ b/tutorials/learn-c.org/ja/Arrays and Pointers.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- 以前の[[ポインタ]]に関するチュートリアルでは、特定のデータ型へのポインタは、そのデータ型の任意の変数のアドレスを格納できることを学びました。例えば、次のコードでは、ポインタ変数 `pc` は文字変数 `c` のアドレスを格納しています。 @@ -123,8 +123,8 @@ free(pvowels); -演習 ----- +Exercise +-------- [パスカルの三角形](http://mathworld.wolfram.com/PascalsTriangle.html)の最初の7行を以下に示します。行*i*には*i*個の要素が含まれていることに注意してください。したがって、最初の3行の数値を格納するには、1 + 2 + 3 = 6個のメモリスロットが必要になります。 @@ -145,8 +145,8 @@ パスカルの三角形の最初の3行の数値を、動的メモリ割り当てを用いて、2次元「配列」に格納するためのスケルトンコードを以下に示す。これらの6つの数値を格納するには、正確に6つのメモリスロットを割り当てる必要があることに注意する。余分なメモリは割り当てない。プログラムの最後に、このプログラムで使用したすべてのメモリブロックを解放する。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include #include @@ -184,14 +184,14 @@ return 0; } -期待している出力 +Expected Output --------------- 1 11 121 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Arrays.md b/tutorials/learn-c.org/ja/Arrays.md index de5c4e9d2..9c938708d 100644 --- a/tutorials/learn-c.org/ja/Arrays.md +++ b/tutorials/learn-c.org/ja/Arrays.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- 配列は、同じ変数名で複数の値をインデックスで整理して保持できる特殊な変数です。配列は非常に簡単な構文で定義されます。 @@ -25,14 +25,14 @@ 配列はコンピュータのメモリ内で、『値が等間隔に整列したもの』として実装されるため、1種類の変数しか持つことができません。 そのため、特定の配列セルへのアクセスは非常に効率的です。 -演習 ----- +Exercise +-------- * 以下のコードは `grades` 変数が欠落しているためコンパイルできません。 * 成績の1つが欠落しています。成績平均が85になるように定義できますか? -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -51,12 +51,12 @@ return 0; } -期待している出力 +Expected Output --------------- The average of the 3 grades is: 85 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Binary trees.md b/tutorials/learn-c.org/ja/Binary trees.md index 00aa732b7..eeb489c5b 100644 --- a/tutorials/learn-c.org/ja/Binary trees.md +++ b/tutorials/learn-c.org/ja/Binary trees.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### Introduction @@ -14,16 +14,16 @@ ツリーに対して実行される操作には、深さ優先探索と幅優先探索という 2 つの主な方法のいずれかによる検索が必要です。**深さ優先探索 (DFS)** は、ツリーまたはグラフのデータ構造を走査または検索するためのアルゴリズムです。ルートから開始し、各ブランチに沿ってあり能な限り探索してからバックトラックします。深さ優先探索の走査には、**前順序** 訪問、左、右、**内順序** 訪問、右、**後順序** 左、右、訪問の 3 つの種類があります。**幅優先探索 (BFS)** は、ツリーまたはグラフの構造を走査または検索するためのアルゴリズムです。レベル順序では、下位レベルに移動する前に、そのレベルのすべてのノードを訪問します。
-演習 ----- +Exercise +-------- 以下は、挿入と出力の機能を持つ二分木の実装です。この木は順序付けされていますが、バランスはとれていません。この例では、挿入時に順序付けが維持されます。 出力ルーチンを深さ優先探索の **pre-order** に変更してください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include #include @@ -115,12 +115,12 @@ } -期待している出力 +Expected Output --------------- 5 4 3 8 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Bitmasks.md b/tutorials/learn-c.org/ja/Bitmasks.md index 5af870066..38103ac98 100644 --- a/tutorials/learn-c.org/ja/Bitmasks.md +++ b/tutorials/learn-c.org/ja/Bitmasks.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ビットマスクとは、データをchar/int/float型ではなく、ビットとして保存するプロセスです。特定の種類のデータをコンパクトかつ効率的に保存するのに非常に便利です。 @@ -84,14 +84,15 @@ bit = storage & (1 << n); 00000000 00001000 ``` -演習 ----- +Exercise +-------- ビットマスクを使用して一部のフラグを操作します。 -チュートリアル コード -------------------- +Tutorial Code +------------- + #include #include @@ -129,11 +130,11 @@ bit = storage & (1 << n); } -期待している出力 +Expected Output --------------- Done! -解答 +Solution ---- #include #include diff --git a/tutorials/learn-c.org/ja/Conditions.md b/tutorials/learn-c.org/ja/Conditions.md index bda52dc6f..d808c3487 100644 --- a/tutorials/learn-c.org/ja/Conditions.md +++ b/tutorials/learn-c.org/ja/Conditions.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### 決断を下す @@ -100,15 +100,15 @@ NOT 演算子 `!` も同様に使用できます。 } -演習 ----- +Exercise +-------- この演習では、`guessNumber` 関数ステートメント内に、数値 `guess` が 555 に等しいかどうかを確認する `if` ステートメントを作成する必要があります。等しい場合、関数は `printf` を使用して「正解です。お察しの通りです!」と出力する必要があります。`guess` が 555 未満の場合、関数は `printf` を使用して「あなたの推測は低すぎます。」と出力する必要があります。`guess` が 555 より大きい場合、関数は `printf` を使用して「あなたの推測は高すぎます。」と出力する必要があります。 * **重要**: printf 文字列の最後に改行文字 `\n` を追加することを忘れないでください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -122,14 +122,14 @@ NOT 演算子 `!` も同様に使用できます。 guessNumber(555); } -期待している出力 +Expected Output --------------- Your guess is too low. Your guess is too high. Correct. You guessed it! -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Dynamic allocation.md b/tutorials/learn-c.org/ja/Dynamic allocation.md index cf0f6d9b6..a970054bd 100644 --- a/tutorials/learn-c.org/ja/Dynamic allocation.md +++ b/tutorials/learn-c.org/ja/Dynamic allocation.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- メモリの動的割り当てはC言語において非常に重要なテーマです。これにより、連結リストなどの複雑なデータ構造を構築できます。メモリを動的に割り当てることで、プログラム作成時にデータのサイズを事前に把握することなく、データを保存できるようになります。 @@ -33,13 +33,13 @@ person 構造体のメンバーにアクセスするには、`->` 表記を使 free は `myperson` 変数自体を削除するのではなく、その変数が指しているデータを解放するだけであることに注意してください。`myperson` 変数はメモリ内のどこかを指したままになりますが、`free(myperson)` を呼び出した後は、その領域にアクセスできなくなります。新しいデータを割り当てるまでは、そのポインタを再び使用してはいけません。 -演習 ----- +Exercise +-------- `malloc` を使用してポイント構造体を動的に割り当てます。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include #include @@ -63,12 +63,12 @@ free は `myperson` 変数自体を削除するのではなく、その変数が return 0; } -期待している出力 +Expected Output --------------- mypoint coordinates: 10, 5 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/For loops.md b/tutorials/learn-c.org/ja/For loops.md index c1e0f8d62..ce3f28192 100644 --- a/tutorials/learn-c.org/ja/For loops.md +++ b/tutorials/learn-c.org/ja/For loops.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- C言語のforループはシンプルです。ループとは、複数回実行されるコードブロックのことです。 forループには、通常「i」と表記されるイテレータ変数が必要です。 @@ -38,13 +38,13 @@ forループは配列の値を、繰り返し処理できます。例えば、 /* 合計には a[0] + a[1] + ... + a[9] が含まれます */ printf("Sum of the array is %d\n", sum); -演習 ----- +Exercise +-------- 変数 `array` の階乗(`array[0]` から `array[9]` までのすべての項目の乗算)を計算します。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -58,12 +58,12 @@ forループは配列の値を、繰り返し処理できます。例えば、 printf("10! is %d.\n", factorial); } -期待している出力 +Expected Output --------------- 10! is 3628800. -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Function Pointers.md b/tutorials/learn-c.org/ja/Function Pointers.md index 5cbc40c1f..0ab025a2a 100644 --- a/tutorials/learn-c.org/ja/Function Pointers.md +++ b/tutorials/learn-c.org/ja/Function Pointers.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ポインタを覚えていますか?文字の配列を指し示し、それを文字列に変換するためにポインタを使いました。 その後、ポインタの制御方法を学んだことで、さらに面白くなりました。 @@ -74,12 +74,13 @@ 1. プログラマーが様々な用途でライブラリを利用できるようにするため -> 「柔軟性」 -演習 ----- +Exercise +-------- + 関数へのポインタの配列を完成させ、配列内のポインタを使って各関数を呼び出します。関数へのポインタの配列? はい、できますよ! -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -118,14 +119,14 @@ } -期待している出力 +Expected Output --------------- this is f1 and var is: 0 this is f2 and var is: 1 this is f3 and var is: 2 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Function arguments by reference.md b/tutorials/learn-c.org/ja/Function arguments by reference.md index 10915f260..0a5d15723 100644 --- a/tutorials/learn-c.org/ja/Function arguments by reference.md +++ b/tutorials/learn-c.org/ja/Function arguments by reference.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ポインタと関数について理解した上で、関数の引数は値渡し、つまり関数間でコピーされて渡されることをご存知でしょう。 しかし、値そのものではなく、値へのポインタを渡したらどうなるでしょうか? こうすることで、関数は親関数の変数や構造体をコピーではなく制御できるようになり、元のオブジェクトを直接読み書きできるようになります。 @@ -48,13 +48,13 @@ p->y++; } -演習 ----- +Exercise +-------- `person`の`age`に1を加算する`birthday`という関数を書いてください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -81,13 +81,13 @@ return 0; } -期待している出力 +Expected Output --------------- John is 27 years old. Happy birthday! John is now 28 years old. -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Functions.md b/tutorials/learn-c.org/ja/Functions.md index 6cff6426c..294f9f38c 100644 --- a/tutorials/learn-c.org/ja/Functions.md +++ b/tutorials/learn-c.org/ja/Functions.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- C関数はシンプルですが、C言語の仕組み上、その威力には多少の限界があります。 @@ -52,15 +52,15 @@ C言語では、関数はコード内で使用される前に定義される必 moo(); } -演習 ----- +Exercise +-------- `print_big` という関数を作成してください。この関数は、引数(整数)を1つ受け取り、引数が10より大きい数値の場合に `x is big`(x は引数)という行を出力します。 * **重要**: printf 文字列の末尾に改行文字 `\n` を追加することを忘れないでください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -78,14 +78,14 @@ C言語では、関数はコード内で使用される前に定義される必 /* あなたの関数をここに書く */ -期待している出力 +Expected Output --------------- 11 is big 22 is big 33 is big -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Hello, World!.md b/tutorials/learn-c.org/ja/Hello, World!.md index 43c3cf327..ca7fcf949 100644 --- a/tutorials/learn-c.org/ja/Hello, World!.md +++ b/tutorials/learn-c.org/ja/Hello, World!.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### イントロダクション @@ -35,12 +35,12 @@ C言語のすべての文はセミコロンで終わる必要があることに 最後になりましたが、重要なことです。文を出力するために関数「printf」を呼び出さなくてはなりません。 -演習 +Exercise -------- 下部のプログラムを変更して、出力に「Hello, World!」と表示されるようにします。 -チュートリアル コード +Tutorial Code ------------- #include @@ -50,12 +50,12 @@ C言語のすべての文はセミコロンで終わる必要があることに return 0; } -期待している出力 +Expected Output --------------- Hello, World! -解答 +Solution -------- #include diff --git a/tutorials/learn-c.org/ja/Linked lists.md b/tutorials/learn-c.org/ja/Linked lists.md index 1f72496e3..3b9e7aec4 100644 --- a/tutorials/learn-c.org/ja/Linked lists.md +++ b/tutorials/learn-c.org/ja/Linked lists.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### イントロダクション @@ -220,13 +220,13 @@ malloc が NULL 値を返したかどうかを常に確認する必要がある } -演習 ----- +Exercise +-------- 先頭へのダブルポインタを受け取り、リスト内の値 `val` を持つ最初の項目を削除する関数 `remove_by_value` を実装する必要があります。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include #include @@ -282,14 +282,14 @@ malloc が NULL 値を返したかどうかを常に確認する必要がある print_list(test_list); } -期待している出力 +Expected Output --------------- 1 2 4 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Multidimensional Arrays.md b/tutorials/learn-c.org/ja/Multidimensional Arrays.md index d2139f425..8894054b1 100644 --- a/tutorials/learn-c.org/ja/Multidimensional Arrays.md +++ b/tutorials/learn-c.org/ja/Multidimensional Arrays.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- 前回の[配列](https://www.learn-c.org/en/Arrays)のチュートリアルでは、配列とその仕組みについて説明しました。これまで見てきた配列はすべて1次元でしたが、C言語では多次元配列を作成・使用できます。多次元配列の宣言の一般的な形式は次のとおりです。 @@ -62,8 +62,8 @@ or maybe this one - というステートメントは、配列の 3 行目で 4 列目の要素を取得します。 -演習 ----- +Exercise +-------- 数学と物理の2つの科目について、5人の生徒グループの平均点を調べてみましょう。そのためには、「grades」という2次元配列を使用します。数学の点数は1行目(「grades[0]」)に格納され、物理の点数は2行目(「grades[1]」)に格納されます。このプログラムを実行するには、以下の手順を実行してください。 @@ -71,8 +71,8 @@ or maybe this one - - 終了条件を指定してforループを完了する - 各科目の平均点を計算する -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -109,13 +109,13 @@ or maybe this one - } -期待している出力 +Expected Output --------------- The average marks obtained in subject 0 is: 78.80 The average marks obtained in subject 1 is: 82.80 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Pointer Arithmetics.md b/tutorials/learn-c.org/ja/Pointer Arithmetics.md index 10f64f648..bad6979a0 100644 --- a/tutorials/learn-c.org/ja/Pointer Arithmetics.md +++ b/tutorials/learn-c.org/ja/Pointer Arithmetics.md @@ -1,5 +1,6 @@ -チュートリアル -------------- +Tutorial +-------- + 以前、ポインタとは何か、そしてポインタの操作方法を学びました。このチュートリアルでは、ポインタに対する算術演算について学習します。 C言語のポインタには、++、--、-、+ といった複数の算術演算を適用できます。 @@ -111,12 +112,14 @@ C言語のポインタには、++、--、-、+ といった複数の算術演算 他にも比較演算(>、<、==など)があります。考え方は変数の比較と非常に似ていますが、この場合はメモリアドレスを比較します。 -演習 ----- +Exercise +-------- + intarray の最後の 3 つのアドレスを、int へのポインターの配列である parray にコピーします。 -チュートリアル コード -------------------- +Tutorial Code +------------- + #include int main() { @@ -147,13 +150,13 @@ intarray の最後の 3 つのアドレスを、int へのポインターの配 } -期待している出力 +Expected Output --------------- Matched! Matched! Matched! -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Pointers.md b/tutorials/learn-c.org/ja/Pointers.md index e1c0f0fcb..9ef2d27a2 100644 --- a/tutorials/learn-c.org/ja/Pointers.md +++ b/tutorials/learn-c.org/ja/Pointers.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ポインタも変数の一種であり、Cプログラミング言語において非常に重要な役割を果たします。ポインタは、以下のような様々な目的で使用されます。 @@ -67,13 +67,13 @@ /* 3 と表示されます */ printf("The value of a is now %d\n", a); -演習 ----- +Exercise +-------- `pointer_to_n` というローカル変数 `n` へのポインタを作成し、それを使用して `n` の値を 1 増やします。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -90,12 +90,12 @@ return 0; } -期待している出力 +Expected Output --------------- Done! -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Recursion.md b/tutorials/learn-c.org/ja/Recursion.md index f76048da1..d2c413703 100644 --- a/tutorials/learn-c.org/ja/Recursion.md +++ b/tutorials/learn-c.org/ja/Recursion.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- 再帰は、関数内に自分自身の呼び出しが含まれている場合に発生します。再帰により、非常に簡潔で洗練された、直感的に理解しやすいコードを作成できます。ただし、再帰が深くなりすぎると、大量のメモリが消費される可能性があります。 @@ -36,13 +36,13 @@ return 0; } -演習 ----- +Exercise +-------- 再帰乗算によって階乗を計算する `factorial()` という新しい関数を定義します (5! = 5 x 4 x 3 x 2 x 1)。慣例により、0の階乗は1に等しい (0! = 1) ことに注意してください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -56,7 +56,7 @@ /* ここで関数を定義します(宣言することを忘れないでください) */ -期待している出力 +Expected Output --------------- 0! = 1 @@ -64,7 +64,7 @@ 3! = 6 5! = 120 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Static.md b/tutorials/learn-c.org/ja/Static.md index 62f99267f..c36552dca 100644 --- a/tutorials/learn-c.org/ja/Static.md +++ b/tutorials/learn-c.org/ja/Static.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- `static` はCプログラミング言語のキーワードです。変数や関数で使用できます。 @@ -56,13 +56,13 @@ C言語では、関数はデフォルトでグローバルです。しかし、` static変数は、その変数を含むファイル内でのみアクセス可能なため、特定のファイル内でのみアクセス可能です。一方、グローバル変数はファイル外からもアクセスできます。 -演習 ----- +Exercise +-------- この演習では、staticキーワードを使っていくつかの数値の合計を計算してみましょう。`sum()`関数には、累計を表す変数を渡さないでください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include int sum (int num) { @@ -78,12 +78,12 @@ static変数は、その変数を含むファイル内でのみアクセス可 return 0; } -期待している出力 +Expected Output --------------- 55 100 150 -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Strings.md b/tutorials/learn-c.org/ja/Strings.md index 25535ac99..b59436d7e 100644 --- a/tutorials/learn-c.org/ja/Strings.md +++ b/tutorials/learn-c.org/ja/Strings.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### 文字列の定義 @@ -66,13 +66,13 @@ C言語における文字列は、実際には文字の配列です。C言語に strncat(dest,src,20); printf("%s\n",dest); -演習 ----- +Exercise +-------- ポインタ表記法を使用して、文字列 `first_name` を値 `John` で定義し、ローカル配列表記法を使用して、文字列 `last_name` を値 `Doe` で定義します。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include #include @@ -94,13 +94,13 @@ C言語における文字列は、実際には文字の配列です。C言語に } -期待している出力 +Expected Output --------------- Done! JohnBoe -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Structures.md b/tutorials/learn-c.org/ja/Structures.md index 595811fcb..38a7b9ee0 100644 --- a/tutorials/learn-c.org/ja/Structures.md +++ b/tutorials/learn-c.org/ja/Structures.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- C構造体は、内部に複数の名前付き変数を含む特別な大きな変数です。構造体は、構造体とクラスの基本的な基礎です。構造体は以下の目的で使用されます。 @@ -57,13 +57,13 @@ typedefを使用することで、型の別名を定義できます。これは mycar.brand = "Ford"; mycar.model = 2007; -演習 ----- +Exercise +-------- 新しい構造体を定義してください。構造体の名前は"person"で、`name`という名前の文字列(charへのポインタ)と、`age`という整数を含みます。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -78,12 +78,12 @@ typedefを使用することで、型の別名を定義できます。これは printf("%s is %d years old.", john.name, john.age); } -期待している出力 +Expected Output --------------- John is 27 years old. -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Unions.md b/tutorials/learn-c.org/ja/Unions.md index ab44fba17..fa42a109b 100644 --- a/tutorials/learn-c.org/ja/Unions.md +++ b/tutorials/learn-c.org/ja/Unions.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- Cの共用体はCの構造体と基本的に同じですが、複数の変数(それぞれが別々のメモリ領域に割り当てられる)を格納する代わりに、共用体では同じ一つの変数に、複数の名前を付けることができます。それにより、その変数に割り当てられたメモリ領域を、名前ごとに異なる型として扱うことができます(共用体のサイズは、最大の型のサイズに、コンパイラが決定するパディングを加えたものになります)。 @@ -100,13 +100,13 @@ union によって変数が同じメモリを共有するため、coins 配列 change.quarter, change.dime, change.nickel, change.penny); -演習 ----- +Exercise +-------- 21 個の文字と 6 個の整数 (21 / 4 == 5 なので 6 個ですが、5 * 4 == 20 なので、この演習ではあと 1 個必要です) の配列を格納する共用体を作成し、整数を 6 つの指定された値に設定してから、文字配列を一連の文字と文字列の両方として出力します。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -128,13 +128,13 @@ union によって変数が同じメモリを共有するため、coins 配列 printf("%s\n", intCharacters.chars); } -期待している出力 +Expected Output --------------- [I, , u, n, d, e, r, s, t, a, n, d, , U, n, i, o, n, s, !] I understand Unions! -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/Variables and Types.md b/tutorials/learn-c.org/ja/Variables and Types.md index 3374494a8..4c587c661 100644 --- a/tutorials/learn-c.org/ja/Variables and Types.md +++ b/tutorials/learn-c.org/ja/Variables and Types.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- ### データ型 @@ -38,13 +38,13 @@ C では文字の配列を使用して文字列を定義します。これにつ a = b - c + d * e; printf("%d", a); /* 1-2+3*4 = 11 が出力されます */ -演習 ----- +Exercise +-------- 次の演習では、数値 `a`、`b`、`c` の合計を出力するプログラムを作成する必要があります。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -60,11 +60,11 @@ C では文字の配列を使用して文字列を定義します。これにつ return 0; } -期待している出力 +Expected Output --------------- The sum of a, b, and c is 12.750000. -解答 +Solution ---- #include diff --git a/tutorials/learn-c.org/ja/While loops.md b/tutorials/learn-c.org/ja/While loops.md index 3f538b465..59577a746 100644 --- a/tutorials/learn-c.org/ja/While loops.md +++ b/tutorials/learn-c.org/ja/While loops.md @@ -1,5 +1,5 @@ -チュートリアル -------------- +Tutorial +-------- whileループはforループに似ていますが、機能は少なくなっています。whileループは、while内の条件が真である限り、whileブロックの実行を継続します。例えば、次のコードはちょうど10回実行されます。 @@ -44,8 +44,8 @@ C言語には、あらゆるループタイプで併用される重要なルー printf("The number %d is even.\n", n); } -演習 ----- +Exercise +-------- `array` 変数は 10 個の数値のシーケンスで構成されています。while ループ内に 2 つの `if` 条件を記述する必要があります。 これらの条件は、ループの流れを以下のように変更します(`printf` コマンドは変更しません)。 @@ -55,8 +55,8 @@ C言語には、あらゆるループタイプで併用される重要なルー 反復変数 `i` を進めずに `continue` 導関数を使用しないと、無限ループに陥ることに注意してください。 -チュートリアル コード -------------------- +Tutorial Code +------------- #include @@ -74,7 +74,7 @@ C言語には、あらゆるループタイプで併用される重要なルー return 0; } -期待している出力 +Expected Output --------------- 7 @@ -82,7 +82,7 @@ C言語には、あらゆるループタイプで併用される重要なルー 9 5 -解答 +Solution ---- #include