{"id":10276,"date":"2025-07-16T13:00:06","date_gmt":"2025-07-16T11:00:06","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=10276"},"modified":"2025-08-12T14:20:43","modified_gmt":"2025-08-12T12:20:43","slug":"declaring-and-initializing-variables","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/","title":{"rendered":"Declaring and initializing variables"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Declaring_variables\" >Declaring variables<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Initializing_variables\" >Initializing variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Data_types\" >Data types<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Declaring_arrays\" >Declaring arrays<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Initializing_arrays\" >Initializing arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Zero-initializing_an_array\" >Zero-initializing an array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Declaring_and_initializing_multidimensional_arrays\" >Declaring and initializing multidimensional arrays<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Declaring_strings\" >Declaring strings<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Initializing_strings\" >Initializing strings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Initializing_an_empty_string\" >Initializing an empty string<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Declaring_an_array_of_strings\" >Declaring an array of strings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/#Initializing_an_array_of_strings\" >Initializing an array of strings<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Declaring_variables\"><\/span>Declaring variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In C, we always declare a variable by stating the data type and the name of the variable using syntax <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type variable_name;<\/span> .<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    \/\/ Declares an integer variable\r\n    int number;\r\n    \r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\", &amp;number);\r\n\r\n    printf(\"You entered %d as integer.\\n\", number);\r\n    return 0;\r\n}\r\n<\/pre>\n<p>When multiple variables of the same data type are declared, they are separated by commas using syntax <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type variable_name_1, variable_name_2;<\/span>. Any number of variables can be declared this way, but the greater the amount, the more unreadable the code gets!<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    \/\/ Declares two real numbers of the same data type\r\n    float firstRealNumber, secondRealNumber;\r\n    \r\n    printf(\"Enter a real number: \");\r\n    scanf(\"%f\", &amp;firstRealNumber);\r\n    \r\n    printf(\"Enter another real number: \");\r\n    scanf(\"%f\", &amp;secondRealNumber);\r\n    \r\n    printf(\"You entered %f and %f as real numbers.\\n\", firstRealNumber,\r\n                                                       secondRealNumber);\r\n    return 0;\r\n}\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Initializing_variables\"><\/span>Initializing variables<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Any variable can be initialized when declared by assigning it a value following <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type variable_name = initial_value;<\/span>\u00a0 syntax. As\u00a0 highlighted by the <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/coding-style\/\">coding style document<\/a><\/strong>, only one variable should be declared per line when initializing variables! This is important for code readability.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    \/\/ Declares and initializes an integer\r\n    int number = 129;\r\n    \r\n    \/\/ Declares and initializes a double\r\n    double realNumber = 25.91;\r\n    \r\n    printf(\"The numbers are: %d and %.2lf\\n\", number, \r\n                                              realNumber);\r\n    return 0;\r\n}\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Data_types\"><\/span>Data types<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>C leaves a lot of uncertainty to variable types. The C standard only specifies the <strong>minimum size<\/strong> and\u00a0 <b>relationship<\/b> between types. This is important to keep in mind, whenever a size is listed as a fixed number with confidence!<\/p>\n<p>The following table only lists the most basic types and formats as a basic reference. For a more complete overview, see <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/C_data_types\">https:\/\/en.wikipedia.org\/wiki\/C_data_types<\/a><\/strong> or<strong> <a href=\"https:\/\/www.tutorialspoint.com\/cprogramming\/c_data_types.htm\">https:\/\/www.tutorialspoint.com\/cprogramming\/c_data_types.htm<\/a><\/strong><\/p>\n<table style=\"width: 100%; border-collapse: collapse; border-style: solid; height: 126px;\">\n<caption>\u00a0<\/caption>\n<tbody>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\"><strong>Data type<\/strong><\/td>\n<td style=\"width: 16.6667%; height: 18px;\"><strong>Minimum size (bytes)<\/strong><\/td>\n<td style=\"width: 16.6667%; height: 18px;\"><strong>Use case<\/strong><\/td>\n<td style=\"width: 16.6667%; height: 18px;\"><strong>Format<\/strong><\/td>\n<td style=\"width: 16.6667%; height: 18px;\"><strong>Comments<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">char<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">1<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">character<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%c<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">ASCII encoding<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">char []<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">?<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">string<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%s<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">Zero-terminated array of ASCII characters<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">int<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">2<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">smaller integers<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%d %i<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">Size varies by platform<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">long<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">4<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">larger integers<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%ld %li<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">Size varies by platform. Recommend using bit-width integers instead<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">float<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">4<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">real numbers, floating point<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%f<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">low precision, avoid for monetary values etc.<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 16.6667%; height: 18px;\">double<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">8<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">real numbers, double precision floating point<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">%lf<\/td>\n<td style=\"width: 16.6667%; height: 18px;\">better precision, still unsuitable when precision is required<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Comment:<\/strong> the minimum size and relationship is very noticeable when programming microcontrollers. E.g. a typical desktop, laptop or a server will have <span class=\"lang:c highlight:0 decode:true crayon-inline \">int<\/span>\u00a0 as 4 bytes, but most commonly used Arduinos have <span class=\"lang:c highlight:0 decode:true crayon-inline \">int<\/span>\u00a0 as 2 bytes. Similarly <span class=\"lang:c highlight:0 decode:true crayon-inline \">long<\/span>\u00a0 data type has to be 2x bigger compared to <span class=\"lang:c highlight:0 decode:true crayon-inline \">int<\/span> , so on a desktop, it&#8217;s likely 8 bytes and on an Arduino it&#8217;s likely 4 bytes. This is also varies based on which processor the Arduino microcontroller uses.<\/p>\n<p>For bit-width integer data types, see <span class=\"lang:c highlight:0 decode:true crayon-inline \">inttypes.h<\/span>\u00a0 library. This is covered in Programming 1.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Declaring_arrays\"><\/span>Declaring arrays<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Declaring arrays follows the same syntax as variables, but adds the size of the array to the declaration, i.e. <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type array_name[array_size];<\/span>\u00a0 . Specifying size is mandatory for static arrays.<\/p>\n<p>To avoid magical numbers, either a macro (<span class=\"lang:c highlight:0 decode:true crayon-inline \">#define<\/span> ) or <span class=\"lang:c highlight:0 decode:true crayon-inline \">const<\/span>\u00a0 variable often is used.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true \">#include &lt;stdio.h&gt;\r\n\r\n#define NUM_CNT 3\r\n\r\nint main(void)\r\n{\r\n    int nums[NUM_CNT];\r\n    \r\n    for (int i = 0; i &lt; NUM_CNT; i++)\r\n    {\r\n        printf(\"Enter number %d: \", i + 1);\r\n        scanf(\"%d\", &amp;nums[i]);\r\n    }\r\n    \r\n    printf(\"Entered nums were: \");\r\n    for (int i = 0; i &lt; NUM_CNT; i++)\r\n    {\r\n        printf(\"%d \", nums[i]);\r\n    }\r\n    printf(\"\\n\");\r\n    return 0;\r\n}<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Initializing_arrays\"><\/span>Initializing arrays<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Arrays can be initialized during declaration, by assigning either all values or a subset of values, that are listed between the <span class=\"lang:c highlight:0 decode:true crayon-inline \">{ }<\/span>\u00a0 brackets, i.e. <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type array_name[array_size] = {val_1, val_2, &#8230;, val_n-1};<\/span> .<\/p>\n<pre class=\"toolbar:2 lang:c decode:true \">#include &lt;stdio.h&gt;\r\n\r\n#define NUM_CNT 3\r\n\r\nint main(void)\r\n{\r\n    int nums[NUM_CNT] = {9, 2, 5};\r\n    \r\n    printf(\"Array contains: \");\r\n    for (int i = 0; i &lt; NUM_CNT; i++)\r\n    {\r\n        printf(\"%d \", nums[i]);\r\n    }\r\n    printf(\"\\n\");\r\n    return 0;\r\n}\r\n<\/pre>\n<p>When an array is initialized, the <span class=\"lang:c highlight:0 decode:true crayon-inline \">array_size<\/span>\u00a0 can be omitted, allowing the compiler to calculate the array size based on the number of initialized values.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    \/\/ Array size omitted, length must be calculated afterwards\r\n    int nums[] = {9, 2, 5};\r\n    int arrayLen = sizeof(nums) \/ sizeof(int);\r\n    \r\n    printf(\"Array contains: \");\r\n    for (int i = 0; i &lt; arrayLen; i++)\r\n    {\r\n        printf(\"%d \", nums[i]);\r\n    }\r\n    printf(\"\\n\");\r\n    return 0;\r\n}<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Zero-initializing_an_array\"><\/span>Zero-initializing an array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Arrays can be zero-initialized by using syntax <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type array_name[array_size] = {0};<\/span>. Note that if you omit the array size while zero-initializing, the array size becomes 1!<\/p>\n<pre class=\"lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\n#define NUM_CNT 3\r\n\r\nint main(void)\r\n{\r\n    \/\/ Zero-initialized array\r\n    int nums[NUM_CNT] = {0};\r\n    \r\n    printf(\"Array contains: \");\r\n    for (int i = 0; i &lt; NUM_CNT; i++)\r\n    {\r\n        printf(\"%d \", nums[i]);\r\n    }\r\n    printf(\"\\n\");\r\n    return 0;\r\n}\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Declaring_and_initializing_multidimensional_arrays\"><\/span>Declaring and initializing multidimensional arrays<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>All array dimensions must be specified during declaration. To declare a 2-dimensional array, we would typically use <span class=\"lang:c highlight:0 decode:true crayon-inline\">data_type array_name[row_cnt][col_cnt];<\/span> notation. Use of rows and cols is illustrative (think of an excel spreadsheet), but also a very common in practice.<\/p>\n<p>When initializing a 2-D array, we use nested brackets to identify rows. Alignment of values is optional, but helpful for maintainability.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">int main(void)\r\n{\r\n    int nums[ROW_CNT][COL_CNT] = {{ 5   3,  14, 3,  6},\r\n                                  {14,  3,  -9, 2,  7},\r\n                                  { 9,  1,   9, 2, 51},\r\n                                  {91,  0,   0, 1, 16},\r\n                                  {15, 43, 656, 1, 1}};\r\n    \r\n    printf(\"Array contains:\\n\");\r\n    for (int i = 0; i &lt; ROW_CNT; i++)\r\n    {\r\n        for (int j = 0; j &lt; COL_CNT; j++)\r\n        {\r\n            printf(\"%3d \", nums[i][j]);\r\n        }\r\n        printf(\"\\n\");\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>Note, that you can omit the first dimension (i.e. row count) in any multidimensional array, however it would need to be calculated later on to iterate over the array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Declaring_strings\"><\/span>Declaring strings<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Strings are handled as zero-terminated arrays in C. Each array member is a character represented in ASCII code, taking up 1 byte. Strings always need to have one extra character in reserve for the zero terminator.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true \">#include &lt;stdio.h&gt;\r\n\r\n#define STR_LEN 8\r\n\r\nint main(void)\r\n{\r\n    char word[STR_LEN];\r\n    \r\n    printf(\"Enter a word (up to %d characters): \", STR_LEN - 1);\r\n    scanf(\"%7s\", word);\r\n\r\n    printf(\"You entered: %s\\n\", word);\r\n    return 0;\r\n}<\/pre>\n<p><strong>Note:\u00a0<\/strong> This way of reading strings is <strong>discouraged<\/strong> in C, but was left as a illustration to keep it simple and short. Proper ways to read strings will be introduced in the appropriate lesson.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Initializing_strings\"><\/span>Initializing strings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Strings are often initialized as string literals, omitting the array length &#8211; this allows the compiler to identify the size. Strings are always surrounded by double quotes, while single characters are between single quotes.<\/p>\n<p>In the following example, the string will contain 13 characters, however the array will be declared for 14 bytes to allow for the terminating NUL character. The phrase zero-terminated comes from the ASCII symbol NUL having integer value 0 (zero).<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    char hello[] = \"Hello, World!\";\r\n    printf(\"%s\\n\", hello);\r\n    return 0;\r\n}<\/pre>\n<p>Most of the time, an asterisk before the variable name is used instead of the empty square brackets. This has the exact same effect.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    char *hello = \"Hello, World!\";\r\n    printf(\"%s\\n\", hello);\r\n    return 0;\r\n}<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Initializing_an_empty_string\"><\/span>Initializing an empty string<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>To initialize an empty string, we need to make sure that the string has an appropriate length for the use case we are planning for it.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\n#define STR_LEN 256\r\n\r\nint main(void)\r\n{\r\n    char word[STR_LEN] = \"\";\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p><strong>DO NOT<\/strong> use <span class=\"lang:c decode:true crayon-inline\">char *string = &#8220;&#8221;;<\/span>\u00a0 or <span class=\"lang:c highlight:0 decode:true crayon-inline \">char string[] = &#8220;&#8221;;<\/span> . The compiler will calculate the size based on the initialization &#8211; these are empty strings containing only the NUL byte string terminator, resulting them being 1 byte long and thus not fitting any characters.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Declaring_an_array_of_strings\"><\/span>Declaring an array of strings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Arrays of strings are handled similarly to two-dimensional arrays, with a few exceptions depending on how the declaration was created.<\/p>\n<p>The declaration <span class=\"lang:c decode:true crayon-inline \">char strings[num_of_strings][string_length];<\/span> allows to create a fixed number of strings indicated by the first dimension (i.e. <span class=\"lang:c highlight:0 decode:true crayon-inline \">num_of_string<\/span>\u00a0), where each string can be up to <span class=\"lang:c highlight:0 decode:true crayon-inline \">string_length &#8211; 1<\/span>\u00a0 characters. All strings will take up the same amount of memory regardless of actual length.<\/p>\n<p>This type of declaration is uncommon and largely impractical. A more common example is listed under the next chapter.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Initializing_an_array_of_strings\"><\/span>Initializing an array of strings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>An array of strings is typically declared as an array of pointers to strings using the following syntax:<\/p>\n<p><span class=\"lang:c decode:true crayon-inline\">char *strings[] = {&#8220;string1&#8221;, &#8220;string2&#8221;, &#8230;, &#8220;string_n-1&#8221;};<\/span><\/p>\n<p>This has the benefit of having the compiler figure out the sizes for both the array and for each initialized string. This is also the structure for command line arguments.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    char *names[] = {\"Tiina\", \"Mari\", \"Toomas\", \"Karl\"};\r\n    int n = sizeof(names) \/ sizeof(char *);\r\n    \r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"%s\\n\", names[i]);\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Declaring variables In C, we always declare a variable by stating the data type and the name of the variable using syntax data_type variable_name; . #include &lt;stdio.h&gt; int main(void) { \/\/ Declares an integer variable int number; printf(&#8220;Enter an integer: &#8220;); scanf(&#8220;%d&#8221;, &amp;number); printf(&#8220;You entered %d as integer.\\n&#8221;, number); return 0; } When multiple variables &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/declaring-and-initializing-variables\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Declaring and initializing variables<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4802,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-10276","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=10276"}],"version-history":[{"count":14,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10276\/revisions"}],"predecessor-version":[{"id":10354,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10276\/revisions\/10354"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4802"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=10276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}