Computer 12 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

GETTING STARTED WITH C

Q1. What is a programming language? Also discuss its different types.


Programming Language:
• It is the way of communication between User and Computer.
• It is used to write computer programs.
• A computer program is a series of instructions that directs a computer to perform
tasks.
• A computer programmer sometimes called a developer, creates or modifies computer
programs.
• A programming language is a set of words abbreviations and symbols that enables a
programmer to communicate instructions to a computer.
• Just as human speak a variety of languages a programmer use a variety of languages
and tools to create programs.
• Several programming languages exist today. Each language has its own rules for
writing the instructions. A language provides the rules to write an instruction is called
its syntax.
• Language is designed for specific purposes e.g. scientific applications, business
solutions or Web Page Development.
Types of Programming Languages:
There are two types of computer languages.
1. Low Level Language
2. High Level Language
Low Level Language (LLL):
• The language, which is closed to machine, is called low-level language.
• A low level programming language is machine dependent. A machine dependent
language runs on only one type of computer.
• These programs are not portable to other types of computer.
[Ch#8] Getting Started with C 196 Computer Science Part-II
• A Computer can easily understand low-level language.
• The low level language requires a deep understanding of the machine architecture.
• There are two types of low-level language.
(i) Machine language
(ii) Assembly language
Machine language:
• It is the native language of computer. It is known as first generation programming
language.
• The computer does not need any translator to understand it.
• Every machine language instruction consists of 0’s and 1’s.
• It is difficult for human beings to understand and learn it.
• It is difficult to locate and remove errors in the program.
• It is directly executed by the computer.
• The machine language programs are machine dependent.
Assembly language:
• Machine language instructions(0’s and 1’s) are replaced with English like words
known as mnemonics ( Ne-monics ). It is second generation programming language.
• It is also called symbolic language.
• Symbolic instruction codes are meaningful abbreviations. A programmer writes C for
compare L for load M for multiply.
• An assembler is required to translate the assembly language programs into machine
language.
High Level Language (HLL):
• It is closed to human language.
• It is user friendly language. User can learn and understand high level language easily.
• The instructions of HLL are written in English statements.
• The programs of HLL are not directly executed on the computer.
• The programs written in HLL are machine independent.
• A language translator is required to translate the HLL into low level language. Each
language has its own translator. These translators are compilers and interpreters
• HLL programs are easy to modify, debug and more reliable.
• The HLL does not require a deep understanding of the machine architecture.
[Ch#8] Getting Started with C 197 Computer Science Part-II
• HLL describes a well defined way of writing programs.
• These languages are mostly used for writing application programs
• Every high level language provides a large number of built in functions.
• The programs in high level language are shorter than the programs in low level
language.
Examples:
• C and C++ (used for writing System Software)
• Java (Java is equipped with strong Network features)
• COBOL (common business oriented language and used for business applications)
• PASCAL
• FORTRAN (Formula Translation and used for mathematical problems)
• BASIC (Beginners all purposes symbolic instruction code)
TECHNIQUES OF PROGRAMMING IN HIGH LEVEL LANGUAGE:
Structured Programming Language:
• A programming language in which the logic of the program is divided into a number
of smaller sections or modules.
• Each section of the program performs a specific function.
• These programs are easy to write, debug and modify.
Un-Structured Programming Language:
• A programming language in which the logic of the program is written in a single
module.
• It is very difficult to detect any error in the program.
• Its readability is difficult.
Q2. What is a language processor? Also discuss its different types.
Ans.
Language Processor/Translator:
• It is software that is used to translate the high level language programs (source code)
into machine language (object code).
• Each language has its own translator and only one type of translator is used in any
language.
There are three types of language processor.
1. Compiler
2. Interpreter
3. Assembler
[Ch#8] Getting Started with C 198 Computer Science Part-II
Compiler:
• The language translator translates the source code into object code and the whole
program is translated at the same time.
• If a program contains errors then compiler cannot convert the source code into
machine code until all the errors are removed from the source program.
• C uses compiler to convert the source code into object code.
Interpreter:
• The language translator translates the source code into object code statement by
statement.
• The working of interpreter is slower than the compiler.
• If there is any error in the program statement it will stop its execution.
• After removing the error it will start reading the next statement.
Assembler:
• The language translator translates the assembly language into machine code.
Q3. What is C language? Also write down about its history. List out some
advantages of C language.
Ans.
C Language:
• In computing, C is a general-purpose procedural computer programming language. It
was originally made to use with UNIX operating system.
• Although C was designed for developing system software, but it is also widely used
for developing application software.
• It is used on many different software platforms and computer architectures.
• C has greatly influenced many other popular programming languages, most notably
C++, which originally was as an extension to C.
History of C Language:
• Dennis Ritchie developed this language in 1972 in AT & T Bell Laboratories.
• It was derived from B programming Language which was developed by Ken
Thompson in 1969-70. B was the basis for C Language.
• It was native Language of UNIX.
• System programs under UNIX were designed in C.
• The earlier version of C was known as K & R (Kernighan and Ritchie).
• ANSI (American national standard institute) developed a standard version of C called
ANSI C.
[Ch#8] Getting Started with C 199 Computer Science Part-II
Advantages of C Language:
• System Programming: C provides a powerful approach for system programming. It
was mainly designed to develop system software programs. Programmers can write
complex software programs more easily than any other low level language.
• Platform Independence: C is a machine independent language. It means that
programs written in C language can be executed on any type of computer hardware
with a little modification. For example, a C program written for Intel micro-processor
can be executed on a machine that uses AMD micro-processor.
• Small Language: C is a very small language. It requires limited space (only some
KB’s) on computer’s hard disk. It has only few reserve words. In spite of that, it is
very powerful for developing different types of programs.
• Fast Code Execution: The code written in C language executes very quickly.
Minimum code execution time is the major cause of using C language for big system
level software projects. Its compiler converts the source code into the object code in a
very short period of time.
• Flexible Codes: The programs written in C language are very flexible. A
programmer can easily change codes. Procedures and functions help the programmers
to utilize them for multiple purposes.
• Structured Programming: C provides structured programming approach. In
structured programming a program is divided into small units called modules. Each
module consists of different instructions to perform a particular task.
• Easy to Learn and Use: C is a very simple language. It is easy to learn and use.
Instructions of C have very simple syntax. So, a programmer needs not necessary to
have a detailed knowledge of computer architecture.
Q4. How to create, edit, save, compile and execute a C language program in
Turbo C++.
Ans.
There are six major steps to prepare a C program for execution. These steps are:
• Writing the source program.
• Saving the source code
• Compiling the source code
• Linking the object file
• Loading the object file
• Executing
[Ch#8] Getting Started with C 200 Computer Science Part-II
Following diagram helps us to understand the inter-connection of these steps:
The user write down the program
and save its as a source File

Source Revised
File Source File

The Compiler attempts to translate


User corrects syntax errors
the source program

List of Errors

New
Object File

The Linker Links the object file with Other


corresponding object files Object File

Executable
File

The Loader places the executable


file into the memory

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.

You might also like