- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Ternary Operator
![]() Share with a Friend |
C Programming - C Ternary Operator
C Ternary Operator
The ternary operator in C is a conditional operator that provides a shorthand way of writing if-else statements. It is a compact and efficient way to make decisions in your code.
Syntax
C
condition ? expression1 : expression2;
- condition: An expression that evaluates to true (non-zero) or false (zero).
- expression1: Executed if the condition is true.
- expression2: Executed if the condition is false.
How It Works
- If the condition is true, the expression1 is executed, and its value is returned.
- If the condition is false, the expression2 is executed, and its value is returned.
Example
C
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Using ternary operator
int max = (a > b) ? a : b;
printf("The maximum value is: %d\n", max);
return 0;
}
Output:
The maximum value is: 20
Nested Ternary Operator
You can nest ternary operators to evaluate multiple conditions.
Example:
C
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 15;
// Find the largest of three numbers
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The largest value is: %d\n", max);
return 0;
}
Output:
The largest value is: 20
Advantages
- Conciseness: Reduces multiple lines of if-else statements into a single line.
- Readability: Makes simple conditional assignments easier to read.
- Efficiency: Inline evaluation can be faster in some cases.
Disadvantages
- Complexity in Nesting: Readability decreases with deeply nested ternary operators.
- Harder Debugging: Errors in nested ternary operators can be difficult to trace.
When to Use
- For simple conditions that involve straightforward expressions.
- When the readability of the code is not compromised.
Avoid Misuse
- Use in Simple Conditions:
C
int result = (x > 0) ? x : 0; // Good use
- Avoid Complex Nesting:
C
// Difficult to read and debug
int result = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
- Do Not Use for Side Effects:
C
(a > b) ? printf("A is greater") : printf("B is greater"); // Avoid; better use if-else for such cases
Use Case: Check Even or Odd
C
#include <stdio.h>
int main() {
int num = 5;
// Using ternary operator to check even or odd
(num % 2 == 0) ? printf("Even\n") : printf("Odd\n");
return 0;
}
Output:
Odd
The ternary operator is a powerful and concise tool in C programming, ideal for simple conditional logic. For more complex scenarios, using traditional if-else is recommended to maintain code clarity.
