P3 Data Types and Operators
P3 Data Types and Operators
Operators
Presented by: P3
Dr. Rakesh Rathi
Assistant Professor & Head
Department of Computer Science and IT
Govt. Engineering College, Ajmer
Introduction
▪ A ‘datatype’ is a keyword that represents the type of data being stored into a variable (or memory).
int num;
▪ Here, int is the datatype and num is the variable.
▪ Once the variables are declared, we can start store data into those variables.
▪ The symbol ‘=‘ is called assignment operator, which stores the right hand side value into the left hand side
variable.
▪ We can combine the declaration and initialization statements into a single statements.
int x; /* declaration statement */
x= 100; /* initialization statement */
int x= 100; /* declaration and initialization statement */
C Datatypes
char Array
int Pointer
float Structure
double Union
Datatypes available in C
void Enumeration
▪ Here, we will learn about Primary datatypes only, and we will learn about Secondary datatypes in later
classes.
▪ char: This datatype represents only one character, like A, a, 9, *, ?, etc. char datatype takes 1 byte of
memory.
char ch; /* declare char type variable ch */
ch = ‘A’; /* store A into ch */
▪ int: This datatype stores an integer number, like 125, -100, 0, 75000, etc. Thus an integer number is a
number without any decimal point. int type takes 2 bytes of memory.
int num; /* declare num variable as int type */
num = 25000; /* store 25000 into num */
▪ float: This datatype represents a number with decimal point like 12.5, 3.14159, -100.5, etc. float takes 4
bytes of memory.
float sal; /* declare sal as float type */
sal = 7570.75; /* store 7570.75 into sal */
▪ Here, myfunction() is the function name and void before it represents that this function will not return any
result.
▪ Also we can use void after the function name to specify that the function does not take any parameters. It
means the function does not take any values from outside.
int myfunction(void)
(
statements;
) Types and Operators by Dr. Rakesh Rathi
Data 6
Primary datatypes
▪ Here, myfunction() returns int type result. Observe the void after the function name in the simple braces.
▪ The compiler checks whether the function is called without passing any values or not.
myfunction(); /* successfully compiles since no value is passed */
myfunction(10); /* compiler shows error, since it knows that function should not be called
with any value */
▪ myfunction(void) is not same as myfunction(). See the following code where void is not written after the
function name.
int myfunction()
(
statements;
)
▪ Here, the compiler does not check whether the function is called with or without values
myfunction(); /* successfully compiles since no value is passed */
myfunction(10); /* successfully compiles as the compiler does not check whether the
function is called with or without value */
▪ For example: we can convert void type pointer into int type pointer or float type pointer.
▪ By default, when we write int or short int both mean the same, in terms of memory.
▪ unsigned represents that the sign is not represented and will be taken as +ve.
▪ By default an int is signed int. So we store +ve numbers and –ve numbers also into int type variable.
▪ Thus it is possible to declare int as: int or signed int or unsigned int.
const:
▪ This qualifier is used to tell the compiler that the variable value cannot be changed.
▪ It means it is constant through out the program.
▪ For example:
▪ The preprocessor statements like #define should be written above main() function.
▪ #define pi 3.1459 represents that the compiler should substitute 3.14159 whenever pi is found in the
program.
Data Types and Operators by Dr. Rakesh Rathi 15
Qualifiers
volatile:
▪‘volatile’ represents that the variable value may change at any time which may not be in the purview of the
compiler.
▪For example:
✔ A variable updated by the system clock or by another program will become volatile variable.
▪Since the volatile variable value can be changed by some external source in an unpredictable way, the
compiler should check its value each time it appears in the program code.
volatile int num; /* Here num is declared as volatile type of variable */
int volatile num; /* In this way also, it can be declared */
Assignment operators:
▪The purpose of assignment operators is to assign or store a value into a variable.
▪Use of assignment operator whose symbol is ‘=‘.
int n= 5; /* value of n is 5 */
++n; /* now n value becomes 6 */
Data Types and Operators by Dr. Rakesh Rathi 20
Operators in C
▪ We can write ++ symbol either before or after the variable.
▪ For example:
n++; /* in this case also, n value will be incremented by 1 */
▪ Writing ++ before a variable is called ‘pre increment’ and writing ++ after a variable is called ‘post
increment’.
▪ In pre increment, the value of the variable is incremented first. Any other operation is done next.
▪ In post increment, all other operations are done first. Increment is done at the end, as last operation.
The original number 10 became 0, we can stop here, Now take the remainders from bottom to top: 1010.
There are 4 bits. We want to represent this number using 8 bits, then we can add 4 more 0s as: 0000 1010.
The sum of the products is coming to 10, the binary number 0000 1010 is equal to the 10 in decimal.
1 0 1
1 1 1
▪ Let us take two numbers 10 and 12. Let us find the result of bitwise OR operation of this 10 and 12.
X = 10 = 0000 1010
Y = 12 = 0000 1100
X|Y = 0000 1110 = 14 in decimal
Data Types and Operators by Dr. Rakesh Rathi 32
Operators in C
Bitwise XOR operator (^):
▪This operator performs XORing operation on the bits, when two bits are XORed.
▪Here XOR represents eXclusive OR operation. The symbol ‘^’ is called cap or caret or circumflex symbol.
X Y X^Y
0 0 0
0 1 1
1 0 1
1 1 0
Truth Table for XOR operation
▪Let us take two numbers 10 and 12. Let us find the result of bitwise XOR operation of this 10 and 12.
X = 10 = 0000 1010
Y = 12 = 0000 1100
X^Y = 0000 0110 = 6 in decimal
x = 10 = 0000 1010
x>>2 = 0000 0010 = 2 in decimal
▪ Another example for value -10.
▪ As the value -10 is a negative number and negative numbers are represented using 2’s complement
notation.
▪ So, num= -10 = 1111 0110.
▪ The leftmost bit is 1 which represents that the number is negative.
num = -10 = 1111 0110
num>>2 = 1111 1101 = -3 in decimal
Note:
▪ Bitwise operators are not used in general purpose. They are mainly used in the programs related to
embedded systems and wireless applications.
▪ Since bitwise operators act on the internal bits directly, they increase the execution speed of a program.
Data Types and Operators by Dr. Rakesh Rathi 35
Operators in C
Program 5: printf(“\n Bitwise AND(x&y):%d”, (x&y));
Write a program to understand the operation of bitwise printf(“\n Bitwise OR(x|y):%d”, (x|y));
operators.
printf(“\n Bitwise XOR(x^y):%d”, (x^y));
#include<stdio.h>
printf(“\n Bitwise leftshift (x<<2):%d”, (x<<2));
#include<conio.h>
printf(“\n Bitwise Rightshift (x>>2):%d”, (x>>2));
void main()
getch();
{
}
int x,y;
/* store 10 and 12 into the variables */
x=10;
y=12;
/* perform bitwise operations */
printf(“\n Bitwise Complement(~x):%d”, (~x));
Output
▪ Here, value of 3/2 and 3/8 is taken as 1 and 0 but, the actual value is 1.5 and 0.375, because 3, 2, 8 are
integer numbers and the result of their division will be again an integer number.