Lecture 2
Lecture 2
1
Keywords
Tokens in C
These are reserved words of the C language. For example int, float, if,
else, for, while etc.
Identifiers
An Identifier is a sequence of letters and digits, but must start with a letter. Underscore
( _ ) is treated as a letter. Identifiers are case sensitive. Identifiers are used to name
variables, functions etc.
Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If
Invalid: 324, short, price$, My Name
Constants
Constants like 13, ‘a’, 1.3e-5 etc.
Integral Types
char Stored as 8 bits. Unsigned 0 to 255.
Signed -128 to 127.
short int Stored as 16 bits. Unsigned 0 to 65535.
Signed -32768 to 32767.
int Same as either short or long int.
long int Stored as 32 bits. Unsigned 0 to 4294967295.
Signed -2147483648 to 2147483647
One should check the file limits.h to what is implemented on a particular machine.
Naming a Variable
Must be a valid identifier.
Must not be a keyword
Names are case sensitive.
Variables are identified by only first 32 characters.
Library commonly uses names beginning with _.
Naming Styles: Uppercase style and Underscore style
lowerLimit lower_limit
incomeTax income_tax
Declaring a Variable
Each variable used must be declared.
A form of a declaration statement is
data-type var1, var2,…;
Declaration announces the data type of a variable and allocates appropriate
memory location. No initial value (like 0 for integers) should be assumed.
It is possible to assign an initial value to a variable in the declaration itself.
data-type var = expression;
Examples
int sum = 0;
char newLine = ‘\n’;
float epsilon = 1.0e-6;
else
printf(“%d is an odd number\n”, num);
Relational Operators
<, <=, > >=, ==, != are the relational operators. The expression
operand1 relational-operator operand2
takes a value of 1(int) if the relationship is true and 0(int) if
relationship is false.
Example
int a = 25, b = 30, c, d;
c = a < b;
d = a > b;
value of c will be 1 and that of d will be 0.
Logical Operators
&&, || and ! are the three logical operators.
expr1 && expr2 has a value 1 if expr1 and expr2 both are
nonzero.
expr1 || expr2 has a value 1 if expr1 and expr2 both are
nonzero.
!expr1 has a value 1 if expr1 is zero else 0.
Example
if ( marks >= 40 && attendance >= 75 ) grade =
‘P’
If ( marks < 40 || attendance < 75 ) grade =
‘N’
Assignment operators
The general form of an assignment operator is
v op= exp
Where v is a variable and op is a binary arithmetic operator. This
statement is equivalent to
v = v op (exp)
a = a + b can be written as a += b
a = a * b can be written as a *= b
a = a / b can be written as a /= b
a = a - b can be written as a -= b
0.d1d 2 p B
e
dRepresentation
All floating point numbers are stored as
Representation
Sk = { x | Bk-1 <= x < Bk }. Number of elements in each Sk is same. In
the previous example it is 900.
Gap between seuccessive numbers of Sk is B k-p.
B1-p is called machine epsilon. It is the gap between 1 and next
representable number.
Underflow and Overflow occur when number cannot be represented
because it is too small or too big.
Two floating points are added by aligning decimal points.
Floating point arithmetic is not associative and distributive.