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

You may also like...

49 Responses

  1. Anonymous says:

    there is error showing in printf

  2. Anonymous says:

    is semicolon necessary after conditional operator

  3. Anonymous says:

    semicolon is necessary

    • rat says:

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

  4. prasanth says:

    & symbol should be there before the c

  5. we can display the condition like that….
    c=((a

  6. Anonymous says:

    include the header files
    #include
    in the program

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

  8. Anonymous says:

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

  9. Tayaba says:

    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

  10. Anonymous says:

    *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;

  11. Avinash says:

    for example 1:
    a=5 and b=4

  12. Rakshith says:

    Program is not giving proper output showing as
    -12-14-16

  13. venkat says:

    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

  14. how work the conditional operator with 3 values

  15. Anonymous says:

    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

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

  17. Anonymous says:

    itz not executing

  18. LearnCOnline says:

    We have recently started a new forum.
    http://forum.learnconline.com/

    Readers can now post their queries and get it resolved.

    Thanks,
    LearnCOnline Team

  19. #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..:)

  20. Kidi ARjun says:

    In this program statment missing

  21. a>b ? a>c?a:c:b>c?b:c

    How this condtion is evaluated…
    Give me ans. immediatly

  22. Anonymous says:

    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);
    }

  23. Anonymous says:

    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

  24. Praveen Rai says:

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

  25. Anonymous says:

    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));
    }

  26. Freshkidd"s says:

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

  27. Anonymous says:

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

  28. Anonymous says:

    prasanth
    #include
    #include
    void main(){
    int a = 10, b = 11;
    int c;
    c = (a < b)? a : b;
    printf(

  29. deepz z says:

    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(

  30. Anonymous says:

    #include
    #include

    main(){
    int a = 10, b = 11;
    int c;

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

  31. Anonymous says:

    HEY SIR GIVEN PROGRAM IS SHOWING ERROR IN PRINTF SENTEX

  32. #include

    main(){
    int a = 10, b = 11;
    int c;

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

    plz try this

  33. ganes says:

    player=(player%2)?1:2;
    how does this works

  34. LF says:

    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

  35. rat says:

    please help me out..
    any solution related to this statement
    a= a+=count==0?5:4;

  36. gokul says:

    can we use quadratic equation using conditionl
    operator

  37. avinash says:

    #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

  38. pankaj says:

    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

  39. sehrish says:

    #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?

  40. Deb says:

    Awesome. I liked it. šŸ™‚ Learning C wont be much simpler than this. Wow. Thank you so much

  1. July 3, 2020

    […] Conditional Operator […]

  2. July 6, 2020

    […] Conditional Operator […]

  3. July 6, 2020

    […] Conditional Operator […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet