Ternary Operator in C [?:] (Conditional Operator) - Do Tech Blog

Ternary Operator in C [?:] (Conditional Operator)

If you are learning a C programming language, then you must have read your operator, which is a part of the Ternary operator which we will know that Ternary operator in example and its syntax.

 

    What is the operator


    The operator is a symbol that is used to perform operations on operands. Example adding the meaning of +, subtracting the meaning of -.


    The operator is divided into

    1. Unary operator 
    2. Binary operator 
    3. Ternary operator 
    4. Special operator 


    What is Ternary Operator in C


    In Computer Programming ?: This type of symbol is called Ternary Operator. It is used to check its condition.


    Definition of Ternary Operator An operator who performs an operation with three operands, meaning it is known by its name that it is with three operands and calculates it is also called the conditional operator.


    Syntax of Ternary Operator


    Variable = Expression1? Expression2: Expression3


    Max = (a> b)? a: b;


    If this given syntax example is understood then when a is greater than b then the value of 'a' will be put in max otherwise the value of b will be put in max.

    Know this syntax well with the program


    Finding a ternary operator to be the largest number out of two numbers in the program.


    Ternary Operator Diagram


    Ternary_Operator_in_C

    Ternary-Operator-in-C

    Ternary Operator Example


    #include <stdio.h>

    #include <conio.h>

    void main ()

    {

    int a, b, large;

    clrscr ();

    printf ("Enter Value of a:");

    scanf ("% d, & a");

    printf ("Enter Value of b:");

    scanf ("% d, & b");

    large = (a> b)? a: b;

    printf ("The large value is:% d", large)

    getch ();

    }

    Input

    Enter Value of a: 5

    Enter Value of b: 3

    output

    The large Value is: 5


    In this program, there are three integer variables a, b, large in which the values of a and b are taken from the user and in which we use the value 5 of a and the value 3 of b and the syntax of the Ternary operator is used.

    large = (a> b)? a: b;


    In which if the value of a is greater than b, the value of a will go to large and if a value is not greater than b, then the value of b will go into large.


    Ternary Operator Example in C


    #include <stdio.h>

    #include <conio.h>

    void main ()

    {

    int a, b, c, large;

    clrscr ();

    printf ("Enter Value of a:");

    scanf ("% d, & a");

    printf ("Enter Value of b:");

    scanf ("% d, & b");

    printf ("Enter Value of c:");

    scanf ("% d, & c");


    large = a> b? (a> c? a: c): (b> c? b: c);

    printf ("The large value is:% d", large)

    getch ();

    }

    Input

    Enter Value of a: 5

    Enter Value of b: 3

    Enter Value of c: 10


    output

    The large Value is: 10

     

    Recommend: Call By Value And Call By Reference


    No comments :

    Post a Comment

    ;