Your browser doesn't support JavaScript Expressions Statements and Operators - Windows Programming

Expressions Statements and Operators

All C programs are made up of statements, which are commands given to the computer executed in sequence.  These statements are usually written one per line, although this is not a coding requirement. C statements always end with a semicolon except for preprocessor directives.

Whitespace

Whitespace refers to characters that are used for formatting purposes. In C, this refers primarily to spaces, tabs, and newlines. The C compiler generally ignores whitespace, with a few minor exceptions. Whitespace serves the purpose of making the code more readable to programmers.

Null Statements

A null statement is a semicolon on a line that performs no action. 

Compound Statements

A compound statement or block comprises one or more statements enclosed within a bracket.{}

Although every statement in a compound statement must end with a semicolon, the compound statement itself does not end with a semicolon. Variables declared within the statement are local to that compound statement.

Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation.  C operators fall into several categories: assignment operators, mathematical operators, relational operators, and logical operators.

Assignment Operator – is used to assign a value to a variable. The Assignment Operator is denoted by equal to sign. An assignment Operator is a binary operator which operates on two operands. The following table lists the assignment operators supported by the C language –

Data typeNotes
charCan store -127 to 127 or 0 to 255. 8 bits. Usually used for holding ASCII characters.
char16_tUnsigned integer type. Usually 16 bits. Used for UTF-16 character representation
char32_tUnsigned integer type. Usually 32-bit bits. Used for UTF-32 character representation
wchar_tOccupies between 16 or 32 bits depending on the compiler being used. Used for Unicode character representation
signed char8 bits. Can store values between -128 to +127. Used for dealing with binary data
signed short intUsually at least 16 bits. Can store values between –32,768 to 32,767
signed intUsually at least 16 bits. Can store values between –32,768 to 32,767
signed long intUsually at least 32 bits. Can store values between –2,147,483,648 to 2,147,483,647
signed long long intusually at least 64 bits. Can store values between –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned char8 bits. Can store values between 0 to 255
unsigned short intAt least 16 bits. Can store values between 0 to 65,535
unsigned intAt least 16 bits. Can store values between 0 to 65,535
unsigned long intAt least 32 bits. Can store values between –2,147,483,648 to 2,147,483,647
unsigned long long intAt least 64 bits. Can store values between 0 to 4,294,967,295
floatUsually 32 bits. Used for storing single precision floating point values.
doubleUsually 64 bits. Used for storing double precision floating point values
long doubleUsually 96 bites. Used for storing long double-precision floating-point values

The Mathematical Operators – C’s arithmetic operator performs mathematical operations such as addition, subtraction, and multiplication on numerical values

assuming a=2 and b=1

Postfix or Prefix? – In C the increment ++ operator increases the value of a variable by 1 and the use of decrement — operator decreases the value of a variable by 1. The operator placement has an important effect on the way the operations are evaluated

Placing the operator before the operand ie ++var causes the variable to be increased before it returns a value

Placing the operator after the operand ie var+ causes the value of the variable to be returned before it is incremented by 1

Relational Operators – checks if the values of two operands are equal. 

assuming a=2 and b=1

Operator Description Example
== returns true (1) if two operands have the same value otherwise returns false (0) (A == B) is not true.
!= returns true (1) if two operands don’t have the same value otherwise returns false (0) (A != B) is true.
> returns true (1) if the left operand is greater than the value of the right operand otherwise returns false (0) (A > B) is true.
< returns true (1) if the left operand is less than the value of the right operand otherwise returns false (0) (A < B) is false.
>= returns true (1) if the left operand is greater than or equal to the value of the right operand otherwise returns false (0) (A >= B) is true.
<= returns true (1) if the left operand is less than or equal to the value of the right operand otherwise returns false (0) (A <= B) is false.

Logical Operators – applies standard boolean algebra operations to their operands

Assume variable A holds 2 and variable B holds 1, then −

Operator Description Example
&& the logical AND operator evaluates two expressions. If both expressions are true, the logical AND expression is true. A==2 && B==1 result is TRUE
|| The logical OR operator evaluates two expressions. If either is true, the expression is true A==2 || B==1 result is TRUE
! A logical NOT statement reverses a normal expression, returning true if the expression is false and false if the expression is true. !(A==2) result is FALSE

Bitwise Operators – perform manipulations of data at the bit level. These operators also perform the shifting of bits from right to left. Bitwise operators are not applied to float or double.  The difference between logical and bitwise operators is that bitwise operators don’t return a boolean result but manipulate individual bits.

The Bitwise operators supported by the C language are listed in the following table. Assume variable A holds 50 and variable B holds 25, then −

A = 0011 0010

B = 000 11001

OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) will give 16 which is 0001 0000
|Binary OR Operator copies a bit if it exists in either operand.(A | B) will give 59 which is 0011 1011
^Binary XOR Operator copies the bit if it is set in one operand but not both.“>(A ^ B) will give 43 which is 00010 1011
~is a unary operator and has the effect of ‘flipping’ the bits.(~A ) will give -51 which is 1111111111001101 in 2’s complement form.
<<Binary Left Shift Operator. The left operand value is moved left by the number of bits specified by the right operand.A << 1 will give 100 which is 0110 0100
>>Binary Right Shift Operator. The left operand value is moved right by the number of bits specified by the right operand.A >> 2 will give 12 which is 0000 1100

Misc Operators

? or ternary operator – Can be used to replace if-else statements

example:y = (x > 10) ? 10 : 20;

Here, y is assigned the value of 10 if x is greater than 10 and 20 if not

. ->(dot and arrow) —- used to reference individual structures and unions.

&(ampersand) —- returns the address of a variable. For example, &x; returns the location of the variable x in memory

*(asterisk) —- Used to Declare and Dereference a pointer.

Operator Precedence in C++

Operator precedence is the order in which operators are evaluated in a compound expression. In C++, when the compiler encounters an expression, it must similarly analyse and determine how it should be evaluated. To assist with this, all operators are assigned a level of precedence. Those with the highest precedence are evaluated first.

Here, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

for example

int myNumber = 20 * 50 + 25 – 25 * 25 << 2;

simplifies to 1000+25-625>>2

simplifies to 400>>2=100

RankNameOperator
1Scope resolution::
2Member selection, subscripting, increment, and decrement. ->
2Member selection, subscripting, increment, and decrement. ->()++ —
3sizeof, prefix increment and decrement, complement, and, not, unary minus and plus, address-of and dereference, new, new[], delete, delete[], casting, sizeof()++ –^ !- +& *()
4Member selection for a pointer.* ->*
5Multiply, divide, modulo* / %
6Add, subtract+ –
7Shift (shift left, shift right)<< = >>=
8Inequality relational== !=
9Equality, inequality== !=
10Bitwise AND&
11Bitwise exclusive OR^
12Bitwise OR |Rank Name Operator|
13Logical AND &&&&
14Logical OR||
15Conditional?:
16Assignment operators= *= /= %= += -= <<= >>= &= |= ^=
!7Comma