how do you use exitreturn statements in ternary operators?

How to use an exit/return statement in ternary operator?

You cannot. The second and third operands of a ternary operator must be expressions, and a return statement is not an expression (see Ternary operators and Return in C). It is, however, allowed for the two to both be void expressions, such as exit(1), in which case the result of the operation is also a void expression.

Additionally, with respect to your specific code, the comma operator (,) has the lowest precedence of all C operators. Therefore, if you intend to use a comma expression as an operand in any other operation, then you need to parenthesize it.

I wanted to formulate these two if statements in a shorter way:

Why? Mere conciseness or brevity is not a useful objective. If it makes your code hard for a human to read and understand, then making your code shorter is a liability.

But if you mean that you want to express the same thing more simply, more clearly, and / or less redundantly, then that can lead you toward forms that also happen to be shorter. For example, you can encapsulate the logic in a function or macro. Personally I often supply a macro that looks something like this:

#define ERROR_IF_NZ(cond, message) do { \
    if (cond) { \
        fputs(message, stderr); \
        exit(1); \
    } \
} while (0)

Using that, your code could look like this:

ERROR_IF_NZ(argc != 2, "Error X\n");
ERROR_IF_NZ(NULL == file, "Cannot open file!\n");

Adjust the macro name so that it makes the most sense to you, and look! It is not only clearer than the original code, but shorter, too, even if you count the length of the macro definition.


Edits to the question have pretty much mooted this answer. If you already have functions that serve the the same role that a macro such as described here would do, then just call them. There is no redeeming value in replacing the functions and the calls to them with a ternary expression, nor is it worth any effort to convert the functions you have into macros. Certainly, reducing source code size is not an reasonable motivation for such changes.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top