COMPUTER TRAINING: Variables & Constants & C Instruction Set

Monday, 24 December 2012

Variables & Constants & C Instruction Set

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 that variable is stored in that location.
These locations may be integer, real or character etc.

Variables
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 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.

Variables
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;

Variables
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
An example program which shows the example of assignments:
Output of the Program:

Variables
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 localvariable.
A local variable can only be accessed by the function in which it is declared. It cannot be accessed by other function.
b) Global:
variable which is declared outside all functions is known as global variable.
variable with a global scope is accessible to all the statements in the program. A globalvariable can 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
 
 
 
Constants
 
Character constants
 
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
 
 
 
 
Constants
 
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 integerconstant. 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
 
 
 
 
Constants
 
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
 
 
 
 
Constants
 
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 astring constant.
 
some example of string constant:
 
: "Hello"
"Welcome to eBiz"
"a"
 
 
C instruction set
 
Introduction
 
In previous section we discuss about various types of C constants, variables and keyword.
 
Now in this section we will discuss about how they are grouped to from instructions.
 
There are basically four types of instruction in C. which has been described separately.
 
1. Type Declaration Instruction
 
2. Input Output instruction
 
3. Arithmetic Instructions
 
4. Control Instructions
 
 
 
C instruction set
 
Type Declaration & Input/Output Instructions
 
This type of instruction is used to declare the type of variable being used in the program.
 
Any variable used in the program must be declared before using it in any statement.
 
This instruction is usually written at the beginning of the C program.
 
Example:
 
int a;
float re,ad;
char name ,des.
 
Input Output instructions
 
These instructions are used to supply input data to a program and obtain the outputresults from it.
 
Example:
 
printf(), scanf().
 
 
 
C instruction set
 
Arithmetic Instructions
 
These types of instructions are used to perform arithmetic operations between constantsand variables.
 
Some of the arithmetic operators are: +, -, * and /.
 
Example:
 
int a;
float b, deta, alpha, gamma, beta;
a=500;
b=0.0056;
deta=alpha*beta/gamma+0.5*2/5;
 
There are three types of arithmetic statements:
 
1. Integer mode arithmetic statement:
 
In this type of arithmetic statement all operands are either integer variable or integer constant.
 
2. Real mode arithmetic statement:
 
In this type of arithmetic statement all operands are either real constant or real variables.
 
3. Mixed mode arithmetic statement:
 
In this type of arithmetic statement some of the operands are integer and some of them are real.
 
 
 
 
C instruction set
 
Control Instructions
 
These types of instructions are used for controlling the sequence of execution of various statements in C program. It determines the 'flow of control' in a program.
 
There are four types of control instructions in C:
 
1. Sequence Control Instructions
 
The sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program.
 
2. Selection or Decision Control Instructions
 
These types of instruction allow the computer to take a decision.
 
3. Case Control Instruction
 
These types of instruction determine which instruction is to be executed next.
 
4. Repetition or Loop Control Instruction
 
The loop control instruction helps computer to execute a group of statements repeatedly.
 
 
 
 

No comments:

Post a Comment