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.

Bitwise Operators in C

Bitwise operators in C are used to perform bit-level operations on integer data types (like int, short, long, char).
These operators work directly on the bits of numbers, not on their decimal or hexadecimal representations.

List of Bitwise Operators

Operator Name Description Example
& Bitwise AND Sets each bit to 1 if both bits are 1 a & b
` ` Bitwise OR Sets each bit to 1 if at least one bit is 1
^ Bitwise XOR (Exclusive OR) Sets each bit to 1 if only one bit is 1 a ^ b
~ Bitwise NOT (One’s Complement) Inverts all bits (1 → 0, 0 → 1) ~a
<< Left Shift Shifts bits to the left, filling with 0 a << n
>> Right Shift Shifts bits to the right a >> n

Examples

Let’s assume:

int a = 5;   // Binary: 0101

int b = 3;   // Binary: 0011

Operation Binary Operation Result Decimal
a & b 0101 & 0011 0001 1
`a b` `0101 0011`
a ^ b 0101 ^ 0011 0110 6
~a ~0101 1010 (depends on system) -6
a << 1 0101 << 1 1010 10
a >> 1 0101 >> 1 0010 2

Detailed Explanation

  1. Bitwise AND (&)
    • Compares each bit of two operands.
    • Only 1 if both bits are 1.

int result = a & b; // 1

Bitwise OR (|)

  • Compares each bit of two operands.
  • 1 if any of the bits is 1.

int result = a | b; // 7

Bitwise XOR (^)

  • 1 if bits are different, 0 if same.

int result = a ^ b; // 6

Bitwise NOT (~)

  • Inverts all bits.

int result = ~a; // -6 (if a = 5, because of 2’s complement)

Left Shift (<<)

  • Moves bits to the left and fills with 0s on the right.
  • Equivalent to multiplying by 2ⁿ.

int result = a << 1; // 10

Right Shift (>>)

  • Moves bits to the right.
  • Equivalent to dividing by 2ⁿ.

int result = a >> 1; // 2

Example Program

#include <stdio.h>

int main() {

int a = 5, b = 3;

printf(“a & b = %d\n”, a & b);

printf(“a | b = %d\n”, a | b);

printf(“a ^ b = %d\n”, a ^ b);

printf(“~a = %d\n”, ~a);

printf(“a << 1 = %d\n”, a << 1);

printf(“a >> 1 = %d\n”, a >> 1);

return 0;

}

Output:

a & b = 1

a | b = 7

a ^ b = 6

~a = -6

a << 1 = 10

a >> 1 = 2

Summary

  • Bitwise operators are used for low-level programming, masking, encryption, graphics, and device control.
  • They are faster than arithmetic operations because they operate directly on bits.

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 *