C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Conditional or Ternary Operator

The conditional operator is sometimes called a ternary operator because it involves three operands. It is best understood by considering the following example
younger = son < father ? 18 : 40;

In the above example, son's age is 18 whereas father's age is 40. But we need the younger age so we make use of conditional operator to extract least age.

Conditional or Ternary Operator

Syntax of Conditional Operator in C

  • The conditional operator contains a condition followed by two statements or values.
  • If the condition is true the first statement is executed, otherwise the second statement or value.
Syntax
condition ? (value 1) : (value 2)

How Conditional Operator Works

Conditional operator performs nothing more special. It is the abstraction of if else statement. Consider the following example how Condtional Operator works
younger = true ? 18 : 40;

If the logical expression resulted true, 18 will be stored in a variable younger.
younger = false ? 18 : 40;

If the logical expression resulted true, 40 will be stored in a variable younger.

Program without Conditional Operator

Here we are to find the least age and store it in a variable younger_age without using Conditional operator.

noternary.c
#include <stdio.h> //header file section
int main() //main section
{
int son = 18;
int father = 40;
int younger_age;
if(son < father)
younger_age = son;
else
younger_age = father;
printf("%d is the younger age", younger_age);
return 0;
}
  • 18 is the younger age

Note:

Here the code looks more verbose as we make use of if else statement.

Program Using Conditional Operator

Here we are to find the least age and store it in a variable younger_age using Conditional operator.

ternary.c
#include <stdio.h> //header file section
int main() //main section
{
int son = 18;
int father = 40;
int younger_age;
younger_age = son < father ? son : father;
printf("%d is the younger age", younger_age);
return 0;
}
  • 18 is the younger age

Note:

In line 7, Conditional Operator is get used to reduce the size of the program.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you