Initializing variables

Takeaway:

  • 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].