Ternary operator in C and C++ - Conditional Operator

<<Previous

Next >>





Ternary Operator ?:

Ternary Operator in C and C++ contains a conditional statement followed by two expressions that return a value. It first evaluates the condition. If the condition returne true, then the first expression will be evaluated and value returned. Otherwise, the second expression will be evaluated and value returned. The conditional operator is also known as ternary operator which takes three arguments.

Ternary Operator in C and C++ - Syntax

Syntax:
	condition ? (expression 1) :(expression 2);

For example:
	X=((2==3) ? 4 : 5);

The above condition (2==3) is false, so the second value 5 is assigned to X.


Ternary Operator in C and C++ - Example

#include <stdio.h>
#include <conio.h>

int main()
{
   int A = 20;
   int B = 15;
   int C;

   C =((A>B)? 20 : 15);

   printf("The C value is : %d \n",C);

   C =((A<B)? 20 : 15);

   printf("The C value is : %d \n",C);
    return(0);
 }

Output:

The value of C is : 20
The value of C is : 15

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














Ternary operator in C and C++ - Conditional Operator

<<Previous

Next >>





Ternary Operator ?:

Ternary Operator in C and C++ contains a conditional statement followed by two expressions that return a value. It first evaluates the condition. If the condition returne true, then the first expression will be evaluated and value returned. Otherwise, the second expression will be evaluated and value returned. The conditional operator is also known as ternary operator which takes three arguments.

Ternary Operator in C and C++ - Syntax

Syntax:
	condition ? (expression 1) :(expression 2);

For example:
	X=((2==3) ? 4 : 5);

The above condition (2==3) is false, so the second value 5 is assigned to X.


Ternary Operator in C and C++ - Example

#include <stdio.h>
#include <conio.h>

int main()
{
   int A = 20;
   int B = 15;
   int C;

   C =((A>B)? 20 : 15);

   printf("The C value is : %d \n",C);

   C =((A<B)? 20 : 15);

   printf("The C value is : %d \n",C);
    return(0);
 }

Output:

The value of C is : 20
The value of C is : 15

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy