0% found this document useful (0 votes)
7 views10 pages

enotes_unit-I_ PartII

The document discusses the types of C variables, including local and global variables, and outlines the rules for constructing variable names and the significance of keywords in C programming. It also covers data types in C, detailing primary and derived types, and provides information on memory types, data storage, and how programs operate within a computer's CPU. Additionally, it explains the fetch-decode-execute cycle that the CPU follows to execute instructions from a program.

Uploaded by

Vrinda Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

enotes_unit-I_ PartII

The document discusses the types of C variables, including local and global variables, and outlines the rules for constructing variable names and the significance of keywords in C programming. It also covers data types in C, detailing primary and derived types, and provides information on memory types, data storage, and how programs operate within a computer's CPU. Additionally, it explains the fetch-decode-execute cycle that the CPU follows to execute instructions from a program.

Uploaded by

Vrinda Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit-I Part II

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.

C language supports 2 different type of data types:

1. Primary data types:

These are fundamental data types in C namely integer (int), floating point(float), character(char)
and void.

2. Derived data types:

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.

Size and range of Integer type on 16-bit machine:

Type Size(bytes) Range

int or signed int 2 -32,768 to 32767

unsigned int 2 0 to 65535

short int or signed short int 1 -128 to 127

unsigned short int 1 0 to 255

long int or signed long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

Floating point type

Floating types are used to store real numbers.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

Float 4 3.4E-38 to 3.4E+38

Double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932


Character type

Character types are used to store characters value.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255

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

2. Read Only Memory (ROM) –


Stores crucial information essential to operate the system, like the program essential to
boot the computer.
 It is not volatile.
 Always retains its data.
 Used in embedded systems or where the programming needs no change.
 Used in calculators and peripheral devices.
 ROM is further classified into 4 types- ROM, PROM, EPROM, and EEPROM.
Types of Read Only Memory (ROM) –
1. PROM (Programmable read-only memory) – It can be programmed by user. Once
programmed, the data and instructions in it cannot be changed.
2. EPROM (Erasable Programmable read only memory) – It can be reprogrammed. To
erase data from it, expose it to ultra violet light. To reprogram it, erase all the previous
data.
3. EEPROM (Electrically erasable programmable read only memory) – The data can
be erased by applying electric field, no need of ultra violet light. We can erase only
portions of the chip.

3. Secondary Storage Devices


Secondary storage is a type of memory that can hold data for long periods of time, even when
there is no power to the computer. Programs are normally stored in secondary memory and
loaded into main memory as needed.
Examples: Disk Drives, Floppy Disk, CD-ROM, Magnetic Tape etc..

How Computers Store Data


CONCEPT: All data that is stored in a computer is converted to sequences of 0s and 1s.
A computer‟s memory is divided into tiny storage locations known as bytes. One byte is only
enough memory to store a letter of the alphabet or a small number. In order to do anything
meaningful, a computer has to have lots of bytes. Most computers today have millions, or even
billions, of bytes of memory.
Each byte is divided into eight smaller storage locations known as bits. The term bit stands for
binary digit. Computer scientists usually think of bits as tiny switches that can be either on or off.
Bits aren‟t actual “switches,” however, at least not in the conventional sense. In most computer
systems, bits are tiny electrical components that can hold either a positive or a negative charge.
Computer scientists think of a positive charge as a switch in the on position, and a negative
charge as a switch in the off position.
Storing Numbers
A bit can be used in a very limited way to represent numbers. Depending on whether the bit is
turned on or off, it can represent one of two different values. In computer systems, a bit that is
turned off represents the number 0 and a bit that is turned on represents the number 1. This
corresponds perfectly to the binary numbering system. In the binary numbering system (or
binary, as it is usually called) all numeric values are writ-ten as sequences of 0s and 1s. Here is
an example of a number that is written in binary:
10011101
The position of each digit in a binary number has a value assigned to it. Starting with the
rightmost digit and moving left, the position values are 20, 21, 22, 23, and so forth, as shown in
Figure 1. Figure 1 shows the same diagram with the position values calculated. Starting with the
rightmost digit and moving left, the position values are 1, 2, 4, 8, and so forth.
Figure 1 The values of binary digits as powers of 2

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

How a Program Works


CONCEPT: A computer‟s CPU can only understand instructions that are written in machine
language. Because people find it very difficult to write entire programs in machine language,
other programming languages have been invented.
Earlier, we stated that the CPU is the most important component in a computer be-cause it is the
part of the computer that runs programs. Sometimes the CPU is called the “computer‟s brain,”
and is described as being “smart.” Although these are common metaphors, you should
understand that the CPU is not a brain, and it is not smart. The CPU is an electronic device that is
designed to do specific things. In particular, the CPU is designed to perform operations such as
the following:
● Reading a piece of data from main memory
● Adding two numbers
● Subtracting one number from another number
● Multiplying two numbers
● Dividing one number by another number
● Moving a piece of data from one memory location to another
● Determining whether one value is equal to another value
● And so forth . . .
As you can see from this list, the CPU performs simple operations on pieces of data. The CPU
does nothing on its own, however. It has to be told what to do, and that‟s the purpose of a
program. A program is nothing more than a list of instructions that cause the CPU to perform
operations.
Each instruction in a program is a command that tells the CPU to perform a specific operation.
Here‟s an example of an instruction that might appear in a program:
10110000
To you and me, this is only a series of 0s and 1s. To a CPU, however, this is an instruction to
perform an operation. It is written in 0s and 1s because CPUs only understand instructions that
are written in machine language and machine language instructions are always written in binary.
A machine language instruction exists for each operation that a CPU is capable of performing.
For example, there is an instruction for adding numbers; there is an instruction for subtracting
one number from another; and so forth. The entire set of instructions that a CPU can execute is
known as the CPU‟s instruction set.
Programs are usually stored on a secondary storage device such as a disk drive. When you install
a program on your computer, the program is typically copied to your computer‟s disk drive from
a CD-ROM, or perhaps downloaded from a Web site. Although a program can be stored on a
secondary storage device such as a disk drive, it has to be copied into main memory, or RAM,
each time the CPU executes it. For example, suppose you have a word processing program on
your computer‟s disk. To execute the program you use the mouse to double-click the program‟s
icon. This causes the program to be copied from the disk into main memory. Then, the
computer‟s CPU executes the copy of the program that is in main memory.

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.

You might also like