1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/** * File: init.c * Author: Risto Heinsar * Created: 21.08.2014 * Modified: 07.09.2018 * * Description: Examples of how to initialize variables * and arrays in C code. */ #include <stdio.h> #define LEN_SMALL_ARR 3 #define LEN_BIG_ARR 5 #define LEN_PART_ARR 10 int main(void) { int i, j; int multipleVar1, multipleVar2, multipleVar3; int num = 5; int numA[] = {7, 3, 6, -4, 2}; int numB[LEN_PART_ARR] = {6, 1, 2, 4, -2}; int numC[LEN_BIG_ARR] = { 0 }; int numD[][LEN_SMALL_ARR] = {{6, 3, 1}, {9, -1, 7}}; int numE[LEN_BIG_ARR][LEN_BIG_ARR] = {{5, 3, 14, 3, 6}, {14, 3, -9, 2, 7}, {9, 1, 9, 2, 51}, {91, 0, 0, 1, 16}, {15, 43, 656, 1, 1}}; char greet[] = {"Hello, how are You?"}; char multipleWords[][7] = {"Hi,", "how", "are?", "You?"}; multipleVar1 = 15; multipleVar2 = 55; multipleVar3 = -43; puts("Printing single digit:"); printf("%d\n\n", num); puts("Printing multiple digits from multiple variables:"); printf("%d %d %d\n\n", multipleVar1, multipleVar2, multipleVar3); puts("Printing an array:"); printf("%2d %2d %2d %2d %2d\n\n", numA[0], numA[1], numA[2], numA[3], numA[4]); puts("Printing second array using a loop:"); for (i = 0; i < LEN_PART_ARR; i++) { printf("%2d ", numB[i]); } printf("\n\n"); puts("Printing the zero initialized array:"); for (i = 0; i < LEN_BIG_ARR; i++) { printf("%2d ", numC[i]); } printf("\n\n"); puts("Printing a matrix:"); printf("%2d %2d %2d\n%2d %2d %2d\n\n", numD[0][0], numD[0][1], numD[0][2], numD[1][0], numD[1][1], numD[1][2]); puts("Printing a word:"); printf("%s\n\n", greet); puts("Printing multiple words:"); printf("%s %s %s %s\n\n", multipleWords[0], multipleWords[1], multipleWords[2], multipleWords[3]); puts("Printing the large array:"); for (i = 0; i < LEN_BIG_ARR; i++) { for (j = 0; j < LEN_BIG_ARR; j++) { printf("%4d", numE[i][j]); } putchar('\n'); } return 0; } |
- Initializing a variable should only be done on the same line of declaration when declaring a single variable. When declaring multiple variables on the same line, initializing them should be done on separate lines.
- numA – When initializing a vector (1-dim array), you don’t need to specify the size of it. In this case, the vector’s length will be determined by the number of members initialized. In this case, it would automatically make an array of 5 members.
- numB – if array declaration size exceeds the number of members initialized, the first members will be initialized. The array will still be able to hold as many elements as specified by the declaration and the slots can be used later in the code. E.g. we initialize 5 members, but have room for 10.
- numC – in this example, we are initializing all of the members of the array to 0.
- numD – When declaring multidimensional arrays, you can leave the first dimension unspecified. This will be set by the initialization values (just as with numA). In the example, this would become a 2×3 matrix. For clarity, we do recommend fixing all the dimension lengths.
- numE – when initializing bigger matrices, it’s recommended to split them between lines for readability. Otherwise it’s really easy to lose a bracket or a comma somewhere. Otherwise it’s just as the previous, however with both dimensions specified.
- greet – strings are handled as arrays of characters in C code. In this case we don’t specify the size of the array, so it will be figured out by the compiler. However when specified, we always need to have room for 1 extra symbol – the string terminator (\0)
- multipleWorlds – Figuratively this holds every word on separate line. Each word has room for 6 characters + the terminator. To access a single character, we have to specify both of the indexes – e.g. multipleWords[0][1] for the letter i in “Hi,” To access a word as a whole, we can just specify it’s index – e.g. multipleWords[0].