Machine Learning in C

Tutorial Introduction to C Language

Getting Started Part Three

Data operators

These operators operate on numerical data types such as char, int, long, short, float, double.

The following is a list of operators on numerical data types:

+
Addition.
// Example:
int a = 1;
int b = 2;
// the value of c is 3.
int c = a + b;
-
Subtraction.
// Example:
int a = 1;
int b = 2;
// the value of c is 1.
int c = b - a;
*
Multiplication.
// Example:
int a = 1;
int b = 2;
// the value of c is 2.
int c = a * b;
/
Division.
// Example:
int a = 4;
int b = 2;
// the value of c is 2.
int c = a / b;
%
Remainder after modular division.
// Example:
int a = 5;
int b = 2;
// the value of c is 1.
int c = a % b;
// the value of d is 2.
int d = a / b;
++
Increment a variable by 1. Good for counting in loops.
// Example:
int a = 5;
// post increment the value of a is now 6.
// this means a is used first and then incremented.
a++;
// pre increment the value of a is now 7.
// this means a is incremented first and then used.
++a;
--
Decrement a variable by 1. Good for counting in loops.
// Example:
int a = 5;
// post decrement the value of a is now 4.
// this means a is used first and then decremented.
a--;
// pre decrement the value of a is now 3.
// this means a is decremented first and then used.
--a;

Note: Unlike other languages you can not use these operators for string data.

Assignment operators

The following is a list of assignment operators:

=
Assign a value to a variable.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value from b, 7.
a = b;
+=
Add a value to a variable.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value from a+b, 12.
a += b;
-=
Subtract a value from a variable.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value from a-b, -2.
a -= b;
*=
Multiply a variable by a number.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value from a*b, 35.
a *= b;
/=
Divide a variable by a number.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value
// from a/b, 0 (integer division).
a /= b;
%=
Set to the remainder after a modular division by a number.
// Example:
int a = 5;
int b = 7;
// the original value assigned to a is now overwritten by the value
// from a%b, 5 (remainder of integer division).
a %= b;

Note: Unlike other languages you can not use these operators for string data.

Boolean operators

These operators are used in statements that make a decisions. A Boolean data type has only two values, true and false. These are often represented as 1 and 0 in C code respectively. The general form of a decision in C is as follows: If an expression evaluates to true then do something, of not then do something else.

The following is a list of boolean operators:

==
Evaluates to true if two objects are equal.
// Example:
// evaluates to true (1)
5 == 5
// evaluates to false (0)
5 == 7
!=
Evaluates to true if two objects are not equal.
// Example:
// evaluates to false (0)
5 != 5
// evaluates to true (1)
5 != 7
>
Evaluates to true if object on LHS is bigger than the object on the RHS.
// Example:
// evaluates to true (1)
6 > 4
// evaluates to false (0)
6 > 9
<
Evaluates to true if object on RHS is bigger than the object on the LHS.
// Example:
// evaluates to false (0)
6 < 4
// evaluates to true (1)
6 < 9
≥
Evaluates to true if object on LHS is bigger or equal than the object on the RHS.
// Example:
// evaluates to true (1)
6 ≥ 4
// evaluates to true (1)
6 ≥ 6
// evaluates to false (0)
6 ≥ 9
≤
Evaluates to true if object on RHS is bigger or equal than the object on the LHS.
// Example:
// evaluates to false (0)
6 ≤ 4
// evaluates to true (1)
6 ≤ 6
// evaluates to true (1)
6 ≤ 9

Logical operators

These operators are used as part of conditional statements such as if and while to form complex Boolean operations. You can think of them as extending the basic Boolean operators above into more complex and useful expressions.

The following is a list of logical operators:

&&
Evaluates to true if both the LHS and RHS expression are true. A boolean AND operation.
// Example:
// evaluates to true (1). Both conditions are true hence evaluates to true.
( (6 < 9) && (6 > 3) )
// evaluates to false (0). The RHS is not true so evaluates to false.
( (6 < 9) && (6 > 15) )
||
Evaluates to true if either or both the LHS and RHS expression are true. A boolean OR operation.
// Example:
// evaluates to true (1). Both conditions are true hence evaluates to true.
( (6 < 9) || (6 > 3) )
// evaluates to true (1). The RHS is not true but the LHS is true so evaluates to true.
( (6 < 9) || (6 > 15) )
// evaluates to false (0). Neither LHS or RHS are true so evaluates to false.
( (6 > 9) || (6 > 15) )
!
Evaluates to true if the expression is false and vice versa. A boolean NOT operation.
// Example:
// evaluates to false (0). 6<9 is true but the '!' inverts it to make it false.
!( 6 < 9 )
// evaluates to true (1). 6>9 is false but the '!' inverts it to make it true.
!( 6 > 9 )

Bitwise operators

Bitwise operators act on individual bits in the data.

The following is a list of bitwise operators:

Note: In the examples below the 0b indicates the number is represented in binary form. The 8 1's and 0's that follow represent the values of the individual bits in the number. The examples shown are all bytes and so there are 8 bits represented.

&
AND bitwise operator.
// Example:
int a = 0b00000001;
int b = 0b00000010;
// c evaluates to 0b00000000
int c = a & b;
//
int d = 0b00000011;
// e evaluates to  0b00000010
int e = b & d;
|
OR bitwise operator.
// Example:
int a = 0b00000001;
int b = 0b00000010;
// c evaluates to 0b00000011
int c = a | b;
//
int d = 0b00000011;
// e evaluates to  0b00000011
int e = b | d;
^
XOR bitwise operator.
// Example:
int a = 0b00000001;
int b = 0b00000010;
// c evaluates to 0b00000011
int c = a ^ b;
//
int d = 0b00000011;
// e evaluates to  0b00000001
int e = b ^ d;
~
Inverse bitwise operator. Also known as 1's compliment.
// Example:
int a = 0b00000001;
// b evaluates to 0b11111110
int b = ~a;
>>
Shift right bitwise operator. Has the same affect as dividing an integer by 2.
// Example:
int a = 0b00001000;
// a evaluates to 0b00000100
a >> 1;
<<
Shift left bitwise operator. Has the same affect as multiplying an integer by 2.
// Example:
int a = 0b00001000;
// a evaluates to 0b00010000
a << 1;

Now that we have covered the basics of the C language we can use the key words and operators to write functions that manipulate data. At the end of the day all programs and applications are a set of functions that manipulate or perform computations on data. In the next section we will start to use functions to write a basic program. We shall also demonstrate all of the operators covered in this article.