Operators in C++

sizeof operator in C++

sizeof operator in C++ is an unary operator which can be used to find the size of the data-type or expression or a variable. It returns the size of the memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by using sizeof operator as shown in the below code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int sizeOfInt; sizeOfInt = sizeof(int); cout << sizeOfInt << ” Bytes”; getch(); } Output: 2 Bytes As you can see, the above code calculated the size of int data type and displayed the result in C++.

Conditional Operator in C++

Simple conditional operations can be carried out with the conditional operator (? :). An expression that makes use of the conditional operator is called a conditional expression. Such an expression can be written in place of the more traditional if – else statement. A conditional expression is written in the form: expression 1 ? expression 2 : expression 3 While evaluating a conditional expression, expression 1 is evaluated first. If expression 1 is true (i.e., if its value is non-zero), then expression 2 is evaluated and this becomes the value of the conditional expression. However, if the expression 1 is false (i.e., if its value is zero) then expression 3 is evaluated and this becomes the value of the conditional expression. Note that only one of the embedded expressions (either expression 2 or expression 3) is evaluated determining the value of a conditional expression. Example: In the conditional expression below....

Assignment Operators in C++

There are several different assignment operators in C Plus Plus (C++). All of them are used to form assignment expressions, which assign the value of an expression to an identifier. The most commonly used assignment operator is =. Assignment expressions that make use of this operator are written in the form: identifier = expression where, identifier generally represents a variable, and expression represents a constant, a variable or a more complex expression. Example: a = 3; x = y; sum = a + b; In the above statements, the first assignment expression causes the integer value 3 to assigned to the variable a, and the second assignment causes the value of variable y to be assigned to x. In the third assignment, result in the value of the arithmetic expression is assigned to the variable sum i.e., the value of a + b is assigned to sum. Remember that the...

Logical Operators in C++

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators in C Plus Plus (C++) language. They are: Operator Meaning && Logical AND || Logical OR ! Logical NOT An expression involving && or || is sometimes called compound expressions, since the expression involves two other expressions, that is, each of these operators (&& and ||) takes two expressions, one to the left and another to the right. Example of && operator: Consider the following expression: a > b && x == 10 The expression on the left is a > b and that on the right is x == 10. The above stated whole expression evaluates to true (1) only if both the expressions are true (i.e., if a is greater than b and the value of x is equal to 10). Example of || operator: Consider the following expression: a...

Relational Operators in C++

There are four relational operators in C Plus Plus (C++). They are: Operator Meaning < less than > greater than <= less than or equal to >= greater than or equal to These operators all fall within the same precedence group, which is lower than the arithmetic and unary operators. The associativity of these operators is left to right. Closely associated with the above mentioned relational operators are the following two equality operators: Operator Meaning == equal to != not equal to The equality operators fall into a separate precedence group beneath the relational operators. These operators also have a left-to-right associativity. These six operators are used to form logical expressions, which represent conditions that are either true or false. The resulting expressions will be of type integer, since true is represented in C++ by the integer value 1 and false by the value 0. Example: Suppose that a, b...

Increment and Decrement Operators in C++

These operators also fall under the broad category of unary operators but are quite distinct than unary minus. The increment and decrement operators are very useful in C++ language. They are extensively used in for and while loops. The syntax of these operators is given below: ++<variable name> <variable name>++ –<variable name> <variable name>– The operator ++ adds 1 to the operand and — subtracts 1 from the operand. These operators manifest in two forms: prefix and postfix. For example, the ++ operator can be used in two ways: ++m and m++ These are two different expressions. The first expression immediately increments the value of m by 1. For example, the statements: int t, m = 1; t = ++m; will cause the value of m to incremented first, and then this value is assigned to t, resulting in both having the same value. On the other hand, the statements:...