2014/02/21

Reason why you must not call function in condition

you cannot guarantee all functions are going to be called in condition.
See code below...




 #include <stdio.h>  
 int funcA(){  
   printf("funcA \n");  
   return 0;  
 }  
 int funcB(){  
   printf("funcB \n");  
   return 1;  
 }  
 int main()  
 {  
   int a = 0;  
   int b = 1;  
   if( funcA() || funcB() )  
   {  
     printf("first condition! funcA() || funcB() \n\n");  
   }  
   if( funcB() || funcA() ){  
     printf("second condition! funcB() || funcA() \n\n");  
   }  
   if( funcA() && funcB() )  
   {  
     printf("third condition! funcA() && funcB() \n\n");  
   }  
   if( funcB() && funcA() ){  
     printf("fourth condition! funcB() && funcA() \n\n");  
   }  
   return 0;  
 }  




funcA
funcB
first condition! funcA() || funcB()

funcB
second condition! funcB() || funcA()

funcA
funcB
funcA



* if the function calls are related to I/O, you may make a mistake and cause memory leak.

No comments:

Post a Comment