What does casting to `void` really do? [duplicate]
Casting to void is used to suppress compiler warnings. The Standard says in §5.2.9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded.
Casting to void is used to suppress compiler warnings. The Standard says in §5.2.9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded.
This is possible in GCC since version 4.6, or around June 2010 in the trunk. Here’s an example: #pragma GCC diagnostic push #pragma GCC diagnostic error “-Wuninitialized” foo(a); /* error is given for this one */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored “-Wuninitialized” foo(b); /* no diagnostic for this one */ #pragma GCC … Read more
This is worth a try. You can’t extend Obsolete, because it’s final, but maybe you can create your own attribute, and mark that class as obsolete like this: [Obsolete(“Should be refactored”)] public class MustRefactor: System.Attribute{} Then when you mark your methods with the “MustRefactor” attribute, the compile warnings will show. It generates a compile time … Read more
According to the standard (§6.4.4.4/10) The value of an integer character constant containing more than one character (e.g., ‘ab’), […] is implementation-defined. long x = ‘\xde\xad\xbe\xef’; // yes, single quotes This is valid ISO 9899:2011 C. It compiles without warning under gcc with -Wall, and a “multi-character character constant” warning with -pedantic. From Wikipedia: Multi-character … Read more
It appears this can be done. I’m unable to determine the version of GCC that it was added, but it was sometime before June 2010. Here’s an example: #pragma GCC diagnostic error “-Wuninitialized” foo(a); /* error is given for this one */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored “-Wuninitialized” foo(b); /* no diagnostic … Read more
You are using a function for which the compiler has not seen a declaration (“prototype“) yet. For example: int main() { fun(2, “21”); /* The compiler has not seen the declaration. */ return 0; } int fun(int x, char *p) { /* … */ } You need to declare your function before main, like this, … Read more
Why should I enable warnings? C and C++ compilers are notoriously bad at reporting some common programmer mistakes by default, such as: forgetting to initialise a variable forgetting to return a value from a function arguments in printf and scanf families not matching the format string a function is used without being declared beforehand (C … Read more
Since you wish to expect a ‘w’ to be inputted (among ‘w’, ‘a’, ‘s’, ‘d’ as probable movement characters ) , which actually is a character hence you must specify %c as an argument to the scanf function. So, your code should look like #include <stdio.h> int main () { int x = 0; int … Read more
The compiler is warning you that the result of the expression x = y is used inside a conditional; I mention this even though it does not appear to be related to your actual question because these days, usually it means that there’s a typo and the author meant to write == instead. Regarding the … Read more