Thread: Use highest warning level recommended?

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    100

    Use highest warning level recommended?

    Hi
    Increase warning level on compiler so warnings is good or not?
    Or in which cases is it good to use?
    you tell me you can C,why dont you C your own bugs?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    Generally you should use the highest warning level your compiler supplies. The more warnings that are caught means the fewer potential problems you will have later.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,425
    I agree with jimblumberg if your code is intended to go out into the wild (such as a gitlab hosted project) but if it's only on your computer then -Wall or /Wall (depends on compiler) is enough to cover your basis, I don't remember any more warning options for MSC but for GCC and ones based on it I believe there was the -Wextra option which is helpful for ironing out potential problems that are normally ignorable on the average set up, things like unused symbols for example.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    294
    In the past I have been using "-Wall -pedantic", but now also recently added "-Wextra", which for me picks up the mixing of mostly signed vs unsigned issues.

    It has shown me a lot of shabby/fuzzy thinking I had, usually around just using "int" as the loop variable for "for" loops, where "size_t" might be a better choice.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    With gcc/g++ I would also suggest that you specifically state the version of the standard you want to use, ie "-std=c++20" because by default gcc/g++ uses "-std=gnu++XX". I recommend sticking with one of the standards instead of the gnu hacks unless you actually need them.

    One other warning I normally use is "-Wvla", especially in C++ since IMO vla are a horrible addition to the C standard and are disallowed in C++.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by awsdert View Post
    I agree with jimblumberg if your code is intended to go out into the wild (such as a gitlab hosted project) but if it's only on your computer then -Wall or /Wall (depends on compiler) is enough to cover your basis,.
    I strongly disagree. Warnings are there for a reason! If you ignore a warning now, when your code is small, it might come back to bite you later as code is added, or worse, causes an intermittent error difficult to track down.

    I strongly agree with jimblumberg, and recommend setting your warning level as high as your compiler allows, always, and don't ignore the warnings shown. This recommendation is especially true for any beginner C programmers reading this thread, and others.

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,425
    Quote Originally Posted by rstanley View Post
    I strongly disagree. Warnings are there for a reason! If you ignore a warning now, when your code is small, it might come back to bite you later as code is added, or worse, causes an intermittent error difficult to track down.

    I strongly agree with jimblumberg, and recommend setting your warning level as high as your compiler allows, always, and don't ignore the warnings shown. This recommendation is especially true for any beginner C programmers reading this thread, and others.
    For beginner C programmers that's fine but for experienced C programmers who are only trying out something Wall is enough, any project that's only meant for one's own computer is usually gonna stay small, and more importantly they're usually gonna be a bodge it job, where it's NOT expected to grow and/or possibly go into the wild, it also serves as good practice for theory tests where you don't have the luxury of a compilers, as a side bonus it helps one become more comfortable calling themself a minor expert (I say minor because despite my experience I don't feel comfortable calling myself a major expert until I start doing kernel level programming)

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,192
    Wall is enough [for a beginner]
    That's idiotic. You should crank it up at least as high as -Wall -Wextra -Wpedantic. In fact you can add more, such as -Wshadow to detect shadowing a variable, a common beginner mistake (although pros might find it more annoying than useful).
    We live as it were by chance, and by chance we are governed. - Seneca

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    From time to time I like to use "-Wmissing-include-dirs" to verify my program is not including folders that do not exist.
    Edit: This is a problem that is normally IDE or makefile caused.

    Tim S.
    Last edited by stahta01; 07-29-2021 at 12:30 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    For beginner C programmers that's fine but for experienced C programmers who are only trying out something Wall is enough,
    I disagree, IMO, experienced programmers prefer to have the compiler help "debug" the program. I usually use quite a few warnings when writing code, even though I will rarely have code that generates some of those warnings (ie: -Wmain). By the way, IMO, warnings are errors in disguise, so never ignore the warnings, even some of the easily understood/explained warnings (-Wmissing-declarations).

    say minor because despite my experience I don't feel comfortable calling myself a major expert
    That's good because looking at some of your code I would say that you're an intermediate programmer at best.

    I don't feel comfortable calling myself a major expert until I start doing kernel level programming
    I hope you realize that there are quite a few experts around that have never done much if any kernel level programming, and just because someone is playing around with kernel source doesn't necessarily make them an expert at anything.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by awsdert View Post
    For beginner C programmers that's fine but for experienced C programmers who are only trying out something Wall is enough, any project that's only meant for one's own computer is usually gonna stay small, and more importantly they're usually gonna be a bodge it job, where it's NOT expected to grow and/or possibly go into the wild, it also serves as good practice for theory tests where you don't have the luxury of a compilers.
    I downloaded your "deflate.zip" you publicly posted.

    gcc version: 10.2.1 on Debian Linux Testing
    Code:
    gcc -Wall main.c
    No warnings or errors displayed.

    Code:
    gcc -std=c18 -Wall -Wextra -Wpedantic
    
    main.c: In function ‘PrintStreamDetails’:
    main.c:222:15: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      222 |   "%s:%u %s( %p ): ptr = %p, pos = %u, num = %u, max = %u, fed = %u, got = ",
          |              ~^
          |               |
          |               void *
    ......
      226 |   stream,
          |   ~~~~~~       
          |   |
          |   STREAM * {aka struct _STREAM *}
    main.c: In function ‘PrintSymbol’:
    main.c:295:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare]
      295 |  uint sym = isprint(symbol->sym) ? symbol->sym : -1;
          |                                                  ^~
    main.c:296:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare]
      296 |  uint lit = isprint(symbol->lit) ? symbol->lit : -1;
          |                                                  ^~
    main.c:300:26: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘HUFFMAN_SYMBOL *’ {aka ‘struct _HUFFMAN_SYMBOL *’} [-Wformat=]
      300 |   "%s:%u: symbols[%3u] (%p): "
          |                         ~^
          |                          |
          |                          void *
    ......
      309 |   symbol,
          |   ~~~~~~                  
          |   |
          |   HUFFMAN_SYMBOL * {aka struct _HUFFMAN_SYMBOL *}
    main.c: In function ‘IdentifyNextSymbol’:
    main.c:386:16: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      386 |   "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n"
          |               ~^
          |                |
          |                void *
    ......
      390 |   , stream
          |     ~~~~~~      
          |     |
          |     STREAM * {aka struct _STREAM *}
    main.c:386:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      386 |   "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n"
          |                   ~^
          |                    |
          |                    void *
    ......
      391 |   , Symbols
          |     ~~~~~~~         
          |     |
          |     HUFFMAN_SYMBOLS * {aka struct _HUFFMAN_SYMBOLS *}
    main.c: In function ‘main’:
    main.c:533:15: warning: unused parameter ‘argc’ [-Wunused-parameter]
      533 | int main( int argc, char *argv[] )
          |           ~~~~^~~~
    main.c: In function ‘RdHuffmanLengDistCodes’:
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:33: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                ~^
          |                                 |
          |                                 void *
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:37: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                    ~^
          |                                     |
          |                                     void *
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 7 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:41: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                        ~^
          |                                         |
          |                                         void *
    main.c:864:14: warning: unused parameter ‘name’ [-Wunused-parameter]
      864 |  char const *name
          |  ~~~~~~~~~~~~^~~~
    Need I say more?

    As a side bonus it helps one become more comfortable calling themself a minor expert (I say minor because despite my experience I don't feel comfortable calling myself a major expert until I start doing kernel level programming)
    No comment.

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,425
    Quote Originally Posted by rstanley View Post
    I downloaded your "deflate.zip" you publicly posted.

    gcc version: 10.2.1 on Debian Linux Testing
    Code:
    gcc -Wall main.c
    No warnings or errors displayed.

    Code:
    gcc -std=c18 -Wall -Wextra -Wpedantic
    
    main.c: In function ‘PrintStreamDetails’:
    main.c:222:15: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      222 |   "%s:%u %s( %p ): ptr = %p, pos = %u, num = %u, max = %u, fed = %u, got = ",
          |              ~^
          |               |
          |               void *
    ......
      226 |   stream,
          |   ~~~~~~       
          |   |
          |   STREAM * {aka struct _STREAM *}
    main.c: In function ‘PrintSymbol’:
    main.c:295:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare]
      295 |  uint sym = isprint(symbol->sym) ? symbol->sym : -1;
          |                                                  ^~
    main.c:296:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare]
      296 |  uint lit = isprint(symbol->lit) ? symbol->lit : -1;
          |                                                  ^~
    main.c:300:26: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘HUFFMAN_SYMBOL *’ {aka ‘struct _HUFFMAN_SYMBOL *’} [-Wformat=]
      300 |   "%s:%u: symbols[%3u] (%p): "
          |                         ~^
          |                          |
          |                          void *
    ......
      309 |   symbol,
          |   ~~~~~~                  
          |   |
          |   HUFFMAN_SYMBOL * {aka struct _HUFFMAN_SYMBOL *}
    main.c: In function ‘IdentifyNextSymbol’:
    main.c:386:16: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      386 |   "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n"
          |               ~^
          |                |
          |                void *
    ......
      390 |   , stream
          |     ~~~~~~      
          |     |
          |     STREAM * {aka struct _STREAM *}
    main.c:386:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      386 |   "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n"
          |                   ~^
          |                    |
          |                    void *
    ......
      391 |   , Symbols
          |     ~~~~~~~         
          |     |
          |     HUFFMAN_SYMBOLS * {aka struct _HUFFMAN_SYMBOLS *}
    main.c: In function ‘main’:
    main.c:533:15: warning: unused parameter ‘argc’ [-Wunused-parameter]
      533 | int main( int argc, char *argv[] )
          |           ~~~~^~~~
    main.c: In function ‘RdHuffmanLengDistCodes’:
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:33: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                ~^
          |                                 |
          |                                 void *
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:37: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                    ~^
          |                                     |
          |                                     void *
    main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 7 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=]
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’
      877 |  printf( PFX_FORMAT "\n", PFX_VALUES );
          |          ^~~~~~~~~~
    main.c:867:41: note: format string is defined here
      867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )"
          |                                        ~^
          |                                         |
          |                                         void *
    main.c:864:14: warning: unused parameter ‘name’ [-Wunused-parameter]
      864 |  char const *name
          |  ~~~~~~~~~~~~^~~~
    Need I say more?


    No comment.
    Yes, because every single one of those are ignorable,most just want me to cast to void* or uint, neither effect the outcome in these cases, and a couple are just unused variable I though t I had used, so in this case Wall was indeed enough

  13. #13
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    793
    Quote Originally Posted by awsdert View Post
    Yes, because every single one of those are ignorable,most just want me to cast to void* or uint, neither effect the outcome in these cases, and a couple are just unused variable I though t I had used, so in this case Wall was indeed enough
    The warnings about casting to void* in printf statements are not ignorable. Passing in the wrong pointer type (i.e., not a void pointer) has undefined behavior. Some architectures even have different sizes of pointers depending on the pointed-to type. The safest thing to do is cast the pointers to void*.

  14. #14
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    100
    Thanks for answers all
    Different pointer size must be more common,when you can compile c to 4 different cpu's
    Using ebx gives me warning, so i preserve that
    Seem easy to cause exception,writing near local variables
    you tell me you can C,why dont you C your own bugs?

  15. #15
    Registered User
    Join Date
    Sep 2020
    Posts
    294
    Back in the old days (of 16-bit IBM PCs) you had "near pointers" "far pointers" and "huge pointers".

    'near' pointers were 16 bits, and could address up to 64k, but only in one block of memory - usually in the data or code segments.

    'far' pointers were 32-bits, but could address 64k anywhere in 1MB of memory

    'huge' pointers were 32-bits, very inefficient, but could address anywhere in 1MB of memory.

    C compilers would compile almost anything without warnings, as they had to run on CPUs that might be 4.77MHz, and fit into only a few hundred kb of RAM.

    Programmers learnt fast or died young back then. Ah, the good old days...

    Explain Near Far Huge pointers in C language

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switching between user-level and kernel-level threads
    By Javed Iqbal in forum Tech Board
    Replies: 9
    Last Post: 12-16-2014, 09:16 AM
  2. Warning Level
    By C_ntua in forum C# Programming
    Replies: 6
    Last Post: 07-13-2010, 07:02 AM
  3. Recommended C#/.NET Book
    By kuphryn in forum C# Programming
    Replies: 4
    Last Post: 04-23-2004, 09:21 AM
  4. Recommended reading?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-19-2003, 06:34 PM
  5. any C Compilers recommended ??
    By imbecile in C in forum C Programming
    Replies: 10
    Last Post: 06-25-2003, 02:16 PM

Tags for this Thread