ãã5年以ä¸Cè¨èªã§ã³ã¼ããæ¸ãã¦ãã¦ããããä»ã§ã¯ããã§åå ¥ãå¾ã¦ãã身ã ã¨ããã®ã«ãå®ã¯ä»ã¾ã§ãA && B || Cããã©ãè©ä¾¡ãããã®ãã¤ãã¤ã確信ãæã¦ã¦ããªãã£ãã
ãããCã®è«çæ¼ç®åã¯å·¦ããå³ã«é ã«è©ä¾¡ãã¦ããã¨ãç絡è©ä¾¡ãããã¨ãããã®è¾ºãã¯ç解ãã¦ããã
ãã Cè¨èªã®ä»æ§ã«åºã¥ã解éã¨ãã¦ãä¾ãã°ãA && B || Cãã¨æ¸ããæã®è©ä¾¡çµæã¯ã
- ã(A && B) || Cã
- ãA && (B || C)ã
ãã®ã©ãã¨çããã®ãï¼ããåªå 度㯠'||' ããã '&&' ã®æ¹ãé«ãã®ã§ã1çªç®ã®è§£éã«ãªãã®ã ãããï¼ãã¨æãã¤ã¤ãç¹ã«èª¿ã¹ãããã§ããªãæ¾ç½®ãã¦ããã
ã§ããããµããªã¾ã¾éããã¦ããããã¤ãæè¿ã«ãªã£ã¦ã¨ããã½ã¼ã¹ã³ã¼ãä¸ã«ãA && B || Cããªé¨åããã£ã¦ãTDM-GCCã§ã³ã³ãã¤ã«ãããè¦åãåºãã
warning: suggest parentheses around '&&' within '||'
è¦ã¯ãé©å®æ¬å¼§ãè£ãã¾ããããã¨ãããã¨ã ããï¼
ãã®è¦åãè¦ã¦ãè¬ã解æãã¹ãæãéã«æ¥ãï¼ãã¨æã£ã次第ã§ãã¾ã試ãã«å¹¾ã¤ãã®å¦çç³»ã®æåã調ã¹ã¦ã¿ããã¨ã«ããããããªãã¹ãã³ã¼ããç¨æã
#include <stdio.h> static int a(void) { (void) puts("a"); return 1; } static int b(void) { (void) puts("b"); return 1; } static int c(void) { (void) puts("c"); return 1; } int main(void) { (void) (a() && b() || c()); return 0; }
é¢æ°aãcã®æ»ãå¤ã®å¤ãå¤ãã¦ã¿ã¦ãã©ã®é¢æ°ãã©ã®é çªã§å®è¡ãããã表示ããã¦ã¿ãã
å®é¨ããå¦çç³»ã¯ä»¥ä¸ã®éãã
å¦çç³» | OS |
---|---|
TDM-GCC 4.5.1 | Windows XP SP3 |
Microsoft Visual Studio 2005 SP1 | åä¸ |
Microsoft Visual Studio 2010 | åä¸ |
Digital Mars C/C++ Compiler 8.49 | åä¸ |
GCC 4.2.1 | FreeBSD 8.1-RELEASE |
GCC 4.3.2 | Debian GNU/Linux 5.0 |
ã©ã®å¦çç³»ã使ã£ã¦ããçµæã¯ãã®éãã1ã¨ã0ã¯ãåé¢æ°ã®æ»ãå¤ã
a() | b() | c() | çµæ |
---|---|---|---|
1 | 1 | 1 | a -> b |
1 | 0 | 1 | a -> b -> c |
0 | 1 | 1 | a -> c |
æå¾ã®ãã¹ãçµæããæ¨æ¸¬ããã«ããA && B || Cãã¨æ¸ãã¨ã(A && B) || Cãã¨åãããã«è©ä¾¡ãããããã ã
ã¨ã¯ããæè¿ãªå¦çç³»ã®æåãè¦ãã ããªã®ã§ããçµå±ãä»æ§çã«ã¯ã©ããªã®ï¼ãã¨ããçåã¯æ´ããªãã¾ã¾ã ã£ããããã
調ã¹ã¦ã¿ãããï¼³ã»ï¼°ã»ãã¼ãã½ã³ï¼ä¸ã¨ï¼§ã»ï¼¬ã»ã¹ãã£ã¼ã«ã»ã¸ã¥ãã¢ã®Cãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ãã®P.268ã«ãããªè¨è¿°ããã£ãã
ï¼çç¥ï¼&& 㯠|| ããåªå é ä½ãé«ããï¼æ¬å¼§ã使ãæ¹ãèªã¿ããããªããã¨ãå¤ãï¼ ä¾ a<b || b<c && c<d || d<e ã¯ï¼æ¬¡ã®ããã«æ¸ãæ¹ãæå¿«ã§ããï¼ a<b || (b<c && c<d) || d<e
ã¨ãããã¨ã¯ãä»æ§çã«ãã(A && B) || Cãã¨è§£éãããã®ãæ£è§£ãªã®ã ãããï¼
ã¨ããã§ãä½ã§ä»ã¾ã§ç解ãææ§ãªã¾ã¾ã§ã大ä¸å¤«ã ã£ããã¨ããã¨ãé©å®æ¬å¼§ãè£ã£ã¦ã³ã¼ããæ¸ãã¦ããããã ãä½ãããã®æ¹ãã³ã³ãã¤ã©ã«ãä»ã®ããã°ã©ãã«ãç§ã®æå³ãä¼ãããããã®ã§ãä»äºã§ã³ã¼ããæ¸ãã¦ããã¨ããããªãã®ã ã
ãã¦ãã¨ã³ããªåã®ããA && B || Cãã®è©ä¾¡çµæã«æ°ãã¤ãããã ãâ¦â¦ä»¶ã®ã³ã¼ãã¯ã©ãèãã¦ããA && (B || C)ãã¨è©ä¾¡ãããªããã°ãªããªãã®ã ããæ¬å¼§ãç¡ãã®ã§ã(A && B) || Cãã¨åãããã«è©ä¾¡ããã¦ãã¾ã訳ã§ãæä½ã«è¨ãã°ãã°ã£ã¦ããã®ã ã