Conditional Operator - mikroElektronika PIC Microcontrollers PIC12 User Manual

Mikroc user's manual
Table of Contents

Advertisement

mikroC
making it simple...

Conditional Operator ? :

The conditional operator
ditional operator is:
expression1 ? expression2 : expression3
Expression1
and
sion3
either
that only one of these two expressions evaluates is very important if you expect
them to produce side effects!
Conditional operator associates from right to left.
Here are a couple of practical examples:
/* Find max(a, b): */
max = (a > b) ? a : b;
/* Convert small letter to capital: */
/* (no parentheses are actually necessary) */
c = (c >= 'a' && c <= 'z') ? (c - 32) : c;
Conditional Operator Rules
Expression1
must obey one of the following rules:
1. Both of arithmetic type;
usual arithmetic conversions, which determines the resulting type.
2. Both of compatible struct or union types. The resulting type is the structure or
union type of
3. Both of
MikroElektronika: Development tools - Books - Compilers
? :
evaluates first. If its value is true, then
is ignored. If
expression3
evaluates and
expression2
or
expression2
expression3
must be a scalar expression;
expression2
expression2
type. The resulting type is
void
mikroC - C Compiler for Microchip PIC microcontrollers
is the only ternary operator in C. Syntax of the con-
evaluates to false, then
expression1
is ignored. The result will be the value of
depending upon which evaluates. The fact
expression2
and
expression3
and
.
expression3
.
void
evaluates
expression2
expres-
and
expression3
are subject to the
page
109

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the PIC Microcontrollers PIC12 and is the answer not in the manual?

This manual is also suitable for:

Pic microcontrollers pic16Pic microcontrollers pic18

Table of Contents