C Ternary Operator (With Examples)
C Ternary Operator (With Examples)
C Ternary Operator
We use the ternary operator in C to run one code when
the condition is true and another code when the condition
is false. For example,
#include <stdio.h>
int main() {
int age;
return 0;
}
Output 1
Output 2
int main() {
// create variables
char operator = '+';
int num1 = 8;
int num2 = 7;
return 0;
}
// Output: 15
variable.
36%
C
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
off
with Programiz PRO! utm_source=sticky-
Claim Discount
In someNowof the cases, banner&utm_campaign=programiz&utm_medium=referral)
we can replace the if...else
(https://programiz.pro/learn/master-c-programming?
Programiz
statement with a tutorials
ternary & operator.
PRO
utm_source=nav-
Search examplesThis will make our code
(/)
floating&utm_campaign=programiz&utm_medium=referral)
cleaner and shorter.
www.domain-name.com
#include <stdio.h>
int main() {
int number = 3;
if (number % 2 == 0) {
printf("Even Number");
}
else {
printf("Odd Number");
}
return 0;
}
#include <stdio.h>
int main() {
int number = 3;
return 0;
}
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/int
u=https://www.programiz.com/c- text=Check%20this%2
programming/ternary-operator) programming/ternary-o
Related Tutorials
C Tutorial
C Programming Operators
(/c-programming/c-operators)
C Tutorial
C if...else Statement
(/c-programming/c-if-else-statement)
C Tutorial
C switch Statement
(/c-programming/c-switch-case-statement)
C Tutorial
C for Loop
(/c-programming/c-for-loop)