A note of caution on GCC’s uninitialized variables

A note of caution on GCC’s uninitialized variables

·

3 min read

Recently, I have started my journey to learn C programming using Visual Studio Code and Mingw-w64. What is Mingw-w64? It is created to support GCC compiler on Windows systems. GCC is the GNU Compiler Collection and GNU (stands for GNU’s not Unix) is key component to help build operating systems like Linux.

If you are interested to find out how to use Mingw-w64 in VS Code, please click this link to start.

Once everything is setup, you should have 3 files in .vscode sub-folder:

  • c_cpp_properties.json (compiler path and IntelliSense settings)
  • tasks.json (build instructions)
  • launch.json (debugger settings)

Here is the file program.c which I wrote to check the variables.

#include<stdio.h>

int main()
{
    int apples;
    int oranges;
    int bananas;

    printf("apples=%d oranges=%d bananas=%d", apples, oranges, bananas);

    return 0;
}

So, if we look at the codes, all those variables are not initialized. Meaning those variables have no value assigned to them. So, if those variables are not initialized, they should have default value which in this case, they all should have default value 0. So I would expect to have all those variables printed as 0.

If you have setup your environment properly, you should be able to run the command below:

gcc program.c -o program

Once executed, simply run program.exe.

But look at my output screenshot below:

Can you see what’s wrong here? My second placeholder is 16! I tried to google around but I couldn’t find the exact problem here. If you know why, please welcome to leave a comment here. The second problem is that it didn’t give any warning for uninitialized variables.

After doing some searches, I finally managed to get it right by using a switch -O

Now, my output is as below:

The switch -O is basically a code optimization with compiler attempt to improve performance and/or code size at the expense of compilation time and possibly the ability to debug the program. If you are interested to find out more, please go to GCC website’s Options That Control Optimization.

Now, in order to help to detect uninitialized variables as warnings, I will need add one more switch which is -Wall.

The switch -Wall helps to enable all warning flags including -Wuninitialized which warns if variable is used without first being initialized. More info about both switches can be found here and here.

The last thing we need to do is to modify tasks.json to include both switches.

{
     // See https://go.microsoft.com/fwlink/?LinkId=733558 
     // for the documentation about the tasks.json format
     "version": "2.0.0",
     "tasks": [
         {
             "type": "shell",
             "label": "gcc.exe build active file",
             "command": "C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gcc.exe",
             "args": [
                 "-g",
                 "-O",
                 "-Wall",
                 "${file}",
                 "-o",
                 "${fileDirname}\${fileBasenameNoExtension}.exe"
             ],
             "options": {
                 "cwd": "C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin"
             },
             "problemMatcher": [
                 "$gcc"
             ],
             "group": {
                 "kind": "build",
                 "isDefault": true
             }
         }
     ]
 }

So when we run build in VS Code, it will automatically optimise codes and display warnings.

Thanks for your time!

Did you find this article valuable?

Support Han Chee by becoming a sponsor. Any amount is appreciated!