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 requirement. C statements always end with a semicolon with the exception of preprocessor directives.
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.
A null statement is a semicolon on a line by itself that doesn’t perform any action.
A compound statement or block consists of 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.
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 value to a variable. The Assignment Operator is denoted by equal to sign. The Assignment Operator is a binary operator which operates on two operands. The following table lists the assignment operators supported by the C language −
Operator | Description | Example |
= | Assigns values from right side operand to left side operand. | C = A + B will assign the value of A + B to C |
+= | Adds the right operand to the left operand and assigns the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
*= | Multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
%= | Calculates the modulus of two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |
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
Operator | Description | Example |
+ | Adds two operands | A + B will give 3 |
- | Subtracts the second operand from the first | A - B will give 1 |
* | Multiplies both operands | A * B will give 2 |
/ | Divides the numerator by de-numerator | A / B will give 2 |
% | Modulus Operator and the remainder after an integer division | A % B will give 0 |
++ | increases operator value by one | A++ will give 3 |
-- | decreases operator value by one | A-- will give 1 |
Postfix or Prefix? - In C use of increment ++ operator increases the value of a variable by 1 and the decrement -- operator decreases the value of a variable by 1. however where the operator is placed 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
Prefix increment/decrement changes the value before the expression is evaluated.
Postfix increment/decrement changes the value after the expression is evaluated.
Relational Operators - checks if the values of two operands are equal or not.
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 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 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
Operator | Description | Example |
---|---|---|
& | 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 |
? 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.
The order in which operators are evaluated in a compound expression is called operator precedence.n C++, when the compiler encounters an expression, it must similarly analyse the expression 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
Rank | Name | Operator |
---|---|---|
1 | Scope resolution | :: |
2 | Member selection, subscripting, increment, and decrement | . -> |
2 | Member selection, subscripting, increment, and decrement | . ->()++ -- |
3 | sizeof, prefix increment and decrement, complement, and, not, unary minus and plus, address-of and dereference, new, new[], delete, delete[], casting, sizeof() | ++ --^ !- +& *() |
4 | Member selection for pointer | .* ->* |
5 | Multiply, divide, modulo | * / % |
6 | Add, subtract | + - |
7 | Shift (shift left, shift right) | << = >>= |
8 | Inequality relational | == != |
9 | Equality, inequality | == != |
10 | Bitwise AND | & |
11 | Bitwise exclusive OR | ^ |
12 | Bitwise OR |Rank Name Operator | | |
13 | Logical AND && | && |
14 | Logical OR | || |
15 | Conditional | ?: |
16 | Assignment operators | = *= /= %= += -= <<= >>= &= |= ^= |
17 | comma | , |
Last Updates:25 October 2022