Basics of Computer Language/c++
Basics of Computer Language/c++
Operators are the foundation of any programming language. Thus the functionality of
C/C++ programming language is incomplete without the use of operators. We can
define operators as symbols that help us to perform specific mathematical and logical
computations on operands. In other words we can say that an operator operates the
operands. For example, consider the below statement:
c = a + b;
Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The
addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
1
Presented by: Irfan Haider
2. Relational Operators: Relational operators are used for comparison of the values of two
operands. For example: checking if one operand is equal to the other operand or not, an
operand is greater than the other operand or not etc. Some of the relational operators are (==,
>= , <= ).
‘==’ operator checks whether the two given operands are equal or not. If so, it
returns true. Otherwise it returns false. For example, 5==5 will return true.
‘!=’ operator checks whether the two given operands are equal or not. If not, it
returns true. Otherwise it returns false. It is the exact boolean complement of
the ‘==’ operator. For example, 5!=5 will return false.
‘>’ operator checks whether 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.
‘<‘ operator checks whether the first operand is lesser than the second operand. If
so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
‘>=’ operator checks whether the first operand is greater than or equal to the
second operand. If so, it returns true. Otherwise it returns false. For
example, 5>=5 will return true.
‘<=’ operator checks whether the first operand is lesser than or equal to the
second operand. If so, it returns true. Otherwise it returns false. For
example, 5<=5 will also return true.
2
Presented by: Irfan Haider
4. Bitwise Operators: The Bitwise operators is used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then calculation is performed on the
operands. The mathematical operations such as addition , subtraction , multiplication etc. can
be performed at bit-level for faster processing.
& (bitwise AND) 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.
| (bitwise OR) 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.
^ (bitwise XOR) 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.
<< (left shift) Takes two numbers, left shifts the bits of the first operand, the
second operand decides the number of places to shift.
>> (right shift) Takes two numbers, right shifts the bits of the first operand, the
second operand decides the number of places to shift.
~ (bitwise NOT) Takes one number and inverts all bits of it
Following is an example C program.
/* C Program to demonstrate use of bitwise operators */
#include<stdio.h>
int main()
{
unsigned char a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); // The result is 00000001
printf("a|b = %d\n", a|b); // The result is 00001101
printf("a^b = %d\n", a^b); // The result is 00001100
printf("~a = %d\n", a = ~a); // The result is 11111010
printf("b<<1 = %d\n", b<<1); // The result is 00010010
printf("b>>1 = %d\n", b>>1); // The result is 00000100
return 0;
}
Output:
a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
3
Presented by: Irfan Haider
2) The bitwise XOR operator is the most useful operator from technical interview
perspective. It is used in many problems. A simple example could be “Given a set of
numbers where all elements occur even number of times except one number, find the
odd occurring number” This problem can be efficiently solved by just doing XOR of all
numbers.
int main(void) {
int arr[] = {12, 12, 14, 90, 14, 14, 14};
int n = sizeof(arr)/sizeof(arr[0]);
printf ("The odd occurring element is %d ", findOdd(arr, n));
return 0;
}
// Output: The odd occurring element is 90
int main()
{
int x = 2, y = 5;
(x & y)? printf("True ") : printf("False ");
(x && y)? printf("True ") : printf("False ");
return 0;
}
// Output: False True
4) The left-shift and right-shift operators are equivalent to multiplication and division by 2
respectively.
As mentioned in point 1, it works only if numbers are positive.
int main()
{
int x = 19;
printf ("x << 1 = %d\n", x << 1);
printf ("x >> 1 = %d\n", x >> 1);
return 0;
}
// Output: 38 9
4
Presented by: Irfan Haider
5) The & operator can be used to quickly check if a number is odd or even
The value of expression (x & 1) would be non-zero only if x is odd, otherwise the value would be
zero.
int main()
{
int x = 19;
(x & 1)? printf("Odd"): printf("Even");
return 0;
}
// Output: Odd
b = 20;
ch = 'y';
“+=”:This operator is combination of ‘+’ and ‘=’ operators. This operator first adds
the current value of the variable on left to the value on right and then assigns the
result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
5
Presented by: Irfan Haider
“-=”:This operator is combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the value on right from the current value of the variable on left and then
assigns the result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
“*=”:This operator is combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on right and then
assigns the result to the variable on the left.
Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 5. Then (a *= 6) = 30.
“/=”:This operator is combination of ‘/’ and ‘=’ operators. This operator first divides
the current value of the variable on left by the value on right and then assigns the
result to the variable on the left.
Example:
(a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.
Data Types in C
Each variable in C has an associated data type. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
Let us briefly describe them one by one:
Following are the examples of some very common data types used in C:
char: The most basic data type in C. It stores a single character and requires a single byte
of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single
precision.
double: It is used to store decimal numbers (numbers with floating point value) with
double precision.
Different data types also have different ranges upto which they can store numbers.
These ranges may vary from compiler to compiler. Below is list of ranges along with the
memory requirement and format specifiers on 32 bit gcc compiler.
6
Presented by: Irfan Haider
DATA TYPE MEMORY (BYTES) RANGE FORMAT SPECIFIER
float 4 %f
double 8 %lf
7
Presented by: Irfan Haider
#include <stdio.h>
int main()
{
int a = 1;
char b ='G';
double c = 3.14;
printf("Hello World!\n");
return 0;
}
Output:
Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4 bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my
size i
s 8 bytes.
Bye! See you soon. :)
8
Presented by: Irfan Haider
Integer literal in C/C++ (Prefixes and Suffixes)
Integer literal is a type of literal for an integer whose value is directly represented in
source code. For example, in the assignment statement x = 1, the string 1 is an integer
literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an
integer literal indicating the value 16(in decimal), which is represented by 10 in
hexadecimal (indicated by the 0x prefix).
Further, in x = “1” the “1” is a string literal(not a character or an integer literal), because
it is in quotes. The value of the string is 1, which happens to be an integer string.
Integer literals are expressed in two types i.e.,
1. Prefixes which indicates the base. For example, 0x10 indicates the value 16 in
hexadecimal having prefix 0x.
2. Suffixes which indicates the type. For example, 12345678901234LL indicates the
value 12345678901234 as an long long integer having suffix LL.
int main()
{
// PREFIXES
cout << 213 << '\n' // decimal integer literal
<< 0213 << '\n' // Octal integer literal
<< 0x213A << '\n' // hexadecimal integer literal
<< 0b101 << '\n' // binary integer literal
// SUFFIXES
9
Presented by: Irfan Haider
// long long literal
<< 1234567890123456789LL << '\n'
return 0;
}
Output:
213
139
8506
5
1234567890123456789
12345678901234567890
12345678901234567890
1221300
Best of Luck!
Presented by
Irfan Haider
BS (6th Semester)
Department of Physics, AIOU Islamabad
10
Presented by: Irfan Haider