Skip to content

Instantly share code, notes, and snippets.

@osalbahr
Last active September 11, 2023 18:10

Revisions

  1. osalbahr revised this gist Sep 11, 2023. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions gcc-clang-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,20 @@ uninitialized.c:5:7: note: 'x' was declared here
    | ^
    ```

    ## Or.... [adding one more non-warning flag](https://stackoverflow.com/questions/30578625/why-is-there-not-any-warning-on-a-declaration-without-initialization-in-a-for-lo)???

    ```console
    $ CC='gcc-13' CFLAGS='-O' make uninitialized
    gcc-13 -O uninitialized.c -o uninitialized
    $ rm uninitialized
    $ CC='gcc-13' CFLAGS='-Wall -O' make uninitialized
    gcc-13 -Wall -O uninitialized.c -o uninitialized
    uninitialized.c: In function 'main':
    uninitialized.c:5:7: warning: 'x' is used uninitialized [-Wuninitialized]
    5 | int x;
    | ^
    ```

    # With `clang`

    ```console
  2. osalbahr revised this gist Sep 11, 2023. 1 changed file with 4 additions and 24 deletions.
    28 changes: 4 additions & 24 deletions gcc-clang-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ int main()
    x += 1;
    printf("%d\n", x);
    }
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra -Wpedantic' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra -Wpedantic uninitialized.c -o uninitialized
    $ # No warning
    ```

    @@ -29,8 +29,8 @@ int main()
    // x += 1;
    printf("%d\n", x);
    }
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall' make uninitialized
    gcc-13 -std=c99 -Wall uninitialized.c -o uninitialized
    uninitialized.c: In function 'main':
    uninitialized.c:8:3: warning: 'x' is used uninitialized [-Wuninitialized]
    8 | printf("%d\n", x);
    @@ -54,23 +54,3 @@ uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
    = 0
    1 warning generated.
    ```

    - - -

    Edit: Ok, `-Wpedantic` does it, nvm
    ```console
    $ CFLAGS='-std=c99 -Wall -Wextra -Wpedantic' make uninitialized
    cc -std=c99 -Wall -Wextra -Wpedantic uninitialized.c -o uninitialized
    uninitialized.c:3:9: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
    int main()
    ^
    void
    uninitialized.c:7:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    x += 1;
    ^
    uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
    int x;
    ^
    = 0
    2 warnings generated.
    ```
  3. osalbahr revised this gist Sep 11, 2023. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions gcc-clang-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,24 @@ uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
    ^
    = 0
    1 warning generated.
    ```

    - - -

    Edit: Ok, `-Wpedantic` does it, nvm
    ```console
    $ CFLAGS='-std=c99 -Wall -Wextra -Wpedantic' make uninitialized
    cc -std=c99 -Wall -Wextra -Wpedantic uninitialized.c -o uninitialized
    uninitialized.c:3:9: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
    int main()
    ^
    void
    uninitialized.c:7:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    x += 1;
    ^
    uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
    int x;
    ^
    = 0
    2 warnings generated.
    ```
  4. osalbahr renamed this gist Sep 11, 2023. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gcc-uninitialized.md → gcc-clang-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,8 @@ gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ # No warning
    ```

    ## Commenting-out the loop

    ```console
    $ cat uninitialized.c
    #include <stdio.h>
    @@ -41,7 +43,8 @@ uninitialized.c:5:7: note: 'x' was declared here
    # With `clang`

    ```console
    $ clang -Wall uninitialized.c
    CC='clang' CFLAGS='-std=c99 -Wall' make uninitialized
    clang -std=c99 -Wall uninitialized.c -o uninitialized
    uninitialized.c:7:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    x += 1;
    ^
  5. osalbahr revised this gist Sep 11, 2023. 2 changed files with 22 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions gcc-version.md → compiler-versions.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,10 @@ gcc-13 (Homebrew GCC 13.2.0) 13.2.0
    Copyright (C) 2023 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ clang --version
    Apple clang version 14.0.3 (clang-1403.0.22.14.1)
    Target: x86_64-apple-darwin22.6.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
    ```
    16 changes: 16 additions & 0 deletions gcc-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # With `gcc` (GNU)

    ```console
    $ cat uninitialized.c
    #include <stdio.h>
    @@ -34,4 +36,18 @@ uninitialized.c:8:3: warning: 'x' is used uninitialized [-Wuninitialized]
    uninitialized.c:5:7: note: 'x' was declared here
    5 | int x;
    | ^
    ```

    # With `clang`

    ```console
    $ clang -Wall uninitialized.c
    uninitialized.c:7:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    x += 1;
    ^
    uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
    int x;
    ^
    = 0
    1 warning generated.
    ```
  6. osalbahr revised this gist Sep 11, 2023. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions gcc-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,20 @@
    ```console
    $ cat uninitialized.c
    #include <stdio.h>

    int main()
    {
    int x;
    for (int i = 0; i < 5; i++)
    x += 1;
    printf("%d\n", x);
    }
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ # No warning
    ```

    ```console
    $ gcc-13 -std=c99 -Wall -Wextra
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ vim uninitialized.c
    $ cat uninitialized.c
    #include <stdio.h>

  7. osalbahr revised this gist Sep 11, 2023. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions uninitialized.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #include <stdio.h>

    int main()
    {
    int x;
    for (int i = 0; i < 5; i++)
    x += 1;
    printf("%d\n", x);
    }
  8. osalbahr created this gist Sep 11, 2023.
    27 changes: 27 additions & 0 deletions gcc-uninitialized.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@


    ```console
    $ gcc-13 -std=c99 -Wall -Wextra
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    $ vim uninitialized.c
    $ cat uninitialized.c
    #include <stdio.h>

    int main()
    {
    int x;
    // for (int i = 0; i < 5; i++)
    // x += 1;
    printf("%d\n", x);
    }
    $ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
    gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
    uninitialized.c: In function 'main':
    uninitialized.c:8:3: warning: 'x' is used uninitialized [-Wuninitialized]
    8 | printf("%d\n", x);
    | ^~~~~~~~~~~~~~~~~
    uninitialized.c:5:7: note: 'x' was declared here
    5 | int x;
    | ^
    ```
    7 changes: 7 additions & 0 deletions gcc-version.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    ```console
    $ gcc-13 --version
    gcc-13 (Homebrew GCC 13.2.0) 13.2.0
    Copyright (C) 2023 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    ```