Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language%20in%20C%20Interview%20Questions%20and%20Answers

Question: If the result of an _expression has to be stored to one of two variables, depending on a condition, can we use conditional operators as shown below?

( ( i < 10 ) ? j : k ) = l * 2 + p ;
Answer: No! The above statement is invalid. We cannot use the conditional operators in this fashion. The conditional operators like most operators, yields a value, and we cannot assign the value of an _expression to a value. However, we can use conditional operators as shown in following code snippet.

main( )
{
int i, j, k, l ;
i = 5 ; j = 10 ; k = 12, l = 1 ;
* ( ( i < 10 ) ? &j : &k ) = l * 2 + 14 ;
printf ( "i = %d j = %d k = %d l = %d", i, j, k, l ) ;
}

The output of the above program would be as given below:
i = 5 j = 16 k = 12 l = 1
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook