Module 5 - Data Types and Variables - CP
Module 5 - Data Types and Variables - CP
• Data Types
• Constants
• Type Conversions
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Examples:
• Name
• Weight
• Quantity
• Price
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Program Data
Program data occurs in the form of
• Variables
• Constants
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Variables
• A name given to a memory location
• The name used to access a variable is also known as an
identifier
• C has strict rules for variable naming
• Variable name can begin with a letter or underscore, and
comprise of letters, digits or underscore. Names are case-
sensitive
• Certain reserved keywords cannot be used as variable names
(continue, if, short, union, return … )
• Use meaningful names!
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Variables (contd.)
• A variable must be declared before its use
Example:
int age, quantity, price;
/* declares three variables that can
store integer values */
• To initialize a variable, use =
Example:
age = 21;
• Declaration and initialization can be combined in one statement
Example:
int amount1 = 45, amount2 = 500, amount3 = 999;
• An uninitialized variable is simply junk (in general)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Variables in Main Memory
• Consider the following C Program:
#include <stdio.h>
int main()
{
int num1, num2, num3;
num1 = 2;
num2 = 4;
num3 = num1 + num2; // computing the sum of num1 and num2
printf(“The sum is: %d \n”, num3); // printing the sum
return 0;
}
• This program has three variables: num1, num2 and num3.
• Where are these variables stored?
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Remember this block diagram!
• OS loads the
program executable
into the RAM
• Executes it line by
line on CPU
A line Program
from exe Program Executable (P1)
(exe)
Program is Compiled
executed Executable
Program of P1 (exe)
on CPU
CPU Executable gets
line by line
loaded into the
RAM
Data Types
Data types
• C is a typed language
• Every data item has a type associated with it.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Fundamental data types in C
• Integer (short, int, long)
• Character (char)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Machine Readable size of
variables
• Every variable occupies space in the main memory
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Organization of Main Memory
Each location
is of 8 bits
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Storing int in main memory
• Remember how integer
values are represented in 2’s
complement representation.
• Our machines use 2’s
complement representation to
store integer variables.
Space occupied
• If int variables are of 2 bytes by int variable,
which is 2 bytes or
size (or 16 bits), then each int 16 bits
variable shall occupies 2
contiguous locations in the
memory
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Types of integer variables
Type / Subtype Minimum size Range Format
(bytes) Specifier
short 2 -32,768 to 32,767 (-215 to 215-1) %hd
unsigned short 2 0 to 65,535 (0 to 216) %hu
int 2 or 4 -32,768 to 32,767 or - %d
2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 0 to 65,535 or 0 to 4,294,967,295 %u
long 4 or 8 -2,147,483,648 to 2,147,483,647 %ld
or -9223372036854775808 to
9223372036854775807
unsinged long 4 or 8 0 to 4,294,967,295 or 0 to %lu
18446744073709551615
long long 8 -9223372036854775808 to %lld
9223372036854775807
unsinged long long 8 0 to 18446744073709551615 %llu
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Knowing the size of data types
• How do we know the size of a data type for our C compiler?
• Use sizeof() operator
• Example:
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Types of floating point
numbers
While integers are classified by size, floating point numbers are
classified by precision
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Questions for you
What happens when you store a very large value in an ‘int’?
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Qualifiers in C
• Sign Qualifier
• Size Qualifier
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Character type
• If everything is in binary, how do we deal with characters?
• Using Character-encoding schemes, such as ASCII
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
ASCII Encoding
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Computations with char
char ch1 = ‘A’, ch2;
printf(“%c\n”, ch1);
ch2 = ch1 + 1;
printf(“%c\n”, ch2);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Non-printable chars
• Escape sequences – backslash followed by lowercase letter
– Examples: \n, \t, etc.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Chars and Strings
• “Internet” is a string, which is basically a bunch of chars put
together
• A string in C is an array of chars.
• We will study about arrays and strings in greater detail later!
• Note the use of double quotes
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Constants
Constants
• C specifies a default type for a constant.
• We can also force a type.
• Default for an integer is int. If a constant doesn’t fit in int, the
next higher type is tried.
• Suffixes can be used to coerce the data type.
• U or u for unsigned. L or l for long….
• For a floating point number, the default is double.
• Suffix F or f demotes it to float; L or l promotes it to long
double.
• A character constant is stored as an int (!!)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Declaring Constants
2 ways:
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Declaring Constants
• Using #define
• Syntax: #define variable_name value
• Note: no use of semicolon at the end
• Example:
#include<stdio.h>
#define X 10 //always written before int main()
int main(){
int A = X;
X = X+10; //error: lvalue required as left operand of assignment
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Type Conversions
Type Conversions
Implicit
– If either operand is long double, convert the other into
long double.
– Otherwise, if either operand is double, convert the other into
double.
– Otherwise, if either operand is float, convert the other into
float.
– Otherwise, convert char and short to int.
– Then, if either operand is long, convert the other to long.
Explicit (also known as coercion or typecasting)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implicit Type Conversion
e.g.,
float A = 100/3; output: 33.000000
float A = 100/3.0 output: 33.333333
int A = 10;
A = A + 'B'; Output: 76
‘B’ is type converted to integer
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Explicit Type Conversion
(Typecasting)
Example1:
Conversion of integer to a float variable
int a = 10;
float f = (float) a;
Example2:
Conversion of integer to a char variable
int a = 20;
char ch = (char) a;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Operators
• Can be unary, binary or ternary
• Used to perform some operation (e.g., arithmetic, logical,
bitwise, assignment) on operands
• In an expression with multiple operators, the order of
evaluation of operators is based on the precedence level
• Operators with the same precedence work by rules of
associativity
• C does not define the order in which the operands of an
operator will be evaluated
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Operator Description Associativity
() Parentheses (grouping) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Unary post-increment/post-decrement
++ -- Unary pre-increment/pre-decrement right-to-left
+ - Unary plus/minus
! ~ Unary logical negation/bitwise complement
(type) Unary cast (change type)
* Dereference
& Address
sizeof Determine size in bytes
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
, left-to-right
Comma (separate expressions) Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Types of Operators
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Special operators
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Arithmetic Operators
– *, /, %, + and –
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Arithmetic Operators
• int a = 31, b = 10;
• float c = 31.0, d = 10.0;
a + b = 41 c + d = 41.000000
a – b = 21 a – b = 21.000000
a/b = 3 c/d = 3.100000
a%b = 1 c%d = Not valid
a * b = 310 c * d = 310.000000
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Arithmetic Operators
• Unary arithmetic operator
⎯ Performs operation on single operand
⎯ ++ (Increment operator) and -- (Decrement operator)
⎯ Both these operator can be applied before an operand as
well as after the operand
⎯ All arithmetical operators follow left to right associativity
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Arithmetic Operators
Increment (++) and Decrement (--) operators
• Can be used either as prefix or postfix
– Prefix:
• “Change value and then use”
• Lower precedence than the postfix
– Postfix:
• “Use and then change value”
• Higher precedence than prefix
• Can be applied only to variables
• Causes side effects
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Increment and Decrement
Operators
Prefix:
++<variable_name> / --<variable_name>
Example:
int A = 10;
printf(“A is %d”, ++A);
printf(“A is %d”, --A);
Output: 11 10
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Increment and Decrement
Operators
Postfix:
<variable_name>++ / <variable_name>--
Example:
int A = 10;
printf(“A is %d”, A++);
printf(“A is %d”, A--);
Output: 10 11
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Relational Operators
• a == b checks whether a is equals to b
• a != b checks whether a is not equal to b
• a < b checks whether a is less than b
• a > b checks whether a is greater than b
• a <= b checks whether a is less than equal to b
• a >= b checks whether a is greater than equal to b
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Logical Operators
• expr1 && expr2 Returns true, when both operands are
non-zero
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Logical Operators – Practice
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Bitwise Operators
• Performs operation at bit level
• A = 43 and B = 7
• A|B= 0 0 1 0 1 0 1 1 (43)
0 0 0 0 0 1 1 1 (7)
= 0 0 1 0 1 1 1 1 (47)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Bitwise Operators
• a & b Known as bitwise AND. Sets the bit only when the
bits in both the operands are set
• A = 43 and B = 7
• A&B= 0 0 1 0 1 0 1 1 (43)
0 0 0 0 0 1 1 1 (7)
= 0 0 0 0 0 0 1 1 (3)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Bitwise Operators
• a^b Known as bitwise XOR. Sets the bit only when the bit
is set in only one of the operand
• A = 43 and B = 7
• A^B= 0 0 1 0 1 0 1 1 (43)
0 0 0 0 0 1 1 1 (7)
= 0 0 1 0 1 1 0 0 (44)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Bitwise Operators
• ~a Flip the bits. Sets the bit only when the bit in the
operand is not set.
• A = 43 = 0 0 1 0 1 0 1 1 (43)
~A = 1 1 0 1 0 1 0 0 (-84, SMF)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Assignment Operator
• A = B // assigns value of B to the variable A
• A op= B A = A op B, op can be any binary arithmetic or
bitwise operator
E.g., A = 43, B = 7
A += B A = A + B O/P: A = 50, B = 7
A &= B A = A & B O/P: A = 3, B = 7
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Comma operator
• Syntax: expr1, expr2, …
• Most often finds use in the for statement.
• Evaluated left to right, and the type and value of the result are
the type and value of the rightmost operand.
• Has the lowest precedence, and so should be used with caution,
when intended.
• Comma is also used as a separator – so don’t get confused.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Short Circuiting in C
• A short circuit in logic is when it is known for sure that an entire
complex conditional is either true or false before the evaluation
of whole expression
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Short Circuiting in Logical AND
a = 0, b = 3;
int I = ++a && ++b;
printf(“%d %d %d”, a, b, I);
O/P 1, 4, 1
a = 0, b = 3;
int I = a++ && ++b;
printf(“%d %d %d”, a, b, I);
O/P 1, 3, 0
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Short Circuiting in Logical OR
Consider the expression: E1 || E2
Output of the expression is false if only both E1 and E2 are zero
If E1 is true, there is no need to evaluate E2
a = 0, b = 3;
int I = ++b || ++a;
printf(“%d %d %d”, a, b, I);
O/P 0, 4, 1
a = 0, b = 3;
int I = ++a || ++b;
printf(“%d %d %d”, a, b, I);
O/P 1, 3, 1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Problems to Solve
int a = 1, b = 1;
int c = a || --b; // --b is not evaluated
int d = a-- && --b; // --b is evaluated, b becomes 0
printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
O/p : a = 0, b = 0, c = 1, d = 0
int i=-1,j=-1,k=0,l=2,m;
m = i++ && j++ && k++ || l++;
printf(“i = %d, j = %d, k = %d, l = %d, m =
%d",i,j,k,l,m);
O/p : i = 0, j = 0, k = 1, l = 3, m = 1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus