Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Currently I have the code (expanded for your benefit):

<number> != <number> ? printf("|\\"); : <number>;

The idea here, is to do this condition in the least number of characters. This is currently, roughly the same number of characters it would take to be doing the exact same thing using an if statement, due to the fact, that I am fulfilling the second argument of the ternary operator which does not need to be filled, it is simply filled, as otherwise the statement does not run. (the final number is really a variable).

Is there any possible way to do the same conditional but shorter? (in C)

share|improve this question

2 Answers 2

up vote 5 down vote accepted
if(x!=y)printf("foo\n");
x!=y?printf("foo\n"):0;
x==y||printf("foo\n");

Yes, you can save some characters with the ternary operator or with the shortcutting logic operators (&& and ||). All three of the above do the same thing. Which you can use depends on things like the return type of printf and whether you need the return value.

Does that answer your question?

share|improve this answer
    
Very good, excellent answer. –  Bernie Feb 23 '13 at 6:50

A year late but one whole character shorter.

x-y&&printf("foo\n");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.