You are on page 1of 53

Presented by: P3

Dr. Rakesh Rathi


Assistant Professor & Head
Department of Computer Science and IT
Govt. Engineering College, Ajmer
 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.

 Here, the compiler does two things:


 Firstly the compiler allots 2 bytes of memory because int takes 2 bytes of memory.
 Secondly, the compiler names that memory block as num.

 Then, we can store an integer number into this variable.


num= 15; /* Store 15 into num */

 We can write other statements as:


float salary; /* Here float is datatype and salary is variable */
char gender; /* Here char is datatype and gender is variable */
double x, y; /* Here x and y variables are declared as double type */
 These statements are called declarative statements, Since they are declaring the datatype of variables.

 Once the variables are declared, we can start store data into those variables.

Data Types and Operators by Dr. Rakesh Rathi 2


 For example:
x= 15; /* Here 15 is stored in the variable x */
salary= 4500.75; /* Here 4500.75 is stored into salary variable */
 Here, int is the datatype and num is the variable.
gender= ‘M’; /* Here M is stored into gender variable */
 These statements are called initialization statements, Since they store starting data into the 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 */

Data Types and Operators by Dr. Rakesh Rathi 3


 A datatype represents the type of data stored into a variable.

 There are two type of datatypes in C:


 Primary datatypes
 Secondary datatypes

C Datatypes

Primary Datatypes Secondary 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.

Data Types and Operators by Dr. Rakesh Rathi 4


 These datatypes represent single values, it means they store only one value.

 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 */

Data Types and Operators by Dr. Rakesh Rathi 5


 double: This datatype represents a big float number. It takes 8 bytes of memory.

double speed; /* declare speed as double type variable */


speed= 3.1e8; /* store 3.1x10 power 8 into speed */
 Here, e represents X10 power, thus to write 3.45x106, we can simply write 3.45e6. Writing numbers using
‘e’ like this is called ‘scientific notation’. ‘e’ stands for exponentiation.
 void: void is a datatype but it is not used with a variable to store data. So, it is called ‘empty datatype’. Void
datatype is generally used before function to specify that the function does not return any value.
void myfunction()
(
statements;
)

 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
 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 */

Data Types and Operators by Dr. Rakesh Rathi 7


 void datatype can be used in declaration of pointers.
 A pointer is a variable which stores the memory address of another variable.
void *p; /* declare void type pointer */
p= &x; /* store memory address of the variables x into p */
 A void type pointer can be converted into any other type of pointer.

 For example: we can convert void type pointer into int type pointer or float type pointer.

Data Types and Operators by Dr. Rakesh Rathi 8


 The storage space allotted to a datatype can be modified using ‘Modifiers’

 There are 4 modifiers: short, long, signed and unsigned.

short and long:


 short modifier represents that the memory should be short.

 long modifier indicates that the memory should be double

 By default, when we write int or short int both mean the same, in terms of memory.

signed and unsigned:


 signed represents that the variable’s value can be +ve or –ve.

 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.

 If we write: unsigned int, then we can store only positive values.

 Thus it is possible to declare int as: int or signed int or unsigned int.

Data Types and Operators by Dr. Rakesh Rathi 9


Datatype Range Memory in Bytes Format used in printf()
int (or short signed int) -32768 to +32767 2 %d or %i
short unsigned int 0 to 65535 2 %u
long int (long signed int) -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to +3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf
char (signed char) -128 to +127 1 %c
unsigned char 0 to 255 1 %c
string %s

Datatypes and their memory usage

Data Types and Operators by Dr. Rakesh Rathi 10


Constant Description
CHAR_BIT Number of bits in a char

CHAR_MAX Maximum decimal integer value of a char

CHAR_MIN Minimum decimal integer value of a char

INT_MAX Maximum decimal value of an int


 It is possible to find out the maximum INT_MIN Minimum decimal value of an int
and minimum values that a variable can
hold, with the help of constants defined LONG_MAX Maximum decimal value of a long
in <limits.h> header file. LONG_MIN Minimum decimal value of a long

SCHAR_MAX Maximum decimal integer value of a signed char

SCHAR_MIN Minimum decimal integer value of a signed char

SHRT_MAX Maximum decimal value of a short

SHRT_MIN Minimum decimal value of a short

UCHAR_MAX Maximum decimal integer value of a unsigned char

UINT_MAX Maximum decimal value of an unsigned integer

ULONG_MAX Maximum decimal value of an unsigned long int

USHRT_MAX Maximum decimal value of an unsigned short int


Data Types and Operators by Dr. Rakesh Rathi 11
Program 1:
Code to know the maximum and minimum values that can be stored into an int type variable.
#include<stdio.h>
#include<conio.h>
#include<limits.h>
void main()
{ Output
printf(“\n Maximum Value stored in int type variable: %+d”, INT_MAX);
printf(“\n Minimum Value stored in int type variable: %d”, INT_MIN);
getch();
}

Fundamental Concepts in C by Dr. Rakesh Rathi 12


 Qualifiers are two words which provide additional information about the variable.
 There are two qualifiers: const and volatile.

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:

const float pi = 3.14159;


 Here, pi is a variable whose value cannot be changed. This means pi is treated as constant.

Data Types and Operators by Dr. Rakesh Rathi 13


Program 2:
To find the area of a circle where the constant pi is used.
#include<stdio.h> getch();
#include<conio.h> }
void main()
{
/* declare the constant pi value */
const float pi=3.14159;
float radius, area;
printf(“\n Enter the Radius of Circle:”);
scanf(“%f”, &radius);
area=pi*radius*radius;
Output
printf(“\n\n\n Area of circle:%f”, area);

Fundamental Concepts in C by Dr. Rakesh Rathi 14


 Another way to define constants is with the ‘#define’ preprocessor which has the advantage that it does not
use any memory.
Program 3: scanf(“%f”, &radius);
Write a C program to know how to use the #define statement to declare area=pi*radius*radius;
constant value.
printf(“\n\n\n Area of circle:%f”, area);
#include<stdio.h>
getch();
#include<conio.h>
}
#define pi 3.14159
void main()
{
float radius, area;
printf(“\n Enter the Radius of Circle:”); Output
 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
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 */

Data Types and Operators by Dr. Rakesh Rathi 16


 To display useful results and reports, we need to perform operations on data. This is achieved by
operators.
 We can define an operator as a symbol that can perform some operation. An operator acts on variables,
called operands.
 For example:
 To perform addition, we can write, a + b. Here + is called operator. a and b are called operands.
 If an operator acts on a single variable, then it is called ‘unary operator’.
 If an operator acts on two variable, then it is called ‘binary operator’.
 If an operator acts on three variables, then it is called ‘ternary operator’.
Operator Category Operator symbols
Arithmetic Operators +, -, *, /, %

Assignment Operators =, +=, -=, *=, /=, %=

Unary Operators -, ++, --


List of operators
Ternary Operators ?: available in C.
Relational Operators <, <=, >, >=, ==, !=

Logical Operators &&, ||, !

Bitwise Operators -, &, |, ^, <<, >>


Data Types and Operators by Dr. Rakesh Rathi
Miscellaneous Operators sizeof(), &, *, , 17
Arithmetic operators:
Example
Operator Meaning Result
If a= 13, b=5
+ Addition a+b 18
- Subtraction a-b 8
* Multiplication a*b 65
/ Division a/b 2.6
% Modulus operator (gives a%b 3
remainder of integer division)

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 x= 15; /* the value 15 is stored into x */


int x= y; /* store y value into x */
int x= y+z-4; /* evaluate the right side expression & store its value into x */
int x= myfunction(); /* the returned value from myfunction() is stored into x */

Data Types and Operators by Dr. Rakesh Rathi 18


 From the preceding examples, we can understand that always the right hand side value is stored into the
left side variable.
 We cannot write something like this:

15= x; /* invalid, since variable is not present at left side */


 While using the assignment operator (=), we can write expressions in a compact form also.
sal= sal + 500 can be written as sal+=500
num= num – 100 can be written as num-=100
x= x * k can be written as x*=k
p= p / 10 can be written as p/=10
i= i % 2 can be written as i%=2
 In the preceding examples, the expression, sal= sal + 500 is called ‘expanded form’ or full form.
 The right side expression, sal+=500 is called ‘compact form’ or shortcut form.
 Both the forms are equal and valid in C language.
 The symbol += is called addition assignment operator, -= is called subtraction assignment operator, *= is
called multiplication assignment operator, /= is called division assignment operator, %= is called modulus
assignment operator.

Data Types and Operators by Dr. Rakesh Rathi 19


Unary operators:
 These operators act on only one variable. There are 3 unary operators:
 Unary minus operator ( - )
 Increment operator ( ++ )
 Decrement operator ( -- )
Unary minus operator ( - ):
 This operator is useful to negate a value. Negation means converting a positive value into negative value
and vise versa.
int x= 5;
x= -x; /* negate the value of x and store into x */
 Since x value is 5, -x value will be -5. This value will be stored into left side variable x. So, after executing
the above statements, x value will become -5.
Increment operator ( ++ ):
 This operator is useful to increase the value of a variable by 1.
 For example:
 If the variable contains 5, its value will become 6.

int n= 5; /* value of n is 5 */
++n; /* now n value becomes 6 */
Data Types and Operators by Dr. Rakesh Rathi 20
 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.

Data Types and Operators by Dr. Rakesh Rathi 21


Program 4: /* post incrementation */
Write a C program to understand the difference between printf(“\n\n Value of X during Post increment:%d”,
pre and post increment. x++);
#include<stdio.h> printf(“\n\n Value of X after Post increment:%d”, x);
#include<conio.h> printf(“\n\n New Updated Value of X:%d”, x);
void main() getch();
{ }
int x=5;
printf(“\n Actual Value of X:%d”, x);
/* pre incrementation */
printf(“\n\n Value of X during Pre increment:%d”, ++x);
printf(“\n\n Value of X after Pre increment:%d”, x);
printf(“\n\n Updated Value of X:%d”, x);
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 22


Decrement operator ( -- ):
 This operator decreases the value of a variable by 1.
 For example:
 If the variable contains 5, its value will become 4.
int n= 5; /* value of n is 5 */
--n; /* now n value becomes 4 */
 We can write – symbol either before or after the variable.
 For example:
n--; /* in this case also, n value will be decremented by 1 */
 Writing – before a variable is called ‘pre decrementation’ and writing – after a variable is called ‘post
decrementation’.
 In pre decrementation, the value of the variable is decremented first. Any other operation is done next.
 In post decrementation, all other operations are done first and decrementation is done at the end.

Activity to do: YOURSELF


Write a C program to understand the difference between pre and post decrement.

Data Types and Operators by Dr. Rakesh Rathi 23


Ternary operator ( ? : ):
 This operator is called ‘Ternary operator’ because it acts on 3 expressions. It is also called ‘Conditional
operator’, since it represents a conditional statement.
 The syntax of this operator:
variable = expression1 ? expression2 : expresssion3;
 If expression1 is true, then the value of expression2 will be evaluated and stored into the variable.
 If expression1 is false, then the value of expression3 will be stored into the variable.
if(expression)
variable = expression2;
else variable = expression3;
 The preceding statement is called ‘Conditional statement’, because it performs a task depending on
whether the condition is true or false.
max = (num1 > num2) ? num1 : num2;
 In the preceding example, if num1 is really greater than num2, then num1 value is stored into max,
otherwise num2 value is stored into max.
if(num1>num2) /* The same expression in C language is written like this */
max = num1;
else max = num2;
Data Types and Operators by Dr. Rakesh Rathi 24
Relational operators:
 This operator are useful to compare two quantities, to know whether a quantity is bigger or smaller than
another one or not.
 The following are relational operations:
Relational Operator Meaning
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equals
!= Not equal
 Relational operators are generally useful to create conditions.
if(x >=y) printf(“%d”, x);
else printf(“%d”, y);
 If the condition: x value greater than or equal to y value becomes true, then x value will be displayed,
otherwise y value will be displayed.
Data Types and Operators by Dr. Rakesh Rathi 25
if(a+b = = 100) a = x;
 If a+b value equals to 100, then only x value will be assigned to a.
Logical operators:
 These operators join two or more conditions. When we join two or more simple conditions, it becomes a
compound condition.
 There are 3 logical operators:
Operator Meaning
&& and operator
|| or operator
! not operator
if (a = = 1 && b = = 2) printf(“%d\t%d”, a,b);
 Here, && is used to combine two conditions a = = 1 and b = = 2. This means, a value should be 1 and
also b value should be 2. Then only, the next printf() statement will be executed.
int p = 0; if(!p) puts(“\nYes”);
 Initially p value is 0. Then !p value will be 1. In C language, 0 is internally treated as false and any other
number is taken as true. So, the condition after if will be true. Because the condition is true. It will display
‘Yes’.
Data Types and Operators by Dr. Rakesh Rathi 26
Bitwise operators:
 These operators act upon individual bits (0 or 1) of numbers. The following are bitwise operators:
Operator Meaning
~ Bitwise complement
& Bitwise and
| Bitwise or
^ Bitwise xor
<< Bitwise leftshift
>> Bitwise rightshift
 To understand these operators, we should have knowledge of how the decimal numbers are represented
in binary number system.
 Decimal number system uses 10 digits (from 0 to 9) to represent any value.
 In binary number system, there are only 2 digits 0 and 1. Hence all the numbers should be represented by
0 and 1 only.
 Now, let us see how to convert a decimal number into a binary number and vice-versa.

Data Types and Operators by Dr. Rakesh Rathi 27


Problem 1:
Convert 10 from decimal number into binary. The rule is to divide the decimal number successively by 2 and
take the reminders from bottom to top.
Ans:
2 10
2 5–0 Remainder
2 2–1
2 1–0
2 0-1

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.

Data Types and Operators by Dr. Rakesh Rathi 28


Problem 2:
Convert 0000 1010 from binary into decimal. The rule is to multiply each digit by the powers of 2 and add
the products.
Ans:
0 0 0 0 1 0 1 0
27 26 25 24 23 22 21 20
0+ 0+ 0+ 0+ 8+ 0+ 2+ 0 =10.

The sum of the products is coming to 10, the binary number 0000 1010 is equal to the 10 in decimal.

Data Types and Operators by Dr. Rakesh Rathi 29


Problem 3:
Convert the decimal number -11 into binary. The point we should remember is this: negative numbers are
converted into binary using 2s complement notation, Take the number 11 which is represented as 0000
1011. Now convert 0s to 1s and vice versa, we get 1s complement representation. So, we get 1111 0100.
Ans:
Now adding 1 to this, we get 2s complement form. Thus we get 1111 0101. this is the binary equivalent for
-11.

11 in binary = 0000 1011


1s complement = 1111 0100
+1
2s complement = 1111 0101 = -11

So, -11 is represented in binary as: 1111 0101.

Data Types and Operators by Dr. Rakesh Rathi 30


Bitwise complement operator (~):
 This operator returns complement form of a number. Converting 0s into 1s and vice versa is called
complement.
int x = 10;
~x = ?
 First, convert 10 into binary number system. We get 0000 1010.
 To get the complement for this number, we convert 0s into 1s and vice versa. Thus we get 1111 0101.
This is equal to -11 in decimal number system.
 So ~x gives us the result -11.
Bitwise AND operator (&):
 This operator performs ANDing operation on the bits, when two bits are ANDed.
X Y X&Y
0 0 0
0 1 0
1 0 0
1 1 1
Data Types and Operators by Dr. Rakesh Rathi Truth Table for AND operation 31
 Let us take two numbers 10 and 12. Let us find the result of bitwise AND operation of this 10 and 12.
X = 10 = 0000 1010
Y = 12 = 0000 1100
X & Y = 0000 1000 = 8 in decimal
Bitwise OR operator (|):
 This operator performs ORing operation on the bits, when two bits are ORed.
X Y X|Y
0 0 0
0 1 1 Truth Table for OR operation

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
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

Data Types and Operators by Dr. Rakesh Rathi 33


Bitwise leftshift operator (<<):
 This operator shifts the bits of a given number towards left a specified number of times.
int x = 10;
x<<2 = ? /* shift the bits of x towards left 2 times */
 When we shift the bits towards left 2 times, the left most 2 bits will be lost. The right most bits will be filled
with zeros.
 Hence x value 10 becomes 40.
x = 10 = 0000 1010
x<<2 = 0010 1000 = 40 in decimal
Bitwise rightshift operator (>>):
 This operator shifts the bits of given number towards right a specified number of times.
 For example:
 We want to find the value of 10 after shifting it towards right for 2 times.
 When we shift the bits towards right 2 times, the right most 2 bits will be lost.
int x = 10;
x>>2 = ? /* shift the bits of x towards right 2 times */

Data Types and Operators by Dr. Rakesh Rathi 34


 Sign bit is the leftmost bit which represents the sign (+ve or –ve) of a number.
 For positive numbers, the sign bit will be 0 and for negative numbers, the sign bit will be 1.
 Here x value is 10 which is positive.
 So, its sign bit will be a 0. In the output, the left most 2 bits will be filled with the sign bit (i.e., zeros).

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
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

Fundamental Concepts in C by Dr. Rakesh Rathi 36


Miscellaneous operators:
sizeof() operator:
 This operator returns the size of a variable.
int num;
sizeof(num) /* return 2 as the int type variable takes 2 Bytes memory */
 sizeof() can also be used with datatypes to know the size of a datatype.

Data Types and Operators by Dr. Rakesh Rathi 37


Program 6:
Write a program to know how the sizeof() operator works.
#include<stdio.h> getch();
#include<conio.h> }
void main()
{
int a;
long int b;
printf(“\n Size of a:%d”, sizeof(a));
printf(“\n Size of b:%d”, sizeof(b));
printf(“\n Size of int:%d”, sizeof(int));
printf(“\n Size of long int:%d”, sizeof(long));
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 38


& operator or address of operator:
 When a variable is declared, compiler will allot memory for the variable.
int x; /* allot 2 bytes of memory for x */
 The memory address of the variable can be known using & operator. & operator is called ‘address of’
operator.
&x; /* memory address of x */

Data Types and Operators by Dr. Rakesh Rathi 39


Program 7:
Write a program to know how the & operator works.
#include<stdio.h>
#include<conio.h>
void main()
{
int x; Output
printf(“\n Memory address of ‘x’ in Hexadecimal form: %x”, &x);
getch();
}

Fundamental Concepts in C by Dr. Rakesh Rathi 40


* operator or pointer operator:
 This operator is useful to declare a pointer.
 A pointer is a variable that stores memory address.
int *p; /* p is a pointer of int type */
p= &x; /* store memory address of x into p */
 The pointer operator is used in 2 ways:
 To declare a pointer.
 To find the value of the variable which is pointed.

Data Types and Operators by Dr. Rakesh Rathi 41


Program 8:
This program is useful to access the memory address of
a variable and value of a variable through a pointer.
printf(“\n\n Pointer refers to %d”, *p);
#include<stdio.h>
getch();
#include<conio.h>
}
void main()
{
int x=100; /* this is a variable of int type*/
int *p; /* this is a pointer of int type*/
p=&x; /* store address of x in p*/
/* now p represents the address of x */
printf(“\n Memory Address:%x”, p);
/* now *p represents the value of x */
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 42


Comma operator:
 The symbol for this operator is a comma ( , ).
 It is used in 4 ways:
 To separate variables in declaration statements.
int x, y, z; /* observe , operator */
 To perform multiple initializations.
x = 1, y = 1, z =2; /* observe , operator */
 To execute multiple expressions.
int x, y=1, z=2;
x = (y++, z++); /* observe , operator */
printf(“%d %d %d”, x, y, z);
 Let us read from right to left. The right most expression z++ value is stored first into x. Since z++
represents post incrementation, z value is not incremented but stored into x. So, x value will be 2. After
that, y value will be incremented and z value will be incremented. So, y value will be 2 and z value will be
3.
 So, Result: x=2, y=2, z=3.

Data Types and Operators by Dr. Rakesh Rathi 43


 To separate expressions in a for loop or while loop.
int i=1;
while (i<10, i!=5) /* observe , operator */
printf(“\n%d”, i++);
 Every time this while loop is executed, both the expressions inside the simple braces are tested. This loop
executes as long as i value is less than 10 and while i is not equal to 5. It means it starts displaying 1, 2,
3, 4 but stops after 4 because the loop terminates when i value is 5.
 So, Result: 1, 2, 3, 4.
Precedence of operators:
 The sequence of execution of operators is called Precedence of operators.
 Table below represents the precedence of operators.
 Operators with the highest precedence appear at the top of the table and those with the lowest appear at
the bottom.
 Associativity represents the direction of operation whether from left to right or vice versa.

Data Types and Operators by Dr. Rakesh Rathi 44


Category Operator Associativity
Braces () [] Left to Right
Unary + - ! ~ ++ -- * & sizeof() Right to Left
Multiplicative * / % Left to Right
Additive +- Left to Right
Shift << >> Left to Right
Relational < <= > >= Left to Right
Equality == != Left to Right
Bitwise AND & Left to Right
Bitwise XOR ^ Left to Right
Bitwise OR | Left to Right
Logical AND && Left to Right
Logical OR || Left to Right
Conditional ?: Right to Left
Assignment = += -= *= /= %= > >= < <= Right to Left
Data Types and Operators by Dr. Rakesh Rathi 45
 Now the expression to see how expressions are evaluated according to the precedence of operators.
k=3/2*4+3/8-3 /* first * and / will be done */
k=1*4+0–3
k=4+0-3 /* next +, - will be evaluated */
k=4–3
k=1 /* lastly = will be done */

 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.

Data Types and Operators by Dr. Rakesh Rathi 46


Program 9:
Program to know that the result of division of two integers will be an integer numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x=3, y=2; Output
float z=x/y;
printf(“\n Result of Division: %f”, z);
getch();
}

Fundamental Concepts in C by Dr. Rakesh Rathi 47


 Now to get actual value in the division of integer numbers, we should convert either one of the integer
number into float so that a float number when divided by an int number, will get correct float value.
Type Casting:
 Converting a datatype into another datatype is called Type casting or simply casting.
 We should write the datatype in simple braces before the variable to convert its type. Writing the datatype
in the simple braces is called Cast operator.
Program 10: float z=(float)x/y; /* type cast int into float*/
Dividing two integer numbers where type casting is done. printf(“\n Result of Division:%f”, z);
#include<stdio.h> getch();
#include<conio.h> }
void main()
{
int x=3, y=2;
Output
 It is also possible to control the number of digits being displayed in the output. This is called formatting the
numeric output.
Data Types and Operators by Dr. Rakesh Rathi 48
Q1. What are declaration statements?
Ans. The statements which declare the datatype of variables are called ‘declaration statements’. Declaration statements
specify which type of data can be stored into the variables.
Q2. What are initialization statements?
Ans. The statements which are useful to store starting data or values into a variable are called initialization statements.
Q3. What is an lvalue?
Ans. ‘lvalue’ is an expression to which a value can be assigned. It is located at the left side of an assignment statements.
‘rvalue’ represents an expression located at the right side of an assignment statements. ‘lvalue’ should be a variable, not
a constant.
Ex. x= y+z-15; /* Here x is lvalue and y+z-15 is rvalue */
Here, the rvalue ‘y+z-15’ is evaluated and its value is stored into lvalue ‘x’.
Q4. What is the difference between float and double?
Ans. float datatype can represents up to 7 digits accurately after decimal point, double can represents up to 15 digits
accurately after decimal point.
Q5. What is void?
Ans. void in C is used in 3 different ways:

Data Types and Operators by Dr. Rakesh Rathi 49


1. When void is used before a function name, it represents that the function does not return any value.
2. When used in the parameters of a function, the function will not accept any value.
3. When used to declare a pointer, it represents that the pointer can be converted into any other type of pointer.

Q6. What is the difference between myfunction() and myfunction(void)?


Ans. When a function is written without void in the braces, the compiler does not check whether the function is called
with or without passing a value. When a function is written with void, then the compiler strictly checks whether it is called
without a value or not. When it is called with a value, the compiler shows error.
Q7. What is volatile?
Ans. A volatile variable is a variable whose value can be changed at any time by an external process. So, each time a
volatile variable is used in a program, the compiler should check its value for any changes.
Q8. How true or false is represented in C language?
Ans. false is represented by 0 and true is represented by any other number.
Q9. What is the main advantage of bitwise operators?
Ans. The main advantage of bitwise operators is that they improve the speed of execution of a program by directly acting
on the bits internally.

Data Types and Operators by Dr. Rakesh Rathi 50


Q10. What is type casting?
Ans. Converting a datatype into another datatype is called “Type casting” or “Casting”. In type casting, we use a cast
operator where we write the datatype in simple braces.
Q11. How much memory integer type variable takes in C language?
Ans. Memory allocation for integer type variable depends on the type of compiler we are using, as:
 On 16bits compiler 2bytes of memory.
 On 32bits compiler 4bytes of memory.
 On 64bits compiler 8bytes of memory.
 On linux operating system by default complier 4bytes of memory.
You can check and conform How much memory size our compiler is allocating to integer type variable by using sizeof().

Data Types and Operators by Dr. Rakesh Rathi 51


Data Types and Operators by Dr. Rakesh Rathi 52
Data Types and Operators by Dr. Rakesh Rathi 53

You might also like