In C, the conditional operator, also known as the ternary operator, is a shorthand way of writing an if-else statement. It has the following syntax:
condition ? expression1 : expression2;
If condition is true, expression1 is evaluated and becomes the result of the entire expression.
If condition is false, expression2 is evaluated and becomes the result.
Syntax to define constant
const data_type var_name = value;
Examples :
Defining using #define preprocessor
We can also define a constant in C using #define preprocessor. The constants defined using #define are macros that behave like a constant. These constants are not handled by the compiler, they are handled by the preprocessor and are replaced by their value before complication.
Example:
int x = 10;
int y = 20;
int max = (x > y) ? x : y; // If x is greater than y, max will be x, otherwise y
Resources :
Test Your Knowledge
Choose The
With supporting text below as a natural lead-in to additional content.