Chapter-2-Computer Science-10 Class-Federal Board
Chapter-2-Computer Science-10 Class-Federal Board
1 2 INTRODUCTION TO COMPUTER
PROGRAMMING
Computer programming is the process of writing a computer program in computer language to
solve a particular problem. Computer program controls the operation of a computer and it is
developed in a computer language to perform a task. Therefore, it is essential for students to
learn computer languages to solve various problems. Computer languages are also known as
programming languages.
• Syntax
Syntax refers to the rules of a programming language according to which statements of a
program are to be written. It describes the way to write correct statements in a program. Syntax
of a programming language is similar to the grammar of a natural language.
For example, an assignment statement consists of a variable and an expression separated by
equal sign as an assignment operator. This is the syntax of assignment statement and it can be
expressed as given below. variable = expression;
• Semantic
Semantic gives meaning to statements of a programming language. It describes the sequence
of operations to be performed by a computer when executing the statements of a computer
program.
For example, in the assignment statement:
sum = a + b;
the semantic of the statement is to perform the expression, that is, add the values stored in
variables a and b and then store the result in variable sum.
Low level language is machine-oriented language. To understand low level language, detailed
knowledge of internal working of computer is required. Low level languages include machine
language and assembly language.
2 Programming in C
• Machine Language
Programming language that is directly understood by computer hardware is known as machine
language. Machine language is associated with architecture of computer. Therefore, programs
written in machine language for one computer will not work on another because of design
differences. It consists of zeroes and ones. It is almost impossible for humans to use machine
language because it entirely consists of numbers. Therefore, practically no programming is done
in machine language. Instead, assembly languages and high level languages are used.
• Assembly Language
Assembly language consists of symbolic codes or abbreviations known as mnemonics. It was
developed to make computer programming easier than machine language. The abbreviations
used in assembly language make it easier to learn and write programs compared to machine
language. A program written in assembly language must be converted into machine language
before it is executed by computer. A program known as assembler is used to translate assembly
language into machine language. Some important characteristics of Assembly language are:
Assembly language allows programmers to have access to all the special features of the
computer they are using. Certain types of operations which are not possible in high level
languages are easily programmed using assembly language.
Generally a program written in assembly language will require less storage and less running
time than one prepared in a high level language.
Assembly languages are still the best choice in some applications but their use is gradually
declining.
• Procedural Languages
Procedural programming is based upon the concept of modular programming. In modular
programming, programs are divided into smaller parts known as modules. Modular programs
consist of one or more modules. A module is a group of statements that can be executed more
than once in a program. Each module in a program performs a specific task.
It is easy to design, modify and debug a program in a procedural language since it provides
better programming facilities.
• Structured Languages
Structured languages consist of three fundamental elements, which are sequence, selection
and repetition.
Sequence: It means, writing program statements in a logical sequence. Each step in the
sequence must logically progress to the next without producing any undesirable effects.
Selection: It allows the selection of any number of statements based on the result of evaluation
of a condition which may be true or false. Examples of statements that implement selection in
programming are if, else-if, switch, etc.
Repetition (loop): It means executing one or more statements a number of times until a
condition is satisfied. Repetition is implemented in programs using statements, such as for and
while loops.
Some examples of structured languages are ALGOL, PL/1, Ada and Pascal.
Complicated and large computer programs are difficult to design, develop, maintain and debug.
The concept of object-oriented programming solves this problem.
The most widely used object-oriented programming languages are C++, C#, php and Java.
1) These languages were developed to make computer programming simple, easier and less
prone to errors.
2) High level languages are not machine dependent. They enable programmers to write
programs that are independent of a particular type of computer.
2 Programming in C
3) Programs written in high-level languages must be translated into machine language by a
compiler or an interpreter before execution by the computer.
4) The process of finding and removing errors in programs (debugging) is easier in high-level
languages compared to low level language.
5) High-level language programs are highly structured. They allow programmers to break
lengthy programs into a number of modules which can be written and tested independently.
This makes writing and testing of programs easier.
C++ was developed by Bjarne Stroustrup also at Bell Laboratories during 1983-1985. C++ is a
superset of C, meaning that any valid C program is also a valid C++ program. The purpose of
developing C++ was to provide programming facilities to easily and quickly write more powerful
programs.
Visual Basic
Visual Basic (VB) is a high level language which evolved from the earlier version called
BASIC. BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. VB is a very popular
programming language for writing Windows and Web applications. It provides a graphical
development environment to programmers to develop powerful Windows and Web applications.
VB is commonly used for developing business programs such as payroll system and inventory
control program. The user can also write programs related with engineering, science, arts,
education, games, etc.
C#
C# (pronounced as C-sharp) is a language developed in 2000 by Microsoft Corporation. It
is a simple, modern, general-purpose programming language. Syntax of C# is very similar to C
34
and C++. It also has some features of Java. It is a language that makes computer programming
easy and efficient. It provides facilities to write Web applications that can be used across the
Internet. All types of programs including games, utilities, operating systems, compilers, business
applications and Web based applications can be developed in C#.
Java
Java is a high-level language developed by Sun Microsystems. It is very similar in syntax to C
and C++. In Java, the user can write all types of programs as those written in other programming
languages and small programs that can be embedded in a Web page accessed through Internet.
Java is an ideal language for network computing. It is used for writing programs for a wide range
of devices, computers and networks. It is widely used in Web applications. The current versions
of most of the Web browsers are made Java enabled. A few browsers that support Java are
Microsoft’s Internet Explorer, Firefox and Mozilla.
Source program consists of statements written in a high level language such as C, Pascal, Java,
etc. For example, a program written in C language by a programmer to print table of a number
is known as source program. When it is translated with a compiler into machine language, the
resulting program is known as object program. The object program is understandable by
computer processor but difficult for a human to read and understand because it consists of
zeroes and ones.
Interpreter
2 Programming in C 35
Interpreter translates high level language programs into machine language but it translates one
instruction at a time and executes it immediately before translating the next instruction. Examples
of programming languages that use interpreter are Java Script, BASIC, Visual Basic and Perl.
Interpreter reads each statement of source program, one at a time and determines what it
means as it executes it. It means each time a statement is read, it must be translated into
machine language before execution. Compiler translates the entire program into object program
before execution by computer. Therefore, a compiled program runs fast.
2 Programming in C
• Text Editor
• Compiler
• Linker
• Loader
• Debugger
Text Editor
A text editor is a simple word-processor that is used to create and edit source code of a program
as shown in Fig.2-2. Files created by a text editor are plain text files. Most of the editors
automatically highlight compile errors to simplify removing them.
36
2 Programming in C
is used in the program, then it would assume that it is defined in C library. It will replace this
function in the object program with the code from C library and then create a single executable
program.
Loader
It is a software that loads programs into memory and then executes them.
Debugger
It is a software that executes a program line by line, examines the values stored in variables
and helps in finding and removing errors in programs.
• A to Z in Capital letters.
• a to z in Small letters.
Special Characters
C language contains the following special character in association with the letters and digits.
To introduce the structure of a C program, consider a very simple program that will print
the message:
I Love Pakistan
The program may be written as shown in Fig.2-6.
This program consists of three main parts which are preprocessor directive, the main()
function and the body of main().
Preprocessor Directives
Preprocessor directives are instructions for the C compiler. Every C language program contains
certain preprocessor directives at the beginning of the program. Before translating a C language
program into machine language, the compiler of C language carries out the processor directives.
These directives start with number sign (#). The most commonly used preprocessor directives
are #include and #define.
#include Preprocessor Directive It
has the following syntax.
#include<header file name>
When this preprocessor is carried out by the C compiler, it will search for the header file that is
written within the less than (<) and greater than (>) symbols and copy it into the source file.
In the above program the header file stdio.h is used. It tells the C compiler to copy the stdio.h
header file into the program. The stdio.h header file stands for standard input-output header. It
includes the standard printf( ) and scanf() function prototypes. In the above program the printf()
functions is used. Therefore, it is required to include this header file in the include preprocessor
directive.
main() Function
C programs consist of one or more functions. A function performs a single well-defined task.
Every C program must have the function main() which is the first section to be executed when
the program runs. The general form of main() is: void main(void)
The word void before the function main() means that this function does not return a value and
the second void inside the brackets means it does not have any argument.
Body of main() Function
The body of the function main() is surrounded by braces (curly brackets { and }). The left brace
indicates the start of the body of the function and the matching right brace indicates the end of
the body of the function.
The body of the function in the program (see Fig. 2.6) consists of a single statement printf()
which ends with a semicolon(;). printf() is the standard output function. The text to be printed is
enclosed in double quotes. A statement in C is terminated with a semicolon. Therefore, a
semicolon is used at the end of printf() statement. This program will print the message I Love
Pakistan on the screen.
42
The program in Fig.2-7 demonstrates how single line comments are used in a program.
In this program comments describe the purpose of statements.
When a variable is used in a computer program, the computer associates it with a particular
memory location. The value of a variable at any time is the value stored in the associated
memory location at that time. Variables are used so that the same space in memory can hold
different values at different times.
2.4.2 RULES FOR SPECIFYING VARIABLE NAMES
The following are the rules for specifying variable names in C language.
2 Programming in C
• A variable begins with a letter or underscore ( _ ) and may consist of letters, underscores
and/or digits.
• The underscore may be used to improve readability of the variable name. For example,
over_time.
• There is no restriction on the length of a variable name. However, only the first 31
characters of a variable are significant. This means that if two variables have the same first
31 characters they are considered to be the same variables.
• Both upper and lower case letters are allowed in naming variables. An upper case letter is
considered different from a lower case letter. For example the variable AVG is different
from Avg or avg.
• Special characters cannot be used as variable name. e.g., #, ?, @ etc.
• Reserved words of C language such as int, case, if, etc., cannot be used as variable
names.
• Space is not allowed in the name of variable. For example ma ss is not correct.
2.4.3 DATA TYPES USED IN C PROGRAMS
C provides three main data types for variables, that is, integer, floating-point and character
variables.
int sum;
The declaration consists of the type name, int, followed by the name of the variable, sum. All
the variables used in a C program must be declared. If there are more than one variable of the
same type, separate the variable names with commas as shown in the following declaration
statement.
44
int a,b,sum,product;
Declaring a variable tells the computer the name of the variable and its type. This enables the
compiler to recognize the valid variable names in program. This is very helpful if the user types
the wrong spellings of a variable name in his program, the compiler will give an error message
indicating that the variable is not declared.
Declaration of an integer variable can begin with the type qualifiers, short, long, unsigned or
signed. Some examples are:
short int num;
long int sum, avg;
unsigned int count;
short unsigned int
count;
Type qualifiers refer to the number of bytes used for storing the integer on a particular computer.
Refer to computer manual to find out the sizes of the computer being used. Generally, the
number of bytes set aside by the compiler for the above type qualifiers are as given in Fig.2-8.
-2,147,483,648 ~
long int 4 +2,147,483,647
Usually the number of bytes set aside by the compiler for the above floating-point type qualifiers
are as given in Fig.2-9.
Variable Declaration No. of Bytes Range
10-38 ~ 1038
float 4
(with 6 or 7 digits of precision)
10-308 ~ 10308
double float 8
(with 15 digits of precision)
10-4932 ~ 104932
long double float 10
(with twice the precision of double)
Fig.2-9 Floating-point data types used in C
2 Programming in C
Here, precision means the number of digits after the decimal point. The word float may be
omitted when it is preceded by double or long double type qualifiers in the declaration
statements.
There is another method of representing floating-point numbers known as exponential notation.
For example, the number 23,688 would be represented as 2.3688e4. Here, 2.3688 is the value
of the number (mantissa) and 4 is the exponent(e). The exponent can also be negative. For
example, the number 0.0005672 is represented as 5.672e-4 in exponential notation. Exponential
notation is used to represent very large and very small numbers. In C, a floating point number of
type float is stored in four bytes. Three bytes for the mantissa and one byte for the exponent
and provides 6 or 7 digits of precision.
Key Points
• Syntax refers to the rules of a programming language according to which statements of a
program are to be written.
• Semantic gives meaning to statements of a programming language.
46
B. 3
Exercise
D. 5
A. -32,768 ~ +32,767
C. 10-308 ~ 10308 ix. Which program translates high level language into
machine language?
A. Compiler
C. Loader
x. Which software helps in finding and removing errors in programs?
A. Linker
B. Loader
Short Questions
Q2. Give short answers to the following questions.
i. Define computer program.
ii. Differentiate between syntax and semantic. iii. Write three
differences between assembly language and HLLs. iv. Write
four characteristics of HLLs.
v. Define Integrated Development Environment (IDE).
vi. Differentiate between constant and variable.
vii. Which of the following are valid C variables? Give the reason if not a valid variable.
area, 5x, Sum, net pay, float, _age, else, case, size22, my_weight viii. What
are reserved words? Why they should not be used as variable names?
2 Programming in C 49
ix. Why comments are used in programs?
x. What is the purpose of header files in C language?
Extensive Questions