Variables
|
|
Introduction
|
|
The defined type of memory area where
the value is stored is called variable.
|
|
Variables are the data item whose
values may vary during the execution of the program.
|
|
A specific location or the address in
the memory is allocated for each variable and the value of
thatvariable is stored in that location.
|
|
These locations may be integer,
real or character etc.
|
|
Rules for constructing variables
names
|
|
There are some specific rules for
constructing variable names in C language:
|
|
(a) Variable name may be a
combination of alphabet digits or underscores and
its lengths should not exceed 8 characters, some compilers
allow 40 characters also.
|
|
(b) The first character must be
an alphabet.
|
|
(c) No comma, blank spaces are
allowed in variable name.
|
|
(d) No special symbols except underscore can
be used as variable names.
|
|
|
Variable
declaration
|
|
All
the variables must be declared before their use. It does two
things:
|
|
(a)
Tell the compiler what the variable name is.
|
|
(b)
Specify what type of data that a variable will hold.
|
|
Syntax
of variable declaration:
|
|
data_type
variable_name;
|
|
Example
of variable declaration:
|
int
i,j,k;
|
char
ch;
|
|
Assigning
values to variables To assign values to the variables, assignment
operator (=) is used. Syntax of assigning
values: variable declaration;Variable_name = value; Example of
assigning values: Int i , j;j = 5 ;i = 0 ; It is also possible to
assign a value at the time of declaration.e.g.int i = 5; More than one
variable can be initialized in one statement using multiple assignment
operators. e.g. j = m = 2; There could be an exception while using
multiple assignment operators. e.g. int i , j = 2 , k; here the
assignment will be i = 0 j=2 and k = garbage value.
|
Scope of variables: Local &
Global
|
|
Scope of variable means where the variable stands
in the program. All variables may not necessary be
available to all statements in a program.
|
|
Variables can have two types of
scope:
|
|
a) Local:
|
|
When a variable is declared inside the function
then such a variable is known as local variable.
|
|
A local variable can only be accessed by the
function in which it is declared. It cannot be accessed by other function.
|
|
b) Global:
|
|
A variable which is declared outside all functions
is known as global variable.
|
|
A variable with a global scope is accessible to all
the statements in the program. A global variablecan
be accessed by all the functions.
|
|
|
Constants
|
|
Introduction
|
|
There are some values which do not change during the execution of
the program. These values are called constants.
|
Constants are of fixed value that
remain unchanged during the execution of a program, and are used in
assignment of statements. Constants are stored in
variables.
|
|
Syntax of constant declaration:
|
|
Const datatype var_name = value;
|
|
Example of Constant declaration:
|
|
Const int a = 5;
|
|
In C language there are five types of constants which
has been described separately
|
|
Character constants
|
|
A character constant consists of a single digit or
a single special symbol enclosed within a pair of single inverted commas.
The maximum length of a character constant can be 1 character.
|
|
e.g. --> 'a', 'i' , '5', '='.
|
|
There are some character sequence constants which
are used in C to represented special action, these are called C
Escape Sequence.
|
|
List of these escape sequence and
its tasks are given below:
|
|
\a : audible bell
|
|
\f : form feed
|
|
\r : carriage return
|
|
\v : vertical tab
|
|
\' : single quote
|
|
\? : question mark
|
|
\HHH: 1 to 3 digit hex value.
|
|
\b : backspace
|
|
\n : newline
|
|
\t : horizontal tab
|
|
\\ : backslash
|
|
\" : double quote.
|
|
\000 : 1 to 3 digit
octal value
|
Integer constants
|
|
An integer constant refers to a sequence of digits.
It could be either positive or negative. and must have at least one digit.
|
|
It mustn't have a decimal point. No commas or blank are allowed
within an integer constant. The allowable range
for integer constants is -32767 to 32767.
|
|
There are three types of integer
constants:
|
|
1. decimal :
|
|
In decimal notation ,simply we write decimal number. e.g. 24,678
|
|
2. octal :
|
|
In octal notation, write(0)immediately before the octal
represention,e.g.-076,-076
|
|
3. hexadecimal :
|
|
In hexadecimal notation ,the constant is preceded by
0x,e.g.,0x3e,-0x3e.
|
|
Some example of integer constants:
|
|
: 426
|
: +762
|
: -8000
|
: -7605
|
Real constants
|
|
Real constants are often
called Floating Point constants.
|
|
It has three parts:
|
|
1. A sign (+ or -) preceding the number portion (optional).
|
|
2. A number portion (representing the base).
|
|
3. An exponent portion following the number portion (optional). This
starts with E or E followed by an integer. The integer may be preceded by a
sign.
|
|
A real constant must have at least one digit. It must have a decimal
point. It could be either positive (default) or negative. No commas and
blank are allowed within a real constant.
|
|
Some example of real constants:
|
|
: +.72
|
: +72
|
: +7.6E+2
|
: 24.4e-5
|
|
Logical & String constants
|
|
A logical constant can have either of two values
either true or false. In C a non-zero value is always treated as true
whereas zero is treated as false.
|
|
The logical constants are very useful in evaluating
logical expressions and complex condition.
|
|
A group of character enclosed within a pair of double inverted
commas (" ") is treated as a string
constant.
|
|
some example of string constant:
|
|
: "Hello"
|
"Welcome to Execute Codes"
|
"a"
|
|
|
|
|
More Topics: