Given this code:
#include <stdio.h> int main( int argc, char** argv ) { switch (0) { case 1: printf("1"); case 2: printf("2"); default: printf("default"); break; } return 0; }
I expect the compiler to tell me that something about the conditional expression is constant ( switch (0)while VS2012 throws this warning for if(0)) or unreachable code ( case 1, case 2).
switch (0)
if(0)
case 1
case 2
However, neither on ideone , nor on Visual Studio 2012, nor on the latest GCC, I get anything.
Why don't even worthy compilers complain here?
I just tried it on CLANG with the extended warning turned on and received the following warning:
( ) , :
case 1: printf("1");
C 2011 standard:
5.1.1.31 ( ), , undefined . . 9)9) , , , . , . .
.
, , switch if ...
switch
if
6.8.4.1 if1 if ....6.8.4.2 switch1 switch . 2 switch case default , switch . 154)3 case , case switch . switch default. ( switch default case , case switch.)154) switch, case default, switch, , .
case
default
... .
; if switch, . if( 0 ) switch( 0 ) , - .
if( 0 )
switch( 0 )
clang -Weverything: , argc argv , switch(0), gcc.
clang -Weverything
argc
argv
switch(0)
gcc
, , . , :
int test(int l) { switch (1) { case 0: return 0; case 1: return 1; default: return -1; }
EDIT: clang , , , ryyker , -Wunreachable-code . .
clang
-Wunreachable-code
Visual Studio 2013, :
if, .
Source: https://habr.com/ru/post/1618135/More articles:Сортировка сортировки для парных в java - javaWhich NSAssertion should be used inside a block? - iosС++ io streams sync_with_stdio нет разницы - c++I can’t leave comments on Google+ - c #anano (python): elemental gradient - pythonRails: Is this a precedent for unidirectional inheritance (STI)? - sqlHow to include VLC in my C # project? - c #How to match browser localization with one of my php gettext i18n? - phpKibana for MySql - mysqlTransactions inside ManagedScheduledExecutorService tasks on TomEE 7.0.0? - java-eeAll Articles