In this article Conditional operator in C we give the information about Conditional operator also know as ternary operator. Ternary operator pair “?:” In C

Increment and decrement operators in C:

C allows two very useful operators that are not commonly found in other languages. These increment and decrement operators are:

++ and — —

Operator ++ used to adds 1 to the operand, while — used to subtracts 1. Both are single operators and take the following:

++ m; Or m ++;                                    — –M; Or, M– –;

Consider the following: m = 5;

y = ++ m; // prefix operator

In this case, the values of y and m will be 6. Suppose, if we write the above statement like this  m = 5;

y = m ++; // Post-fix operator

So, the value of y will be 5 and m 6.

A prefix operator adds 1 to the first operand and then the result is assigned to the variable to the left.

The post-fix operator first assigns a value to the left variable and then extends the operand.

++ and — Rules for the operator:

  1. The increment (++) and decrement (–) operators are unary operators and they need variables as their operand.
  2. When the prefix ++ (or – –) is used with a variable in a variable, the expression is evaluated using the original value of the variable and then the variable is increased (or decreased) by one.
  3. When the prefix ++ (or – –) is used in an expression, the variable is first increased (or decreased) and then the expression is evaluated using the new value of the variable.

Conditional operator in C:

Conditional operator:

Ternary operator pair “?:” In C. Is available to create conditional expressions of

Exp1? Exp2: Exp3

Where exp1, exp2 and exp3 are expressions.

Operator ?: works as follows:

exp1 is first evaluated. If it is not zero (true), the exp2 expression is evaluated and becomes the value of the expression. If exp1 is the flag, then exp3 is evaluated and its value becomes a value expression.

Note that only one expressions is evaluated.

For example, consider the following statements.

l=100;

m=250;

x=(l > m) ? l : m;

In this example, x will be assigned the value of b.

i.e. x=250.

Special Operators in C:

Some of the special operators used in c languages. These operators are referred as separators or punctuates.

Ampersand (&) Comma(,)
Asterisk(*) Ellipsis(….)
Braces({}) Hash(#)
Brackets([]) Parenthesis(())
Colon(:) Semicolon(;)

Example based on sizeof operators:

#include <stdio.h>
#include<conio.h>
int main()
{
int a;    char b;     float c;    double d;  long double q;
printf(“1st  Storage size for int data type:%d \n”,sizeof(a));
printf(“2nd  Storage size for char data type:%d \n”,sizeof(b));
printf(“3rd  Storage size for float data type:%d \n”,sizeof(c));
printf(“4th  Storage size for double data type:%d\n”,sizeof(d));

printf(“5th  Storage size for long double data type:%d\n”,sizeof(q));
return 0;
}

OUTPUT:

1st  Storage size for int data type:2

2nd  Storage size for char data type:1

3rd  Storage size for float data type:4

4th  Storage size for double data type:8

5th  Storage size for double data type:10

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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