L6-L9-Variables, Data Types, Sizes and Constants
L6-L9-Variables, Data Types, Sizes and Constants
• Variable names can be as long as you want, although only the first 63 (or
31) characters might be significant.
• Compiler knows the amount of memory needed for storing the variable.
• Compiler can verify that operations done on a variable are allowed by its
type.
8/28/2019 CSE 1001 Department of CSE 14
Primary (built-in or Basic)Data types
INTEGER CHARACTER
double: the same as type float, and roughly twice the size of
float.
short int or
signed short int 8 -128 to 127
short
unsigned int
8 0 to 255
• Character zero ( ‘0’ ) is not the same as the number (integer constant) 0.
• There are other escape sequences like, \t for tab, \v for vertical tab, \n
for new line etc.
letter = ‘A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = “A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are internally stored
as numeric values (ASCII code) */
The value before the letter e is known as the mantissa, whereas the value
that follows e is called the exponent.
Type Size
32 bits
Single Precision Float
4 bytes
64 bits
Double Precision double 8 bytes
#include <stdio.h>
int main ()
{
int a = 25;
int b = -2;
printf(“%d\n”,-a);
printf(“%d\n”,-b);
return 0;
}
!= Is not equal to
ATTENTION !
the “is equal to” operator == and the “assignment” operator =
ATTENTION !
When comparing floating-point values, Only < and >
comparisons make sense !
8/28/2019 CSE 1001 Department of CSE 34
Relational operators
An expression such as a < b containing a relational operator is called a
relational expression.
The value of a relational expression is one, if the specified relation is true and
zero if the relation is false.
E.g.:
10 < 20 is TRUE
20 < 10 is FALSE
A simple relational expression contains only one relational operator and takes
the following form.
!(FALSE) = TRUE
!(TRUE) = FALSE
m=5;
y=m++; Postfix Mode
Here y continues to be 5. Only m changes to 6.
Don’ts:
Attempting to use the increment or decrement
operator on an expression other than a modifiable
variable name or reference.
Example:
++(5) is a syntax error
&(AND),|(OR),^(EXOR)
op op
These are binary operators and & | ^
1 2
require two integer operands.
1 1 1 1 0
These work on their operands bit 1 0 0 1 1
by bit starting from LSB
(rightmost bit). 0 1 0 1 1
0 0 0 0 0
These are used to move bit patterns either to the left or right.
Note:
x=y<<1; same as x=y*2 (Multiplication)
x=y>>1; same as x=y/2 (Division)
Suppose x=1001100010001111
~x=0110011101110000 (complement)
• The table in the next slide gives the implicit type conversions
One operand is Long int, and the other is unsigned int a) Result will be long int
then
a) If unsigned int can be converted to long int the
unsigned int operand will be converted
b) Else both operands will be converted to unsigned b) Result will be unsigned long
long int int
One operand is Long int, the other will be converted Result is also long int
to long int
One operand is unsigned Long int, the other will be Result is also unsigned long int
converted to unsigned long int
8/28/2019 54
Type Conversions in Expressions
• The final result of an expression is converted to the type of
the variable on the left of the assignment sign before
assigning the value to it
• However the following changes are introduced during the
final assignment
• Float to int causes truncation of the fractional part
• Double to float caused rounding of digits
• Long int to int causes dropping of the excess higher order
bits
• The type cast operator has the effect of converting the value of the variable ‘a’ to
type float for the purpose of evaluation of the expression.
• This operator does NOT permanently affect the value of the variable ‘a’;
• The type cast operator has a higher precedence than all the arithmetic operators
except the unary minus and unary plus.
Example Action
x=(int) 7.5 7.5 is converted to integer by
truncation
a=(int) 21.3/(int)4.5 Evaluated as 21/4 and the result
would be 5
b=(double)sum/n Division is done in floating point mode
y=(int)(a+b) The result of a+b is converted to
integer
z=(int)a+b a is converted to integer and then
added to b
p=cos((double)x) Converts x to double before using it
• Example:
count += 10;
• Equivalent to:
count=count+10;
Equivalent to:
if ( a > b )
maxValue = a;
else
8/28/2019 maxValue = b; CSE 1001 Department of CSE 63
Comma (,)operator
The coma operator is used basically to separate expressions.
i = 0, j = 10; // in initialization [ l r ]
Detailed
Precedence
Table
2*((i/5)+(4*(j-3))%(i+j-2))
67
8/28/2019 CSE 1001 Department of CSE
Example solution:
2*((8/5)+(4*(5-3))%(8+5-2))
2*(1+(4*2)%11)
2*(1+8%11)
2*(1+8)
2*9
18
Evaluation:
+ (x==25 && y< 10)
< (x==25 && true)
== (False && true)
&& (False)