495 views
0 votes
0 votes
i was trying a program with macros and wanted to throw compile time error. I resolved my program however on further studying i am confused with the use of logical operator. Please help me to understand this

for the following program with && operator output shown is "successfully  Performed"

#include <stdio.h>

int main() {
   int age=15;

   #if (age>5) && (age>10)   /*replace && with || and check for different combination of relational operator*/
   #error cannot perform this operation
   #endif
   printf("successfully performed");
   return 0;
}

the same output is produced if we replace (age>10) with (age<10). BUT if i use logical OR || operator this will throw the #error.

Why is this so? is it undefined behaviour of compiler?

1 Answer

0 votes
0 votes

Heard of short circuiting? 

for || - as soon as it got any statement true (from LHS to RHS) compiler  will not check further and will returns true; else false;

for && -as soon as it got statement false (from LHS to RHS) compiler  will not check further and will returns false; else true;

Hope it help.

Related questions

0 votes
0 votes
2 answers
1
Debargha Mitra Roy asked Apr 10
101 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
3 votes
3 votes
3 answers
2
Laxman Ghanchi asked May 19, 2023
1,159 views
#include<stdio.h void print(int n) { printf("Hello "); if(n++ == 0) return ; print(n); n++; } int main() { void print(); print(-4); }How many times printf execute?? And H...
0 votes
0 votes
1 answer
3
Laxman Ghanchi asked May 19, 2023
685 views
#include<stdio.h>void print(int n){ printf("Hello "); if(n++ == 0) return ; print(n); n++;}int main(){ void print(); print(-4);}