Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
11 views
in Tutorial & Interview questions by Goeduhub's Expert (7.8k points)

Evaluates its first operand, and, if the resulting value is not equal to zero, evaluates its second operand. Otherwise, it evaluates its third operand, as shown in the following example:

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.8k points)
 
Best answer

Evaluates its first operand, and, if the resulting value is not equal to zero, evaluates its second operand. Otherwise, it evaluates its third operand, as shown in the following example:

a = b ? c : d;

is equivalent to:

if (b)

a = c;

else

a = d;

This pseudo-code represents it : condition ? value_if_true : value_if_false. Each value can be the result of an evaluated expression.

int x = 5; 

int y = 42;

printf("%i, %i\n", 1 ? x : y, 0 ? x : y); /* Outputs "5, 42" */

The conditional operator can be nested. For example, the following code determines the bigger of three numbers:

big= a > b ? (a > c ? a : c)

: (b > c ? b : c);

The following example writes even integers to one file and odd integers to another file:

#include<stdio.h> 

int main()

{

FILE *even, *odds; int n = 10;

size_t k = 0; 

even = fopen("even.txt", "w");

odds = fopen("odds.txt", "w"); 

for(k = 1; k < n + 1; k++)

{

k%2==0 ? fprintf(even, "\t%5d\n", k)

: fprintf(odds, "\t%5d\n", k);

}

fclose(even); 

fclose(odds); 

return 0;

The conditional operator associates from right to left. Consider the following:

exp1 ? exp2 : exp3 ? exp4 : exp5

As the association is from right to left, the above expression is evaluated as

exp1 ? exp2 : ( exp3 ? exp4 : exp5 )

Related questions

0 like 0 dislike
1 answer 4 views
0 like 0 dislike
1 answer 7 views
0 like 0 dislike
1 answer 8 views
0 like 0 dislike
1 answer 8 views
0 like 0 dislike
1 answer 7 views
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...