Computer 12 Notes
Computer 12 Notes
Computer 12 Notes
Source Revised
File Source File
List of Errors
New
Object File
Executable
File
Executable File
In Memory
Create a file
• User can open a menu using mouse or press ALT+F key to open a file menu.
• Select New to open a new file.
Edit a file
• To edit a file first we need to open that file.
• To open a file from file menu select open.
• Locate the file in open box.
• Press ok to open the located file
Save a file
• To save a file select save from file menu or press F2 short cut key.
• A dialog box will appear.
• Write down the name, extension and location of the file to create e.g. Z:\ First.C
Compile a file
• From compile menu select compile option OR press ALT + F9
[Ch#8] Getting Started with C 201 Computer Science Part-II
• If there is no error in the program, it will be translated to object code.
Execute a file
• To execute a file select run from run menu OR press Ctrl + F9
• The out put of the program is shown on the screen.
• To see the out put press ALT + F5.
Q5. Define Source program, Object Program, Linker and Loader.
Ans.
Source Program:
• The program written in High Level Language is called source program.
• The computer does not understand the source code.
• The source code is converted into machine code and then it is directly executed on the
computer.
Object Program:
• The machine language version that result from compiling the source is called object
code. Compiler stores the object code on storage media for exhibition.
• The computer understands the object code directly.
Linker:
• The linker is a program that combines the object program with additional library files
and produces an executable file with .exe extension.
• In linking process the object file is linked to many other library files.
• The executable file (.exe) runs directly on the computer after linking process.
• Each source program consists of built-in functions. Built in functions are contained in
library files.
• Linker links all relevant library files with our object code and produces an executable file.
• Linker creates executable file when the source code is error free.
Loader:
• It is a system software. For execution, the loader loads the executable files in the
memory.
• Ctrl+F9 key is used to load and run the programs.
• The executable file (.exe) runs directly on the computer after linking process.
• When a program is run, the output screen will disappear in a moment to see the
output press Alt+F5.
Q6. What is the basic structure of a C Program.
Ans.
Basic Structure of a C Program:
Structure of a C program means the logic to write a program. C is structured
programming language. It provides a better way of writing programs. The linker of C
program links many files before execution. There are two parts of a C program structure.
o Preprocessor Directives.
[Ch#8] Getting Started with C 202 Computer Science Part-II
o Main function with C statements
Preprocessor Directives:
• The instructions given to the compiler before the beginning of the actual program are
called preprocessor directives or compiler directives.
• It always begins with # symbol e.g. #include, #define. These are the instructions that
tells the compiler to perform an action before compiling the source program.
#include directive:
• The include directive gives a program access to the library.
• The include directive causes the preprocessor to insert definitions from a standard
header file into a program before compilation i.e. The include directive tells the
compiler where to find the meaning of identifiers used in the program.
• It is used to include header files in the program. There are two methods to include
header files.
#include <header file name>
Or
#include “header file name” (User defined HF)
Examples:
#include<stdio.h>
#include “conio.h”
#define directive:
• This preprocessor directive is used to define a constant macro.
• Constant Macro: It is a name that is replaced by a particular constant value before
compilation.
• It is use to assign a constant value to an identifier.
• Macro name cannot be changed during program execution.
Syntax:
#define Macro_name expression/constant
Example:
#define PI 3.142857
Preprocessor:
• Preprocessor is a part of compiler that modifies or handles a C program before its
compilation.
[Ch#8] Getting Started with C 203 Computer Science Part-II
• It is a program that processed the preprocessor directives. It performs some
processing before compilation.
Header Files:
• These files contain the definition of standard library functions. Each header file
contains definition of one type of functions only e.g. math.h contains the definition of
all mathematical functions.
• The extension of header file is .h
• The include directive is used to add header files in the source program.
• All the header files are located in INCLUDE subfolder.
• These are the prototypes for the statements used in the program. To use a library
function in a program its relevant header file must be mention at the top of program.
• There are two methods to include header files.
#include <header file name>
Or
#include “header file name” (User defined HF)
Examples:
#include<stdio.h>
#include “conio.h”
Main Function:
• It indicates the beginning of a C program.
• Every C program has a main function and if it is not included in the program, then the
compiler generates an error message.
• The remaining lines in the program are enclosed in pair of curly braces and are called
body of main function i.e. all the program statements are written in body of main
function.
• The user-defined functions are created outside the main function.
• The program execution always begins from main function.
Syntax:
void main (void)
{
Statements of the program // Body of main function
}
• The definition of main starts with void represents the data type returned by main,
which means function returns nothing, however, it can also return a value.
[Ch#8] Getting Started with C 204 Computer Science Part-II
• The second void enclosed in parentheses represents that the main function does not accept
any argument or any parameter; however, arguments can be passed to the main function.
• Main function may accept one or more value and may output one value. The data type of
main is specified before keyword main.
Delimiters:
• The braces in the main function represents start and end of the program, these braces
are called delimiters.
• { Represents start of the code.
• } Represents end of the function code.
• The lines between curly braces are statements of the program.
Statement Terminator:
• Each statement of a C program ends with a semicolon (;) called statement terminator.
• If statement terminator (semicolon) is missing in any statement then the compiler will
generate the following error message:
Statement missing;
Q7. What are programming errors? Also discuss its different types.
Ans.
POGRAMMING ERRORS:
• The errors in a program are called BUGS.
• The process of finding and removing these errors is called debugging.
There are three types of errors.
1. Syntax Errors
2. Run Time Errors
3. Logical Errors.
Syntax Errors:
• A syntax error occurs when the program violates one or more grammar rules of
language (HLL).
• A compiler detects these errors at the time of compilation.
• If a program has syntax error then it can not be translated and executed.
• The errors must be removed for the successful compilation.
• These errors are easy to locate and remove because the compiler specifies the location
and type of error.
[Ch#8] Getting Started with C 205 Computer Science Part-II
• In C language examples are:
Missing statement terminator
Function call missing
A misspelled keyword
Run Time Errors:
• A run time error occurs when the program directs the computer to perform an illegal
operation.
• Run time errors are detected and displayed by the computer during the execution of a
program.
• When a run time error occurs, the computer stops the execution and displays a
diagnostic message.
Example:
Dividing number by zero.
Logical Errors:
• A logical error occurs when a program follows a faulty algorithm.
• The compiler cannot detect logical errors; therefore no error message is reported from
the compiler.
• Programs cannot be crashed due to logical errors.
• It is difficult to detect logical error. Program with logical errors produces wrong output.
• All the statements should be verified one by one to detect and remove the logical errors.
Examples:
Use of incorrect mathematical formula.
Data input is incorrect.
Incorrect sequence of operations in a program.
Q8. Explain the debugging feature of Turbo C++.
Ans.
Debugging Feature of Turbo C++:
• Turbo C++ is an IDE (integrated development environment) tool. It provides us
the facility to create, edit, delete, run, compile, and debugging the programs.
• Turbo C++ provides many useful debugging features. Through these features, we
can easily locate logical and syntax errors. Debugging options are provided as a
[Ch#8] Getting Started with C 206 Computer Science Part-II
separate menu named Debug in the Menu bar. Here is a brief description of these
features.
• Compiling Single Line:
TC Compiler provides an excellent option to compile the program line by line.
This enables the programmer to easily locate errors. For compiling a single line at
a time, select Run → Trace Into OR press F7 shortcut key.
• Watches:
Watches or watch expressions are used to check the value of a variable during the
program execution. It shows that how and when the value of some particular
variable is changed. It is normally used in combination with compiling single line.
Following procedure is used to apply watch or watch expressions.
o Compile the program line by line through F7 OR select Run → Trace Into.
o During the execution, when control comes to the line that contains required
variable, place the cursor on that variable whose value is to be checked.
o Select Debug → Watches from Menu bar. A sub menu will appear.
o Select Add Watch from the submenu OR simply use Ctrl + F7 shortcut key. A
dialog box will appear with selected variable shown in Watch Expression
field.
o Click OK or press Enter. TC sub-window shows an error message
automatically that shows the current value of the selected variable.
• Breakpoints:
• It is the most successful and an effective technique of debugging. A breakpoint is
a point in the program where the compiler temporarily stops the program
execution, so that the programmer can analyze necessary values. TC provides awn
easy way to use this mechanism for applying breakpoints. A breakpoint can be
applied through two simple steps:
• Place the cursor on the line where you want to apply the breakpoint.
• Select Debug → Toggle Breakpoint OR use Ctrl + F8 shortcut key. Turbo C++
automatically stops the program execution when control reached at that particular
line.
• Evaluate/Modify Window:
The evaluate/modify window is used to change the value of variable during
program execution. It can be useful if the user is single stepping the program and
wants to change the value of a certain variable. The following procedure is used
to use evaluate/modify window:
[Ch#8] Getting Started with C 207 Computer Science Part-II
o Select Debug > Evaluate/Modify: A new window will appear with three
fields.
o Enter the name of the variable whose value is to be modified in expression
field.
o Enter the new value for the variable in New Value field. The value of the third
field Result will also change automatically.
Q.9 Write a short note on Turbo C++ compiler.
Ans.
• Turbo C++ is a compiler used for C and C++ languages. Since, C++ is based on C
language. Turbo C++ provides an environment in which programmer can write,
compile, and debug programs for both languages. It is a software product of
Borland International.
• It provides a complete IDE (Integrated Development Environment) also called TC
Editor. it is used to create, edit and save C programs. TC provides a powerful
debugger that helps the programmers in detecting and removing errors. Here is a
basic layout of Turbo C IDE.
• Turbo C editor provides an easy way to write programs. The user can open TC
Editor through one of the following ways:
o Simply type “TC” in Command prompt (at C:\)
o Double click on the TC shortcut at C → TC → Bin → TC shortcut.
o Type C:\ TC\ bin\TC in Start → Run text box and OK button to run Turbo C
Editor.
• The Menu bar contains of Turbo C Editor contains menus to create, edit, compile,
execute, and debug C programs. A menu can be accessed through mouse or
keyboard shortcut. For keyboard shortcut, combination of Alt key with the first
highlighted character of each menu is used. For example, Alt + F is used to open
File menu.
Q.10 Clarify the difference between “.C” and “.CPP” extension.
Ans.
• Turbo C++ is a compiler used for both C and C++ languages. The default extension
for each program in TC environment is “.CPP” (stands for C Plus Plus) which is used
to include many additional features for C++ programs. Many of these additional
features are not supported by ANSI C. So, “.CPP” extension must be changed into
“.C” because the standard extension of C programs is only “.C”. In Short:
o .CPP extension is used for C++ programs.
[Ch#8] Getting Started with C 208 Computer Science Part-II
o .C extension is used for C programs.