C Interview Questions and Answers

 

There seem to be a few missing operators ...

There seem to be a few missing operators, like ^^, &&=, and ->=.





A logical exclusive-or operator (hypothetically ``^^'') would be nice,
but it couldn't possibly have short-circuiting behavior analogous to && and
|| Similarly, it's not clear how short-circuiting would apply to hypothetical
assignment operators &&= and ||=. (It's also not clear how often &&= and ||=
would actually be needed.)



Though p = p->next is an extremely common idiom for traversing a linked list,
-> is not a binary arithmetic operator. A hypothetical ->= operator therefore
wouldn't really fit the pattern of the other assignment operators.



You can write an exclusive-or macro in several ways:



#define XOR(a, b) ((a) && !(b) || !(a) && (b)) /* 1 */



#define XOR(a, b) (!!(a) ^ !!(b)) /* 2 */



#define XOR(a, b) (!!(a) != !!(b)) /* 3 */



#define XOR(a, b) (!(a) ^ !(b)) /* 4 */



#define XOR(a, b) (!(a) != !(b)) /* 5 */



#define XOR(a, b) ((a) ? !(b) : !!(b)) /* 6 */

Posted by:Richards