Question about C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Solar wrote:
Brynet-Inc wrote:And then you could turn on the -pedantic (or -pedantic-errors) flag...
Note that "-pedantic" does not refer to "pedantic error-checking" but rather "pedantic adherence to what the standard says". At some points the standard requires a diagnostic where "GNU style" doesn't, and vice versa.
Pedantic stuff is warning about unused variables, unused parameters with a name (yes), in some standard forms not declaring a function with an empty parameter list as (void) but as (), stuff like that. The stuff just about nobody commonly thinks should be considered wrong, but really is.

Solar, I see a long list of possible warnings but no -Wextra. Any reason for that?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Candy wrote:Solar, I see a long list of possible warnings but no -Wextra. Any reason for that?
Uh... I'm sure I had one, but can't really remember at the moment... :oops:
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

I checked... indeed I overlooked -Wextra. Adding it led to several "unused parameter" warnings where I had added a function stub but didn't implement it yet (so I disabled that particular warning class for now), and three "comparing signed to unsigned" warnings that were easily fixed - revealing, however, a genuine bug in my printf() code.

Point in case. Warnings are a good thing.
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

I am thinking that -Wextra includes the list of warnings you explicitly turn on. My common warning list is:

Code: Select all

-Wall -Wextra -Werror -pedantic -pedantic-errors -std=gnu++98
If I could've set it to gnu++03 or 0x I would. This cleans up your code to not include stuff that could be pretty vague and to mostly keep a clean style. It forbids a number of "wrong" constructs.

I am not claiming that this produces good code. I am claiming that this changes a proper and consistent style into proper, consistent and readable code. You could make an IOCCC submission with these items, but if applied to properly formatted coding fishes out 99% of all mistakes.

If you learn to avoid the complex mistakes you can let the compiler complain about the rest. I can proudly claim that 90% of my mistakes are caught by the compiler, 8% by me during coding (but eh... that's impossible) and 2% during testing (where something doesn't crash, flaw, but just does the wrong thing). Code that does something can be changed to do another thing. Code that doesn't stably do something can't.


Just for fun, try the above flags with "make -k" and count the errors. All of these errors could and can be problems in your code.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Candy wrote:I am thinking that -Wextra includes the list of warnings you explicitly turn on.
Nope.

Check 'info gcc', Warning options.
Every good solution is obvious once you've found it.
Post Reply