Chapter 1
Chapter 1
Introduction to Computer
What is a Computer?
Types of Software
Computer Language
Introduction to C Language
Introduction to Computer
What is a Computer?
Hardware
All the equipment’s and accessories which can be seen and touched within the
computer system or related to the computer system.
Hardware is the equipment used to perform the necessary computations and
includes the CPU, monitor, keyboard and printer etc.
Software
Types of Software
Prewritten application programs are the programs, which are used by a user to
acquire the desired results.
Examples are Windows, MS Word and Paintbrush etc
Computer Language
Machine Language
Low Level Language
High Level Language
Machine Language
In the Low Level Language, Machine Language is still used by the computer as it
processes data, but Low Level Language software first translate the specified
operation symbol onto its Machine Language equivalent.
Example is Assembly Language.
The main difference between the High Level Language and the other two are that
High Level Language is much easier and understandable by a human being and
the second difference is that in High Level Language one instruction can perform
several machine level instructions.
C Language in between the Low Level Language and High Level Language.
That’s why it is also called a Middle Level Language, since it was designed to
have both: a relatively good programming efficiency and relatively good machine
efficiency.
1. Use an editor or a word processor program to enter each line of the source
program into memory and to save it on disk as a source file.
2. Use a compiler program to translate the source program into machine
language. If there are syntax error i.e error in grammar, the compiler displays
these errors on the monitor. Use the editor program to correct the errors by
editing and resaving the source program.
3. When the source program is free of syntax errors, the compiler saves its
machine language translation as an object program.
4. The linker program combines your object program with additional object files
that may be needed for your program to execute and saves the final machine
language program an executable file on the disk.
5. The loader program places the executable file in the memory, ready for
execution.
Source Revised
File Source
File
Success
Failure List of
Object Errors
File
Executable
File
Executable
Program in
Memory
It is the 3rd step of the problem-solving tool. In this step all the programming
steps are written in human languages step by step to solve the problem.
Algorithm is the sequence of instruction, which are given to computer.
Example what to perform technical task, how the instruction can be divided in
a modules, skeleton development of a problem.
The flow chart uses boxes of different shape to represent different types of
instructions. These symbols have been standardize by ANSI ( American National
Standard Institute ). Some of the major symbols are shown as follows.
a). Terminators.
This symbol is used to indicate the beginning and ending of the program.
It is the first and last step of the flow chart.
b). Input/Output
This symbol is used to show the process of any input or output function
such as read or write instructions.
c). Process
d). Decision
e). Documentation
If a program is too long that might cause some confusion, we connect the
program by connector to make it simpler. An On-Page connector connects
the flow chart on the same page. A connector symbol is represented by a
circle and a letter or digit is placed within the circle to indicate the link.
When the program is too long to fit in a single page then we use the
symbol of Off-Page connector to continue the program on the other page.
These arrows show the flow of the program or flow chart. These arrows
show the exact sequence of the program instructions.
Program written in flow charts are transferred in any computer languages step
by step is called Coding.
6. Integration
Debugging means to find out or remove the errors from the program. There
are four types of errors that may be encountered when running a program.
Logical errors are often most difficult type of errors to detect. These errors
can be avoided be careful planning and by following a properly
constructed flow chart. If a program runs but does not produce the
desirable results then there may be an error in the logical organization of
the program.
Introduction to C Language
1. IDE
IDE means Integrated Development Environment. It is a screen display
with windows and pull down menus. The program listing, its output,
error messages and other information are displayed in separate
windows. You can use menu selections to invoke all the operations
necessary to develop your program including editing, compiling,
debugging, linking and program execution.
Memory Models
There are five memory models: Small, Compact, Medium, Large and
Huge.
In the Small model your program can have upto 64K of code and 64K of
data storage.
In the Medium model the program code can be exceed this 64K limit, but
the data is still restricted to 64K.
Other models offer different arrangements of memory space.
Each memory model requires a separate library file, and each library files
takes up space on your hard disk.
Executable Files
Executable files are stored in the subdirectory BIN. The most important
executable file for the Turbo C Language is the TC.EXE. Executing this program
places the IDE on your screen.
The BIN directory also contains programs for the command line development
process. For example
Various files are combined with your programs during linking. These files contain
routines for a wide variety of purposes. They are stored in LIB subdirectory.
Library Files
Library files are group of precompiled routines for performing specific tasks. For
example, if a programmer uses a function such as printf() to displayed text on
the screen, the code to create the display is contained in a library file. A library
file has a unique characteristic: only those parts of it that are necessary will be
linked to a program, not the whole file.
Header Files
The subdirectory called INCLUDE contains header files. These files are text files,
like the one you generate with a word processor or the Turbo C editor. Header
files can be combined with your program before it is complied, in the same way
that a typist can insert a standard heading in a business letter. Each header file
has a “. h” file extension.
Programmer-Generated Files
You can place the programs that you write in any subdirectory you choose; for
instance, a subdirectory under TC.
The edit window should be active, with the cursor flashing at the upper right
corner. Type a program as follows:
{
printf ( “ This is my first program in C Language. “ );
}
Type as you would on a typewriter. Characters will appear where the cursor is
positioned. Press [ Enter ] key to move the cursor to the beginning of the net line.
If you make a mistake, use the [ Backspace ] key to delete the character to the
left of the cursor, or the [ Del ] key to delete the character under the cursor.
After you have typed in the source file for the program, you should save it on
your disk or hard disk. To do this, select the Save option from the File menu. You
can also accomplish the same effect simply by pressing the [ F2 ] function key.
Saving the source file is not necessary before compiling and running your
program, but it is a good idea and programming practice.
After you have written the source file for your program, you need to turn it into an
executable file. The compiler, which is a part of the IDE, translate the source
code into another file known as ( .obj ) Object file, consisting of the machine
language. The linker then links the entire necessary object files together to
produce a final executable program with the extension ( .exe ).
In Turbo C IDE, compiling and linking can be performed together in one step.
There are two ways to do this: either you can select make EXE file from the
compile menu, or you can press the [ F9 ] key.
If there is no errors in the compiling and linking process, you are ready to run the
program by simply selecting Run option from the Run menu bar or by pressing
the [ Ctrl ] [ F9 ] key combination.
To see the program output, select user screen option from the window menu or
press [ Alt ] [ F5 ].
To exit from the Turbo C IDE, select the Quit option from the file menu.
Class Assignment No 1
Compiler Vs Interpreter
Compiler translates the whole source program into machine language at once
while the Interpreter translates the source program into machine language line by
line.
{
printf ( “ This is my first program in C Language. “ );
}
Function Definition
Delimiter
Statement Terminator
The printf ( ) function
Function Definition
All C Language programs are divided into units called “Functions”. A function
in C will always a ( ) at its end. Main( ) is always be the first function to be
executed and is the one to which control is passed when the program is
executed.
The word “void” preceding the “main“ specifies that the function main( ) will
not return a value. The second “void”, in the parenthesis, specifies that the
function takes no arguments.
Delimiter
Following the function definition are the braces, which signals the beginning
and ending of the body of the function.
The opening brace “ { “ indicates that a block of code that forms a distinct unit
is about to begin. The closing brace “ } “ terminates the block code.
Statement Terminator
The line in our program that begins with the word “ printf “ is an example of a
statement. A statement in C Language is terminated with a semicolon “ ; “.
The linker looks in the CS.LIB file, finds the section of this file containing
printf( ) and causes this section to be linked with the source program.
The printf( ) function uses a unique format for printing constants and variables.
For example
{
printf ( “ I am %d years old. “, 20 );
}
Output:
I am 20 years old.
The function printf( ) can be given more than one argument. For example in the
above program we have taken 2 arguments in the printf( ) function. The two
arguments are separated by a comma. The printf( ) function takes the vales on
the right of the comma and plugs it into the string on the left.
Why we are using it?
Why does it plug in it?
Where it finds a format specifier such as %d?
Format Specifier
The format specifier tells the printf( ) where to put a value in a string and what
format to use in printing the values. In the previous program, the %d tells the
printf( ) to print the value 20 as a decimal integer.
Then what will be the method of writing a character or floating point number?
Why not simply put the decimal integer into the original string as compared to the
format specifier?
%c single character
%s string
%d signed decimal integer
%f floating point ( decimal notation )
%e floating point ( exponential notation )
%g floating point ( %f or %e, which ever is shorter )
%u unsigned decimal integer
%x unsigned hexadecimal integer
%o unsigned octal integer
Example:
{
printf ( “ My name is %s and I am %d years old. “, ”Ahmad”, 20 );
}
Output:
Example:
{
printf ( “ The letter %c is “, ‘j’ );
Output:
In the above program, there are two different new things to note.
Second thing to note is that even though the output statement is printed by two
separate program lines, it does not consists of two line of text on the output
screen. That is because printf( ) does not automatically prints a newline character
at the end of a line.
Class Assignment No 2
3. Why not simply put the decimal integer into the original string as compared to
the format specifier?
4. Write a program, which shows the output of the character, exponential and
unsigned decimal integer.