LOCAL VARIABLE
|
GLOBAL VARIABLE
|
|
1
|
Variables declared within the function body are
called local variables.
|
Variables declared outside of all the functions of a
program and accessible by any of these functions are called global variables.
|
2
|
They are automatically created at the point of their
declaration within the function body, and exist inside the specific function
that creates them.
|
They are created at the beginning of program
execution and remain in existence all through the period of the execution of
the program.
|
3
|
They are unknown to other functions and to the main
program.
|
These variables are known to all functions in the
program and can be used by these functions as many times as may be required.
|
4
|
They are recreated each time a function is executed
or called.
|
They do not get recreated if the function is
recalled.
|
5
|
The existence of the local variables ends when the
function completes its specific task and returns to the calling point.
|
Global variables do not cease to exist when control
is transferred from a function. Their value is retained and is available to
any other function that accesses them.
|
Showing posts with label Unit-2. Show all posts
Showing posts with label Unit-2. Show all posts
Saturday, November 8, 2014
2.10 LOCAL VARIABLE vs GLOBAL VARIABLE
Thursday, September 25, 2014
2.9 STORAGE CLASSES
To
fully define a variable, one needs to maintain not only its ‘type’ but also its
‘storage class’. A variable’s storage class tells us:
ü Where
the variable would be stored.
ü
What will be initial value of the
variable
ü
What is the scope of the variable i.e.
in which functions the value of the variable would be available.
ü What
is the life of variable i.e. how long would the variable exist.
There
are four storage classes in C:
1. Automatic
storage class
2.
Register storage class
3.
Static storage class
4. External
storage class
Automatic
Storage Class
Storage
|
Memory
|
Default
Initial Value
|
Garbage
|
Scope
|
Local
to the block, in which variable is defined
|
Life
|
Till
the control remains within the block in which the variable is defined
|
Example:
(i)
#include<stdio.h>
void
main()
{
auto
int i = 1;
{
{
{
printf(“%d”,i):
}
printf(“%d”,i);
}
printf(“%d”,i);
}
}
OUTPUT
1 1 1
(ii)
#include<stdio.h>
void
main()
{
auto int i, j = 1;
printf(“%d%d”, i, j);
}
OUTPUT
216321 1
(iii)
#include<stdio.h>
void
main()
{
auto
int i = 1;
{
auto int i = 2;
{
auto int i =
3;
printf(“%d”,i):
}
printf(“%d”,i);
}
printf(“%d”,i);
}
OUTPUT
3 2 1
Register Storage
Class
Storage
|
CPU
Registers
|
Default
Initial Value
|
Garbage
|
Scope
|
Local
to the block, in which variable is defined
|
Life
|
Till
the control remains within the block in which the variable is defined
|
NOTE: A value
stored in CPU register can always be accessed faster than the one that is
stored in memory.
Example:
#include<stdio.h>
void
main()
{
register int i;
for(i = 1; i <= 5; i++)
printf(“%d”, i);
}
OUTPUT
1 2 3 4 5
Static Storage
Class
Storage
|
Memory
|
Default
Initial Value
|
Zero
|
Scope
|
Local
to the block, in which variable is defined
|
Life
|
Value
of the variable persists between different function calls.
|
Example:
AUTO
#include<stdio.h>
void
increment();
void
main()
{
increment();
increment();
increment();
}
void
increment()
{
auto int i = 1;
printf(“%d”, i);
i++;
}
OUTPUT
1 1 1
|
STATIC
#include<stdio.h>
void
increment();
void
main()
{
increment();
increment();
increment();
}
void
increment()
{
static int i = 1;
printf(“%d”, i);
i++;
}
OUTPUT
1 2 3
|
External Storage
Class
Storage
|
Memory
|
Default
Initial Value
|
Zero
|
Scope
|
Global
|
Life
|
As
long as the program’s execution does not come to an end.
|
Example:
#include<stdio.h>
int
i;
void
increment();
void
decrement();
void
main()
{
printf(“%d”, i);
increment();
increment();
decrement();
decrement();
}
void
increment()
{
i = i + 1;
printf(“%d”, i);
}
void
decrement()
{
i = i - 1;
printf(“%d”, i);
}
OUTPUT
0 1 2 1 0
WHICH TO USE
WHEN
ü Use static
storage class only if you want the value of a variable to persist between
different function calls.
ü
Use register storage class for only
those variables that are being used very often in a program.
ü
Use extern storage class for only those
variables that are being used by almost all the functions in the program. This
would avoid unnecessary passing of these variables as arguments when making a
function call.
ü Most
of the times, we use auto variables, because often it so
happens that once we have used the variables in a function, we don’t mind
losing them.
2.8 OPERATORS and EXPRESSIONS
§ C
supports a rich set of built-in operators.
§
An operator is a symbol that tells the
computer to perform certain mathematical or logical manipulations.
§ C
operators can be classified into 8 categories.
Arithmetic
Operators
§ C
provides all the basic arithmetic operators
Operator
|
Meaning
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo
division
|
NOTE: %
cannot be applied to float data values
§
Three different type of calculations can
be carried out:
i.
Integer arithmetic
ii.
Real arithmetic
iii.
Mixed-mode arithmetic: When one operand
is integer and other floating point, the result is always floating point
because integer gets promoted to float first and then the expression is
evaluated.
Relational
Operators
§ We
often compare two quantities and depending on their relation, take certain
decisions. These comparisons can be done with the help of relational operators.
Operator
|
Meaning
|
<
|
is
less than
|
<
=
|
is
less than or equal to
|
>
|
is
greater than
|
>
=
|
is
greater than or equal to
|
= =
|
is
equal to
|
! =
|
is
not equal to
|
Logical
Operators
§ C has the following three logical
operators
&&
meaning logical AND
|| meaning logical OR
! meaning logical NOT
Truth
Table
OP-1
|
OP-2
|
OP-1
&& OP-2
|
OP-1
|| OP-2
|
!OP-1
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
-
|
0
|
1
|
0
|
1
|
1
|
0
|
0
|
0
|
0
|
-
|
Assignment
Operator
§ Assignment
operator is used to assign the result of an expression to a variable.
§
Syntax:
variable_name
= expression;
§
C also has a set of ‘shorthand’
assignment operator of the form:
Syntax:
v op = exp;
where, v is a
variable
op is a arithmetic operator
exp is an expression
Statement with simple assignment
operator
|
Statement with shorthand operator
|
a =
a + 1
|
a +
= 1
|
a =
a - 1
|
a -
= 1
|
a =
a * (n+1)
|
a *
= n + 1
|
a =
a / (n+ 1)
|
a /
= n - 1
|
a =
a % b
|
a %
= b
|
Increment and
Decrement Operators
§ Increment
and decrement operators are unary operators and they require variable as their
operands.
§
These operators are solved right to
left.
§
When postfix ++ (or --) is used with a
variable in an expression, the expression is evaluated first using the original
value of the variable and then the variable is incremented (or decremented) by
one.
§
When prefix ++ (or --) is used in an
expression, the variable is incremented (or decremented) first and then the
expression is evaluated using the new value of the variable.
§ The
precedence and associativity of ++ and – operators are the same as those of
unary + and unary -.
Conditional
Operator
§ A
ternary operator pair’?:’ is available in C to construct conditional
expressions of the form:
exp1
? exp2 : exp3;
where exp1, exp2, exp3 are
expressions.
Working
exp1 is
evaluated first. If it is nonzero (true), then the expression exp2 is evaluated
and becomes the value of the expression. If exp1 is false, exp3 is evaluated
and its value becomes the value of the expression.
e.g.
a =
10;
b =
15;
x =
(a > b) ? a : b;
Bitwise
Operators
§ C has
a distinction of supporting special operators known as bitwise operators for
manipulation of data at bit level.
§
These operators are used for testing of
bits, or shifting them right or left.
Operator
|
Meaning
|
&
|
bitwise
AND
|
|
|
Bitwise
OR
|
^
|
bitwise
exclusive OR
|
<<
|
Shift
Left
|
>>
|
Shift
Right
|
Special
Operators
a)
Comma
Operator
§ The
comma operator can be used to link the related expressions together.
§ A
comma-linked list of expressions are evaluated left to right and the value of
right-most expression is the value of the combined expressions.
e.g. value = (x = 10,
y = 5, x + y);
b) sizeof Operator
§ the
sizeof is a compile time operator and whn used with an operand, it returns the
number of bytes the operator occupies.
§ The
operand may be a variable, a constant or a data type qualifier.
e.g. m = sizeof (sum);
m = sizeof (long int);
§ The
sizeof operator is normally used to determine the lengths of array and
structures when their sizes are not known to the programmer.
§ It is
also used to allocate memory space dynamically to variables during the
execution of a program.
PRECEDENCE OF
ARITHMETIC OPERATORS
§ An arithmetic
expression without parentheses will be evaluated from left to right using the
rules of precedence of operators.
§
There are two distinct priority levels
of arithmetic operators in C:
High
priority * / %
Low
priority + -
§ The
basic evaluation procedure include ‘two’ left-to-right passes through the
expression.
During
the first pass, the high priority operators (if any) are applied as they are
encountered.
During
the second pass, the low priority operators (if any) are applied as they are
encountered.
e.g.
x = 9 - 12 / 3 + 3 * 2 – 1
First Pass:
Step 1: x = 9 –
4 + 3 * 2 – 1
Step 2: x = 9 –
4 + 6 – 1
Second Pass:
Step 3: x = 5 + 6 – 1
Step 4: x = 11 – 1
Step 5: x = 10
TYPE CONVERSION
IN EXPRESSIONS
§ If
the operands are of different types, the lower type is automatically converted
to the higher type before the operation proceeds. The result is of higher type.
§
Conversion
Hierarchy
Fig 06: Conversion
Hierarchy
§ Implicit Type Conversion
C permits mixing
of constants and variables of different types in an expression. C automatically
converts any intermediate values to the proper type so that the expression can
be evaluated without losing any significance. This automatic conversion is
known as implicit type conversion.
§ Explicit Type Conversion
The process of
local conversion is known as explicit conversion or casting a value. The
general form of cast is:
(data type) expression;
e.g.
The calculation
of ratio of females to males in a town is given by the formula:
ratio
= female_number / male_number;
Since
female_number and male_number are declared as integers in the program, the
decimal part of the result of division would be lost and ratio would represent
a wrong number. This problem can be solved by converting locally one of the
variables to the floating point as shown below:
ratio = (float) female_number / male_number;
ratio = (float) female_number / male_number;
The operator (float) converts the
female_number to floating point for the purpose of evaluation of the
expression. Then using the rule of automatic conversion, the division is
performed in floating point mode, thus returning the fractional part of the
result.
OPERATOR
PRECEDENCE and ASSOCIATIVITY
§ Precedence is
defined as the priority assigned to operators such that it would be clear that
which operator should be applied first.
The operator
with higher priority should be given priority over the operator with lower priority.
§ Associativity defines
which operator will work and how it will work.
OPERATOR
|
DESCRIPTION
|
ASSOCIATIVITY
|
RANK
|
( )
[ ]
|
Function
Call
Array
element reference
|
Left to Right
|
1
|
+
-
++
--
!
~
*
&
sizeof
(type)
|
Unary
plus
Unary
minus
Increment
Decrement
Logical
negation
One’s
complement
Pointer
reference
Address
Size
of an object
Typecast(conversion)
|
Right to Left
|
2
|
*
/
%
|
Multiplication
Division
Modulus
|
Left to Right
|
3
|
+
-
|
Addition
Subtraction
|
Left to Right
|
4
|
<<
>>
|
Left
Shift
Right
Shift
|
Left to Right
|
5
|
<
< =
>
> =
|
Less
than
Less
than or equal to
Greater
than
Greater
than or equal to
|
Left to Right
|
6
|
= =
!=
|
Equality
Inequality
|
Left to Right
|
7
|
&
|
Bitwise
AND
|
Left to Right
|
8
|
^
|
Bitwise
XOR
|
Left to Right
|
9
|
|
|
Bitwise
OR
|
Left to Right
|
10
|
&&
|
Logical
AND
|
Left to Right
|
11
|
||
|
Logical
OR
|
Left to Right
|
12
|
?:
|
Conditional
Operator
|
Right to Left
|
13
|
=
*=
/= %=
+= -=
&=
^=
|=
<<= >>=
|
Assignment
Operator
|
Right to Left
|
14
|
,
|
Comma
Operator
|
Left to Right
|
15
|
Subscribe to:
Posts (Atom)
