# Conditional operator in C

Conditional operator in C is also known as ternary operator. It is called ternary operator because it takes three arguments. It evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.

**Syntax:**

condition ? result1 : result2;

If the condition is true, result1 is returned else result2 is returned.

## Examples:

10==5 ? 11: 12; // returns 12, since 10 not equal to 5. 10!=5 ? 4 : 3; // returns 4, since 10 not equal to 5. 12>8 ? a : b; // returns the value of a, since 12 is greater than 8.

### Program in C using Conditional or Ternary operator

#include<stdio.h> #include<conio.h> void main() { int a = 10, b = 11; int c; c = (a < b)? a : b; printf(“%d”, c); }

**Output:**

10

In the above program, if the value of a is less than b then the value of a would be assigned to the variable b. Else, the value of b would be assigned to variable c. In this case, the value of a is 10 and value of b is 11 i.e., a < b is true. Hence, the value of a i.e., 10 will be assigned to variable c.

You may also want to read **if-else statement in C**, an alternative to the conditional or ternary operator

there is error showing in printf

int c, a=5, b=4;

c=a<b?a:b;

cout<<"the answer is : "<<c<<endl;

system("pause");

return 0;

}

no error found.you try again this program taking getch().

is semicolon necessary after conditional operator

yes

semicolon is necessary

if semicolon is not used then, build-in error will be displayed.. so it is absolute necessary i guess

& symbol should be there before the c

we can display the condition like that….

c=((a

yes but taking condition this type:

c=((a<b)? a ? b);

include the header files

#include

in the program

program is not compiling!!! shows error in printf statement & void main

no, program is correct. your statement wrong that’s why error is found.

include stdio.h header file , check double quotes r proper in printf statement and try it once with only main(by removing void)

1.int a, b;

#define MAX(x,y) ((x) > (y)) ? (x) : (y)

a = 3;

b = MAX (3, a++);

wat will be the output for b n a?

2.int a, b;

#define MAX(x,y) ((x) > (y)) ? (x) : (y)

a = 3;

b = MAX (a++, 3);

wat will b de output for a n b?

plz explain ur answers also

*In the name of GOD*

in example 1:

x will be 3;

y will be 3;

so result 2 will be assigned to b, so b=3;

then a will be increase one, so a=4;

notice that a will increase after checking in ternary operator, because u’ve used a++, not ++a;

in example 2:

x will be 3;

y will be 3;

so b will be y, b=3;

then a will increase to 4;

for example 1:

a=5 and b=4

Program is not giving proper output showing as

-12-14-16

Example 1:

a=5 and b=4

first: max(3,a++)=((3)>(a++)?(3):(a++)

condition is false a become is 4

after that a++ is b=4 and a is 5

how work the conditional operator with 3 values

both example 1 and 2 gives the same value of a and b.

ex1:x=3,y=a++=3

then, a=3 (given)

b=max(3,3) which is 3

ex2:x=a++=3, y=3

hence a=3 and b= max(3,3) which is 3

write a program to read 3 no. And print gretest no………using ternery operator please solve hurry

itz not executing

We have recently started a new forum.

http://forum.learnconline.com/

Readers can now post their queries and get it resolved.

Thanks,

LearnCOnline Team

#include

#include

int main()

{

int a = 10, b = 11;

int c;

c = ((a < b)? a : b);

printf(“%d”, c);

return 0;

}

for me it works this way..:)

In this program statment missing

a>b ? a>c?a:c:b>c?b:c

How this condtion is evaluated…

Give me ans. immediatly

I have corrected the program for you.

/*Conditional operator*/

#include

void main()

{

int a = 10, b = 11;

int c;

c = (a < b)? a : b;

printf(“%d”, c);

}

you havent given the directives

#include

#include

int main (void)

{

int a = 10, b = 11;

int c;

c = (a < b)? a : b;

printf(“%d”, c);

return 0;

}

this should run if it doesnt also add ‘getch();’ in the end

if we use any operator as that +,-,/,* in scanf what it will be print

Getting 3 numbers from input and printing the GREATEST:

#include

#define MAX(A, B, C) (((A) >= (B)) ? (((A) >= (C)) ? (A) : (C)) : (((B) >= (C)) ? (B) : (C)))

int main()

{

int a = 0, b = 0, c = 0;

printf(“Enter the first number: “);

scanf(“%d”, &a);

printf(“Enter the second number: “);

scanf(“%d”, &b);

printf(“Enter the third number: “);

scanf(“%d”, &c);

printf(“\nThe largest number of %d, %d, and %d is %d.”, a, b, c, MAX(a, b, c));

}

lol i am a new programmer age 12 .. you guys make it look har lamo… i can do all this with ease…

what does “a:b” mean ….??

prasanth

#include

#include

void main(){

int a = 10, b = 11;

int c;

c = (a < b)? a : b;

printf(

If u try to run this program in Turbo c…Just modify the above program as below………

/*Conditional operator*/

#include

#include

void main()

{

int a = 10, b = 11;

int c;

clrscr();

c = (a < b)? a : b;

printf(

#include

#include

main(){

int a = 10, b = 11;

int c;

c = (a < b)? a : b; printf(“%d”, c); getch();

}

HEY SIR GIVEN PROGRAM IS SHOWING ERROR IN PRINTF SENTEX

#include

main(){

int a = 10, b = 11;

int c;

c = (a < b)? a : b; printf(“%d”, c);

}

plz try this

player=(player%2)?1:2;

how does this works

int b = 5, c = 15, d = 8, e = 8,a;

a = b>c?c>d?12:d>e?13:14:15;

printf(“%d”,a);

output=15

anyone can explain me plz.. in brief ..hw it happen

please help me out..

any solution related to this statement

a= a+=count==0?5:4;

can we use quadratic equation using conditionl

operator

#include

#include

main()

{

int a,b,c;

clrscr();

printf(“enter the a,b values);

scanf(“%d %d”,&a,&b);

c=(a<b)?a:b;

printf("c=%d',c);

getch();

}

a=10,b=15

void main()

{ int i=5,j=8,k=10,x,y,a;

x=a** -j;

y=k++ * j–;

printf(“Value of x %d”,x);

printf(“Value of y %d”,y);

}

ans: x=856

y=80

plz explain this

#include

int main ()

{

int n;

printf (“enter an integer\n:”);

scanf(“%d”,&n);

switch (n%2)

{

case 0;

printf(“number is even\n”);

break;

case 1;

printf(“number is even\n”);

break;

}

return 0;

}

What will be the output of the above program?

Awesome. I liked it. 🙂 Learning C wont be much simpler than this. Wow. Thank you so much