SWE CSN 125 CWD122 Lectures Notes 1
SWE CSN 125 CWD122 Lectures Notes 1
Introduction
COMPUTER SYSTEMS
―A Computer is an electronic device that stores, manipulates and retrieves the data.
The following are the objects of computer System
a) User ( A person who uses the computer)
b) Hardware
c) Software
COMPUTER SOFTWARE
Software of a computer system can be referred as anything which we can feel and see.
Example: Windows, icons
System software manages the computer resources. .It provides the interface between the
hardware and the users.
Application software, on the other hand is directly responsible for helping users solve their
problems.
System Software
System software consists of programs that manage the hardware resources of a computer
and perform required information processing tasks.
These programs are divided into three classes:
➢ the operating system,
➢ system support, and
➢ system development.
The operating system provides services such as a user interface, file and database access,
and interfaces to communication systems such as Internet protocols.
The primary purpose of this software is to keep the system operating in an efficient manner
while allowing the users access to the system.
System support software provides system utilities and other operating services.
Examples of system utilities are sort programs and disk format programs.
Operating services consists of programs that provide performance statistics for the
operational staff and security monitors to protect the system and data.
The last system software category, system development software, includes the language
translators that convert programs into machine language for execution ,debugging tools to
ensure that the programs are error free and computer –assisted software
engineering(CASE) systems.
Application software
Application software is broken in to two classes:
➢ general-purpose software and
➢ application – specific software.
Application –specific software can be used only for its intended purpose.
COMPUTER LANGUAGES
To write a program (tells what to do) for a computer, we must use a computer language.
Over the years computer languages have evolved from machine languages to natural
languages.
2) Assembly Languages:
• English-like abbreviations representing elementary computer
operations.
• translated to machine code by using assemblers.
Example:
MOV AL,3BH
ADD AL, AH
SUB AL,AH
MOV [SI]
Advantages:
1) Easy to understand and use
2) Easy to modify and isolate error
3) High efficiency
3) High-level Languages:
• Codes similar to everyday English
• Use mathematical notations
• translated to machine code by using compilers.
• C, C++, PASCAL, FORTRAN, BASIC are high-level languages.
Example:
c=a+b;
if(a<b)
printf(“a is less than b\n”);
else
printf(“a is NOT less than b\n”);
Advantages:
1) Easy to write and understand
2) Easy to isolate an error
3) Machine independent language
4) Easy to maintain
5) Better readability
6) Low Development cost
7) Easier to document
8) Portable
Disadvantages:
1) Needs translator
2) Requires high execution time
3) Poor control on hardware
4) Less efficient
Difference between Machine, Assembly, High Level Languages
Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.
1) Compiler: It is a program which is used to convert the high level language
programs into machine language
2) Assembler: It is a program which is used to convert the assembly level
language programs into machine language
3) Interpreter: It is a program, it takes one statement of a high level language
program, translates it into machine language instruction and then immediately
executes the resulting machine language instruction and so on.
Comparison between a Compiler and Interpreter
COMPILER INTERPRETER
An interpreter is used to translate
A Compiler is used to compile an entire
each line of
program and an executable program is
the program code immediately as it
generated through the object program
is entered
The executable program is
The executable program is stored in a
generated in RAM
disk for
and the interpreter is required for
future use or to run it in another
each run of
computer
the program
The compiled programs run faster The Interpreted programs run slower
A very few languages use
Most of the Languages use compiler
interpreters.
➢ The Preprocessor
➢ The Translator
The Preprocessor reads the source code and prepares it for the translator.
While preparing the code, it scans for special instructions known as preprocessor
commands. These commands tell the preprocessor to look for special code libraries.
The result of preprocessing is called the translation unit.
After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language. The translator reads the
translation unit and writes the resulting object module to a file that can then be combined
with other precompiled units to form the final program.
An object module is the code in the machine language.
Linking Programs
The Linker assembles all functions, the program‘s functions and system‘s functions into
one executable program.
Executing Programs
To execute a program we use an operating system command, such as run, to load the
program into primary memory and execute it.
Getting the program into memory is the function of an operating system program known as
the loader. It locates the executable program and reads it into memory. When everything is
loaded the program takes control and it begin execution.
ALGORITHM
Algorithm is a finite sequence of instructions, each of which has a clear meaning and can
be performed with a finite amount of effort in a finite length of time. No matter what the
input values may be, an algorithm terminates after executing a finite number of instructions.
Example
Q. Write a algorithm to find out if a number is odd or even?
Ans.
step 1 : start
step 2 : input number
step 3 : rem=number mod 2
step 4 : if rem=0 then
print "number even"
else
print "number odd"
endif
step 5 : stop
FLOWCHART
Flowchart is a diagrammatic representation of an algorithm. Flowchart is very helpful in
writing program and explaining program to others.
Symbols Used In Flowchart
Different symbols are used for different states in flowchart, For example: Input/Output and
decision making has different symbols. The table below describes all the symbols that are
used in making flowchart
Draw flowchart to find the largest among three different numbers entered by user.
INTRODUCTION TO C LANGUAGE
C is a general-purpose high level language that was originally developed by Dennis Ritchie
for the Unix operating system. It was first implemented on the Digital Equipment Corporation
PDP-11 computer in 1972
The Unix operating system and virtually all Unix applications are written in the C language.
C has now become a widely used professional language for various reasons.
➢ Easy to learn
➢ Structured language
➢ It produces efficient programs.
➢ It can handle low-level activities.
➢ It can be compiled on a variety of computers.
Facts about C
➢ C was invented to write an operating system called UNIX.
➢ C is a successor of B language which was introduced around 1970
➢ The language was formalized in 1988 by the American National Standard Institue
(ANSI).
➢ By 1973 UNIX OS almost totally written in C.
➢ Today C is the most widely used System Programming Language.
➢ Most of the state of the art software have been implemented using C
Why to use C?
C was initially used for system development work, in particular the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as code written in assembly language.
C Program File
All the C programs are written into text files with extension ".c" for example hello.c. You can
use "vi" editor to write your C program into a file
BASIC STRUCTURE OF C PROGRAMMING
The program execution begins at the opening brace and ends at the closing brace.
The closing brace of the main function is the logical end of the program.
All statements in the declaration and executable part end with a semicolon.
For example
• #include -- includes contents of a named file. Files usually called header files.
➢ #include <math.h> -- standard library maths file.
➢ #include <stdio.h> -- standard library I/O file
C Compiler
The C compiler translates source to assembly code. The source code is received from the
preprocessor.
Assembler
The assembler creates object code. On a UNIX system you may see files with a .o suffix
(.OBJ on MSDOS) to indicate object code files.
Link Editor
If a source file references library functions or functions defined in other source files the link
editor combines these functions (with main()) to create an executable file.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
• The following program displays "Hello World" on the computer screen (monitor).
#include <stdio.h>
Preprocessor directive:
• Tells computer to load contents of a header file <stdio.h>,
• which includes standard input/output functions.
• For example printf()is one of the standard input/output functions.
Simple C Program:
/* This is our first program in C Language */
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
int main()
• C programs contain one or more functions,
return 0;
• A way to exit a function.
• return 0, in this case, means that the program terminated normally.
Right brace }
• Indicates end of main has been reached.
Example 1: Write a C program which displays your name and surname in two consecutive
lines .
Example 2: Write a C program which displays the following lines.
Today
is a
nice
day
C Program: Addition of two integer numbers
/* This program adds two integer numbers */
#include <stdio.h>
int main()
{
int a, b, sum; /* variable declarations */
printf("Enter first integer\n"); /* prompt the user */
scanf( "%d", &a); /* read first integer */
printf("Enter second integer\n"); /* prompt the user */
C TOKENS
C tokens are the basic buildings blocks in C language which are constructed together to
write a C program.
C KEYWORDS
C keywords are the words that convey a special meaning to the c compiler. The keywords
cannot be used as variable names.
The list of C keywords is given below:
C IDENTIFIERS
Identifiers are used as the general terminology for the names of variables, functions and
arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits
with either a letter or the underscore(_) as a first character.
There are certain rules that should be followed while naming c identifiers:
Name Remark
_A9 Valid
Invalid as it contains special character other than the
Temp.var
underscore
void Invalid as it is a keyword
C CONSTANTS
A C constant refers to the data items that do not change their value during the program
execution. Several types of C constants that are allowed in C are:
Integer Constants
Integer constants are whole numbers without any fractional part. It must have at least one
digit and may contain either + or – sign. A number with no sign is assumed to be positive.
There are three types of integer constants:
Real Constants
The numbers having fractional parts are called real or floating point constants. These may
be represented in one of the two forms called fractional form or the exponent form and
may also have either + or – sign preceding it.
The mantissa must be either an integer or a real number expressed in decimal notation.
The letter e separating the mantissa and the exponent can also be written in uppercase i.e.
E
And, the exponent must be an integer.
Character Constants
A character constant contains one single character enclosed within single quotes.
Examples of valid character constants
‘a‘ , ‘Z‘, ‘5‘
It should be noted that character constants have numerical values known as ASCII values,
NOTE: An escape sequence consumes only one byte of space as it represents a single
character.
Escape Sequence Description
a Audible alert(bell)
b Backspace
f Form feed
n New line
r Carriage return
t Horizontal tab
v Vertical tab
\ Backslash
― Double quotation mark
‗ Single quotation mark
? Question mark
Null
STRING CONSTANTS
String constants are sequence of characters enclosed within double quotes. For example,
“hello”
“abc”
“hello9”
Every sting constant is automatically terminated with a special character „‟ called thenull
character which represents the end of the string.
For example, “hello” will represent “hello” in the memory.
Thus, the size of the string is the total number of characters plus one for the null character.
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose.
[] () {} , ; : * … = #
Braces{}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Brackets[]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.
VARIABLES
A variable is nothing but a name given to a storage area that our programs can
manipulate.
Each variable in C has a specific type, which determines
➢ the size and layout of the variable's memory;
➢ the range of values that can be stored within that memory;
➢ and the set of operations that can beapplied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore. Upper and lowercase letters are distinct
because C is case-sensitive.
Based on the basic types explained, there will be the following basic variable types -
Type Description
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
C programming language also allows defining various other types of variables like
Enumeration, Pointer, Array, Structure, Union, etc.
Variable Definition in C
A variable definition tells the compiler where and how much storage to create for the
variable.
A variable definition specifies a data type and contains a list of one or more variables of
that type as follows -
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double, bool, or
any user-defined object; and variable_list may consist of one or more identifier names
separated by commas. Some valid declarations are shown here –
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the compiler to
create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The initializer
consists of an equal sign followed by a constant expression as follows –
type variable_name = value;
For definition without an initializer: variables with static storage duration are implicitly
initialized with NULL (all bytes have the value 0); the initial value of all other variables are
undefined.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with
the given type and name so that the compiler can proceed for further compilation without
requiring the complete detail about the variable.
A variable definition has its meaning at the time of compilation only; the compiler needs
actual variable definition at the time of linking the program.
A variable declaration is useful when multiple files are used.
ARITHMETIC OPERATORS IN C
C Arithmetic operators are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus in C programs.
Arithmetic
S.no Operation Example
Operators
1 + Addition A+B
2 – Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
OUTPUT:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
ASSIGNMENT OPERATORS IN C
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as
“sum = 10;”
Other assignment operators in C language are given below.
OUTPUT:
Total = 45
RELATIONAL OPERATORS IN C
Relational operators are used to find the relation between two variables. i.e. to compare
the values of two variables in a C program.