home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18796 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.5 KB  |  89 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!ukma!netnews.louisville.edu!starbase.spd.louisville.edu!dsembr01
  3. From: dsembr01@orion.spd.louisville.edu (Darren S. Embry)
  4. Subject: QUESTION: TRICKS USING THE ? : OPERATOR
  5. Sender: news@netnews.louisville.edu (Netnews)
  6. Message-ID: <dsembr01.725056046@starbase.spd.louisville.edu>
  7. Date: Tue, 22 Dec 1992 20:27:26 GMT
  8. Nntp-Posting-Host: orion.spd.louisville.edu
  9. Organization: University of Louisville
  10. Lines: 77
  11.  
  12.  
  13. Okay, here are a couple of interesting and maybe useful tricks I discovered
  14. that involve the trinary ? : operator:
  15.  
  16. 1)    Using it as a rudimentary if-else statement:
  17.  
  18.         (2*3==6)
  19.             ? printf ("TRUE")
  20.             : printf ("FALSE");
  21.         
  22.     as a substitute for
  23.     
  24.         if(2*3=6)
  25.             printf("TRUE");
  26.         else
  27.             printf("FALSE");
  28.         
  29. 2)    Nesting the ? : operator, as in the example below:
  30.  
  31.     a?(b?c:d):e        can be written as    a?b?c:d:e
  32.     a?b:(c?d:e)        can be written as    a?b:c?d:e
  33.     
  34. I discovered that both of these work using gcc and cc.  Here's a test program
  35. I produced and here's the output:
  36.  
  37. ------------------------------------------------------ test.c ----------------
  38. #include<stdio.h>
  39.  
  40. main()
  41.         {
  42.  
  43.         (2*3==6)
  44.                 ? printf("\n---TRUE---\n")
  45.                 : printf("\n---FALSE---\n") ;
  46.  
  47.         printf("%d %d\n",1?1?1:2:3,1?(1?1:2):3);
  48.         printf("%d %d\n",1?0?1:2:3,1?(0?1:2):3);
  49.         printf("%d %d\n",0?1?1:2:3,0?(1?1:2):3);
  50.         printf("%d %d\n",0?0?0:2:3,0?(0?1:2):3);
  51.  
  52.         printf("%d %d\n",1?1:1?2:3,1?1:(1?2:3));
  53.         printf("%d %d\n",1?1:0?2:3,1?1:(0?2:3));
  54.         printf("%d %d\n",0?1:1?2:3,0?1:(1?2:3));
  55.         printf("%d %d\n",0?1:0?2:3,0?1:(0?2:3));
  56.  
  57.         printf("%d %d\n", 1?0:1?2:3,1?0:(1?2:3));
  58.  
  59.         }
  60. ----------------------------------------------------- end of test.c --------
  61.  
  62. ----------------------------------------------------------- output ----------
  63. ---TRUE---
  64. 1 1
  65. 2 2
  66. 3 3
  67. 3 3
  68. 1 1
  69. 1 1
  70. 2 2
  71. 3 3
  72. 0 0
  73. ---------------------------------------------------- end of output ---------
  74.  
  75. Now, here are a couple of questions that you must answer:
  76.  
  77. 1) Is this valid ANSI or K&R C?
  78. 2) Does it work this way on other compilers for other systems?
  79. 3) Is this recommended?
  80.  
  81. I would appreciate lots of answers, and any objections to using this.
  82.  
  83. Thanks in advance,
  84. -- 
  85. ---->8------ Darren Embry --- dsembr01@starbase.spd.louisville.edu -----8<----
  86.  o  o   This is the Internet.  This is a .signature.  This is the 1990's.  You
  87. \____/  know what to do.  This sentence is the implied disclaimer.  You know
  88.         where to direct flames.
  89.