Operators & Arrays in C
Tags: programming, operators, arrays, coding
Published: 2021-05-07 13:56:08

Operators are the foundation of any programming language. Thus the functionality of the C language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In C, operators in Can be categorized in the following categories:

    Arithmetic Operators (+, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement)
    Relational Operators (==, !=, >, <, >= & <=) Logical Operators (&&, || and !)
    Bitwise Operators (&, |, ^, ~, >> and <<) Assignment Operators (=, +=, -=, *=, etc.)
    Other Operators (conditional, comma, size of, address, redirection)
Logical Operators:

They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under consideration. They are described below:

    Logical AND operator: The && operator returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).

    Logical OR operator: The || operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.

    Logical NOT operator: The ! operator returns true the condition in consideration is not satisfied. Otherwise, it returns false. For example, !a returns true if a is false, i.e. when a=0.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlnixo1r91zvpds88p5a.png)

Bitwise Operators:

    The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

    The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

    The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

    The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

    The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

    The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqe7n83gu34v99va4873.png)

Assignment Operators:

Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data-type as the variable on the left side otherwise the compiler will raise an error.

Different types of assignment operators are shown below:

    =: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.

    +=: This operator is a combination of + and = operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

    -=: This operator is a combination of - and = operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.

    *=: This operator is a combination of * and = operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.

    /=: This operator is a combination of / and = operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldm8wym9la8o4t9pbqfe.png)

Relational operators

Relational operators are used to compare two values in order to understand the relationship of a number pair (e.g., less than, greater than, equal to, etc.) Let’s see them one by one:

    Equal to operator: Represented as ==, the equal to operator checks if the two given operands are equal. If they are, it returns true. Otherwise, it returns false. For example, 5==5 will return true.

    Not equal to operator: Represented as !=, the not equal to operator checks if the two given operands are equal. If they are not, it returns true. Otherwise, it returns false. It is the exact Boolean complement of the == operator. For example, 5!=5 will return false.

    Greater than operator: Represented as >, the greater than operator checks if the first operand is greater than the second operand. If so, it returns true. Otherwise, it returns false. For example, 6>5 will return true.

    Less than operator: Represented as <, the less-than operator checks if the first operand is lessthan the second operand. If so, it returns true. Otherwise, it returns false. For example, 6<5 will return false.

    Greater than or equal to the operator: Represented as >=, the greater than or equal to operator checks if the first operand is greater than or equal to the second operand. If it is, it returns true; else, it returns false. For example, 5>=5 will return true.

    Less than or equal to operator: Represented as <=, the less than or equal to operator checks if the first operand is less than or equal to the second operand. If it is, it returns true else false. For example, 5<=5 will also return true.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aby400leugcmtqcn917o.png)

Arrays:

An array is a collection of data items, all of the same type, accessed using a common name.
A one-dimensional array is like a list;  A two dimensional array is like a table;  The C language places no limits on the number of dimensions in an array, though specific implementations may.
Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Declaring Arrays:

Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.
Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.
Dimensions used when declaring arrays in C must be positive integral constants or constant expressions. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9cgzva2qgbx1a674wk3.png)

This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double, use this statement 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdz1elqr94jl9eudmjme.png)

Initializing Arrays

Arrays may be initialized when they are declared, just as any other variables.
Place the initialization data in curly {} braces following the equals sign.  Note the use of commas in the examples below.
An array may be partially initialized, by providing fewer data items than the size of the array.  The remaining array elements will be automatically initialized to zero.
If an array is to be completely initialized, the dimension of the array is not required.  The compiler will automatically size the array to fit the initialized data.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76rzvy3ahr9jdxkhc90b.png)

SAMPLE 1-D Array Program:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cz9sr5z59wicqaue0bss.png)

Multidimensional Arrays:

Multi-dimensional arrays are declared by providing more than one set of square [ ] brackets after the variable name in the declaration statement.

One dimensional arrays do not require the dimension to be given if the array is to be completely initialized.  By analogy, multi-dimensional arrays do not require the first dimension to be given if the array is to be completely initialized.  All dimensions after the first must be given in any case.
For two dimensional arrays, the first dimension is commonly considered to be the number of rows, and the second dimension the number of  columns.  We will use this convention when discussing two dimensional arrays.
Two dimensional arrays are considered by C/C++ to be an array of ( single dimensional arrays ).  For example, "int numbers[ 5 ][ 6 ]"  would refer to a single dimensional array of 5 elements, wherein each element is a single dimensional array of 6 integers.  By extension, "int numbers[ 12 ][ 5 ][ 6 ]" would refer to an array of twelve elements, each of which is a two dimensional array, and so on.
Another way of looking at this is that C stores two dimensional arrays by rows, with all elements of a row being stored together as a single unit.  Knowing this can sometimes lead to more efficient programs. 
Multidimensional arrays may be completely initialized by listing all data elements within a single pair of curly {} braces, as with single dimensional arrays.
It is better programming practice to enclose each row within a separate subset of curly {} braces, to make the program more readable.  This is required if any row other than the last is to be partially initialized.  When subsets of braces are used, the last item within braces is not followed by a comma, but the subsets are themselves separated by commas.
Multidimensional arrays may be partially initialized by not providing complete initialization data.  Individual rows of a multidimensional array may be partially initialized, provided that subset braces are used.
Individual data items in a multidimensional array are accessed by fully qualifying an array element.  Alternatively, a smaller dimensional array may be accessed by partially qualifying the array name.  For example, if  "data" has been declared as a three dimensional array of floats, then data[ 1 ][ 2 ][ 5 ] would refer to a float, data[ 1 ][ 2 ] would refer to a one-dimensional array of floats, and data[ 1 ] would refer to a two-dimensional array of floats.  The reasons for this and the incentive to do this relate to memory-management issues that are beyond the scope of these notes.

SAMPLE 2-D Array Program:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo62ts8s6sf1xvvgf1pu.png)

Advantages of Arrays:

Arrays represent multiple data items of the same type using a single name.
In arrays, the elements can be accessed randomly by using the index number.
Arrays allocate memory in contiguous memory locations for all its elements. Hence there is no chance of extra memory being allocated in case of arrays. This avoids memory overflow or shortage of memory in arrays.

Applications:
Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.