enotes_unit-I_ PartII
enotes_unit-I_ PartII
Types of C Variables
As we saw earlier, an entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory. These locations can contain integer,
real or character constants. In any language, the types of variables that it can support depend on
the types of constants that it can handle. This is because a particular type of variable can hold
only the same type of constant. For example, an integer variable can hold only an integer
constant, a real variable can hold only a real constant and a character variable can hold only a
character constant.
The rules for constructing different types of constants are different. However, for constructing
variable names of all types the same set of rules apply. These rules are given below.
Rules for Constructing Variable Names
a) A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some
compilers allow variable names whose length could be up to 247 characters. Still, it
would be safer to stick to the rule of 31 characters. Do not create unnecessarily long
variable names as it adds to your typing effort.
b) The first character in the variable name must be an alphabet or underscore.
c) No commas or blanks are allowed within a variable name.
d) No special symbol other than an underscore (as in gross_sal ) can be used in a variable
name.
Ex.: si_int
m_hra
pop_e_89
C compiler is able to distinguish between the variable names by making it compulsory for you to
declare the type of any variable name that you wish to use in a program. This type declaration is
done at the beginning of the program. Following are the examples of type declaration statements:
Ex.: int si, m_hra ;
float bassal ;
char code ;
Since, the maximum allowable length of a variable name is 31 characters; an enormous number
of variable names can be constructed using the above-mentioned rules. It is a good practice to
exploit this enormous choice in naming variables by using meaningful variable names.
Thus, if we want to calculate simple interest, it is always advisable to construct meaningful
variable names like prin , roi, noy to represent Principle, Rate of interest and Number of years
rather than using the variables a, b , c.
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a
broad sense to the computer). The keywords cannot be used as variable names because if we do
so we are trying to assign a new meaning to the keyword, which is not allowed by the computer.
Some C compilers allow you to construct variable names that exactly resemble the keywords.
However, it would be safer not to mix up the variable names and the keywords. The keywords
are also called „Reserved words‟.
There are only 32 keywords available in C. Figure 1.5 gives a list of these keywords for your
ready reference.
Local variables:
Variable whose existence is known only to the main program or functions are called local
variables. Local variables are declared with in the main program or a function. Default value is
garbage.
Global variables:
Variables whose existence is known to the both main() as well as other functions are called
global variables. Global variables are declared outside the main() and other functions. Default
value is zero.
#include<stdio.h>
int g=10;
void main()
{
int l=20;
printf(“global=%d \n local = %d”,g,l);
}
Data Types in C
Data types specify how we enter data into our programs and what type of data we enter.
C language has some predefined set of data types to handle various kinds of data that we can use
in our program. These datatypes have different storage capacities.
These are fundamental data types in C namely integer (int), floating point(float), character(char)
and void.
Derived data types are nothing but primary datatypes but a little twisted or grouped together like
array, stucture, union and pointer.
Data type determines the type of data a variable will hold. If a variable x is declared as int. it
means x can hold only integer values. Every variable which is used in the program must be
declared as what data-type it is.
Integer type
Integers are used to store whole numbers.
void type
void type means no value. This is usually used to specify the type of functions which returns
nothing. We will get acquainted to this datatype as we start learning more advanced topics in C
language, like functions, pointers etc.
Memory Types
1. Random Access Memory (RAM) –
It is also called as read write memory or the main memory or the primary memory.
The programs and data that the CPU requires during execution of a program are stored in
this memory.
It is a volatile memory as the data loses when the power is turned off.
RAM is further classified into two types- SRAM (Static Random Access Memory) and
DRAM (Dynamic Random Access Memory).
Storing Characters
Any piece of data that is stored in a computer‟s memory must be stored as a binary number. That
includes characters, such as letters and punctuation marks. When a character is stored in
memory, it is first converted to a numeric code. The numeric code is then stored in memory as a
binary number. Over the years, different coding schemes have been developed to represent
characters in computer memory. Historically, the most important of these coding schemes is
ASCII, which stands for the American Standard Code for Information Interchange.
ASCII is a set of 128 numeric codes that represent the English letters, various punctuation marks,
and other characters. For example, the ASCII code for the uppercase letter A is 65. When you
type an uppercase A on your computer keyboard, the number 65 is stored in memory (as a binary
number, of course).
Fetch-decode-execute cycle:
When a CPU executes the instructions in a program, it is engaged in a process that is known as
the fetch-decode-execute cycle. This cycle, which consists of three steps, is repeated for each
instruction in the program. The steps are:
1. Fetch A program is a long sequence of machine language instructions. The first step of the
cycle is to fetch, or read, the next instruction from memory into the CPU.
2. Decode A machine language instruction is a binary number that represents a command that
tells the CPU to perform an operation. In this step the CPU decodes the instruction that was just
fetched from memory, to determine which operation it should perform.
3. Execute The last step in the cycle is to execute, or perform, the operation.