You are on page 1of 51

COMPUTER SCIENCE

12

CHAPTER 8: Getting Started with C

Copyright @ IT Series www.itseries.com.pk


Topics

• Program and Programming languages


• Characteristics of High-Level Programming Languages
• Differentiate between Low-Level and High-Level Languages
• Source Code and Object Code
• Language Processors or Translators
• History of C Language
• Advantages or Characteristics of C Language
• Turbo C++
• Create, Edit , Compile and Execute C Program
• Steps to prepare C Program for Execution

Copyright @ IT Series www.itseries.com.pk


Topics(Cont.)
• .c and .cpp Extensions
• Process of setting te output and source directories
• Basic Structure of C Program
• Preprocessor Directives
• Header Files
• main Function
• C Statements
• Bug and Debug
• Types of Error ( Syntax errors, Logical Errors and Run-Time Errors
• Unstructured and Structured Programming Languages
• Debugging Features of Turbo C++

Copyright @ IT Series www.itseries.com.pk


 A well defined set of instructions given to the computer
A person who develops
• All tasks performed by a computer are controlled by computer program program is called
programmer.
• Written in programming languages

Examples of Programs:

• Microsoft Word

• Adobe Photoshop

• Skype

Copyright @ IT Series www.itseries.com.pk


Hi..

• A set of words and symbols used to write a computer program


• Provides way of communication between user and computer
• A computer program is written in a programming language
• Provides set of rules for writing the computer Programs called syntax
• The instructions must be written according to syntax of the language
• A large number of programming languages are available for writing programs
• Programmer selects a programming language according to the type of the program

scanf
printf
(), {}, “ “ ;

Copyright @ IT Series www.itseries.com.pk


Low Level Language
• Near to computer hardware and far from human languages
• Divided into two main categories
• Machine Language
• Assembly Language
Machine Language
• Instructions are written in binary form or code
• Directly understood by the computer
• Native language of computer
• Fast program execution
• No need of translator
• Machine dependent
Low Level
• Difficult to understand and learn Languages
• Very time consuming for writing and modifying program

Copyright @ IT Series www.itseries.com.pk


Assembly Language
• Also a low level language
• One step higher than machine language
• English like words are used instead of binary code
• English like words are known as mnemonics
• mnemonic is an abbreviation for an operation
• Examples of mnemonics:
• ADD
• MOV
• MUL
• Easier than machine language
• Mostly used to write system software
• The program written in assembly language cannot be executed directly on the computer
• Requires a translator named assembler

Copyright @ IT Series www.itseries.com.pk


High Level Languages
• Close to human languages
• Instructions are similar to English language such as input and print etc.
• Easy to understand
• Every High level language has its own set of rules
• Set of rules is called syntax
• Each instruction must follow syntax of the language
• Needs translator
• Separate translator for each language
• Translator indicates errors in instructions
• A program must be error free to be converted into machine code

Copyright @ IT Series www.itseries.com.pk


Examples of High Level Languages
• C/C++ : System & Application Software
• Java: Provides strong features for Networking Programming

• Pascal: Used for Scientific and Business Applications


• FORTRANN: FORmula TRANslation, very powerful Mathematical Capabilities
• BASIC: Beginners All Purpose Symbolic Instructions, mainly for students for solving simple problems
• COBOL: Common Business Oriented Language. Designed for business applications

Copyright @ IT Series www.itseries.com.pk


• Closer to human languages and far from machine language
Easy to Learn • English like languages and are easier to learn

• Easy to read and modify


Easy Error Detection • Easy to find errors in programs written in high level languages

• Syntax of High level langauges are standardized


• These languages describe well defined way of writing programs
Standardized Syntax • Different organizations determine standard syntax of these languagues
• ANSI(American National Standard Institute) is popular organization

• These languages do not require deep knowledge of hardware


Deep Hardware • A programmer can write efficient programs without a deep knowledge
of hardware
Knowledge not Required • He can concentrate on solving problems rather than concerning
hardware architecture

Copyright @ IT Series www.itseries.com.pk


• High Level Languages provide machine independence
• It means that the programs written in high level language can be
Machine Independence executed on different type of computer
• Example- Program written in C language program can be executed
on Intel & Motorola processors

• Programming in low level language is very difficult


More Programmer • But High level languages are easy to learn
• It encourages more people to learn these languages

• Programs written in high level languages are shorter than low level
languages
Shorter Programs • One instruction of high level language is equivalent to many
instructions of low level language

Copyright @ IT Series www.itseries.com.pk


Copyright @ IT Series www.itseries.com.pk
Source Code Translator(Compiler) Object Code

#include<stdio.h> 010101011010010110
#include<conio.h> 101010011101010101
void main() {
011101010101010010
printf(“I Love Pakistan”);
} 101110101010101101
010101010111011110

• High Level Language Instructions


• Also called source program • Machine Language Instructions
• Computer cannot understand source code • Also called object program or machine code
• Cannot be executed by the computer directly • Computer can understand it directly
• Need to be converted in object code

Copyright @ IT Series www.itseries.com.pk


• Type of system software that converts high level language or assembly language instructions
into machine language
• Types of language translator are:

Copyright @ IT Series www.itseries.com.pk


Compiler
• Program that converts the instruction of a high-level language into machine language as a whole
• Converts source code or program into machine or object code or program if it is free of errors
• Object program can be executed many times without translating it again
• A source code or program containing an error cannot be compiled
• The compiler generates error message to describe the cause of error
• All errors must be removed to successfully compile a source program
Examples
• C and C++ compilers Source Code Translator(Compiler) Object Code

#include<stdio.h> 010101011010010110
#include<conio.h> 101010011101010101
void main() {
011101010101010010
printf(“I Love Pakistan”);
} 101110101010101101
010101010111011110

Copyright @ IT Series www.itseries.com.pk


Interpreter

• Translate one statement of source code / program and executes it and then next statement is
translated and this goes on until end of the program

• If there is an error in the program statement, the interpreter stops working and displays the error
message

• Advantage over compiler is that an error is found immediately

• Does not execute the statement containing error

• Not very efficient

• It converts the instructions into machine program/code each time it is executed

• GWBASIC uses interpreter

Copyright @ IT Series www.itseries.com.pk


Assembler
• Program that translates the instructions of assembly language into machine language

Assembly Object Code


Assembler
Instructions

INC counter 0101010110100


MOV TOTAL, 48 1011010101001
ADD AH, BH 1110101110101
ADD MARKS, 10 0101011010101

Copyright @ IT Series www.itseries.com.pk


Compiler Interpreter
Convert the entire source code into machine code Converts one instruction into machine code at a time

Generates object code Does not generate object code

Convert high level program that can be executed Convert high level language program each time is
many times executed

Program execution is fast Program execution is slow

Displays syntax error after compiling the whole Display the syntax error on each instruction of the
program program

Copyright @ IT Series www.itseries.com.pk


 C is a popular general purpose language
 Developed by Dennis Ritchie at AT & T Bell Labs in 1972
 C language was derived from earlier programming language called B
 B was developed by Ken Thomson in 1969-70
 provided the basis for the development of C Dennis Ritchie

 C language was originally designed to write system program under Unix OS


 Earlier version of C was known as K & R (Kernighan and Ritchie)
 American National Standard Institute (ANSI) developed a standard
version of C language in late 1980s → ANSI C
 New version provided many features that were not available in the older
Version
Kernighan Ken Thomson

Copyright @ IT Series www.itseries.com.pk


1. Convenient Language

 Provides many facilities in easier way that are difficult to use in low-level languages

 Write complex programs more easily as compared to low-level languages

2. Well-Structured Language

 Syntax is very easy to use

 Program written in C language are easy to maintain and modify

Copyright @ IT Series www.itseries.com.pk


3. Machine Independence
 Program written in high-level language can be executed on any types of computers
 Example - A program written in C language can be executed on Intel and Motorola processors

Copyright @ IT Series www.itseries.com.pk


4. Modularity
 Breaking a large program into smaller units or modules
 Modules in C are called functions
 Modules are more manageable than one large program
 Modules can be independently implemented and tested
5. Case Sensitivity
 Differentiate uppercase and lowercase words
 Makes it easier to maintain the source code
 All keywords are written in lowercase e.g. while, for etc.

Copyright @ IT Series www.itseries.com.pk


6. Hardware Control
 Provides close control on hardware
 Used to write efficient programs to
control hardware components of computer system
7. Small Language
 Has small number of keywords and programming controls
 Very powerful language for developing different types of program
8. Fast Code Generation
 Compiler of C language generate very fast code.
 The code executes very efficiently → programs take less time to execute

Copyright @ IT Series www.itseries.com.pk


1. Easier to Write
 C is a high level language
 Instructions of C are similar to English language that are easy to understand
BUT
 Machine language is low-level language
 Program written in machine language consist of 0s and 1s
 Very difficult to write, modify and manage programs in machine language
2. Machine Independence
 Program written in C language can be executed on different types of computer
BUT
 Program written in machine language is machine-dependent
 It can only work on the computer it was designed for

Copyright @ IT Series www.itseries.com.pk


 Compiler used for C language is called Turbo C++
 Provides a complete Integrated Development Environment (IDE) known as TC editor
 Used to create, edit, save , compile and run C programs
 Provides a powerful debugger feature → detecting and removing errors in programs
 Type TC on DOS prompt and press Enter key to Start IDE
 The menu bar of IDE contains menus to
create, edit, compile, execute and debug C program
 The user can open menu by clicking it with the mouse.
 The user can also press a combination of ALT key and
the first highlighted character of the name of menu.
 For example, ALT+F is used to open File menu

Copyright @ IT Series www.itseries.com.pk


Creating and Editing a C program
 Process of Writing C program is known as Editing
 This process includes writing, modifying and deleting program statements
 Part of Turbo C++ IDE that is used to write C programs is called Edit Window
 A new edit windows can be opened by selecting File>New option from menu bar
 Edit has a double lined border
 Cursor blinks in the window
 Position of the cursor indicates the starting
point to write a program
 User can expand window by clicking the arrow
in the upper right corner

Copyright @ IT Series www.itseries.com.pk


Saving a C program
 Process of storing the program on disk
 A program should be saved on disk to be used repeatedly
 C programs are saved with .C extension
 Procedure to Save a C program Using Turbo C++
 Select File>Save. The Save As dialog box will appear
 Default name NONAME00.CPP appears
 Enter the file name
 Enter the path to save file
 Click OK. The program will be saved at specified location

Copyright @ IT Series www.itseries.com.pk


Compile a C program

 Process of converting source program into object program

 Converted program save with .obj extension

 Source program cannot be compiled if it contains any syntax error

 Compiler generates error message to describe the cause of error

 Procedure to Compile C program Using Turbo C++

 Select Compile>Compile

 Program will be translated into object program

 In case of error, compiler will generate error message

Copyright @ IT Series www.itseries.com.pk


Linking a C program
 Linking is process of linking library files with object program
 These files are used to perform different tasks such as input/output
 A library file must be linked with the object file before execution
 Linker is a program that combines the object program with additional library files
 Linker generates error message if the library file does not exist
 A new file is created with .exe extension if the process of linking is successful
 The linker can be started in Turbo C++ by selecting Compiler > Link from the menu bar
Executing a C Program
• Execution is the process of running an executable file
• The C program can be executed afer compiling and linking
• The program must be loaded into memory to execute
• In Turbo C++,
• The program can be loaded in the memory by selecting Run > Run from menu bar or pressing
CTRL+F9.
• The screen flickers for some time when a program is executed.
• The output screen displays the output of the program and disappears.
• The user can display the output screen by selecting Window > User Screen or pressing ALT+F5
Copyright @ IT Series www.itseries.com.pk
• First step is to create and edit a new program
Create and Edit a C Program
• Process is performed using text editor like Notepad etc.

• Process to store the program on disk


Saving a C Program
• Program should be saved on disk to be used repeatedly

• Process of converting source program to object program


Compiling a C Program
• Compiler converts the source program if its free of syntax error

• Process of linking library files with object program


Linking a C Program
• A library file must be linked with the object file before execution
• Process of placing an execution file into memory for execution
• A loader is a program that places an executable file in the
Loading a C Program memory
• Loader is part of Operating System

• Executing is the process of running an executable file


Executing a C Program • Instructions written in the program are executed by the
computer

Copyright @ IT Series www.itseries.com.pk


Copyright @ IT Series www.itseries.com.pk
.c Extension
 Compiler can compile the programs of C++ as well as C
 ANSI does not support extended features
 Compiler restricts a program to only standard features of C
if the source program is saved with .c extension
.cpp Extension
 C++ is an extension of C language
 Compiler can use many additional features if the source program is saved with .cpp extension

Copyright @ IT Series www.itseries.com.pk


 Turbo C++ stores object files and executable files in BIN subdirectory of TC directory
 It is not the right place for storing these files
 These files should be stored in the same directory in which source files are stored
 The user can change the setting for output and source directories
 The following procedure is used to set output and source directories:
 Select Options > Directories. A window will appear with four fields.
 Include Directories: It indicates the location of standard header files included in C
programs.
 It should already be set to drive:\TC\INCLUDE. The drive can be any drive on computer.
 Library Directories: It indicates the location of library files.
 It should already be set to drive:\TC\LIB. The drive can be any drive on the computer.
 Output Directories: It indicates the location where the compiler stores object files and
linker stores executable files.

Copyright @ IT Series www.itseries.com.pk


 Source Directories: It indicates the location of source code of the libraries that do not
belong to the open project

 Enter the path for output directory like "C:\MyPrograms" in Output Directory field.
 Enter the path for Source directory like "C:\MyPrograms" in Output Directory field.
 Click OK. The new setting will be applied

Copyright @ IT Series www.itseries.com.pk


 The format of writing C program
 The basic structure of a C program is very flexible
It consists of following parts:
 Preprocessor directive
 main() function
 Program body (C statements)
Example

Copyright @ IT Series www.itseries.com.pk


 Preprocessor Directive
• First line in a processor directive to include the header file stdio.h
• It gives instructions to C preprocessor
• Preprocessor is a program that modifies C source program before compilation
• Preprocessor directives starts with hash # symbol
 main() Function
• main () function is the place where execution of C program starts
• When a program executes, the control enters main () function and starts executing its
statements
 Program Body
• Statements are written in curly braces
• The braces are also known as delimiters
• These statements are collectively known as the body of program
• Every statement program is terminated with a semi colon;
• Compiler generates an error if any statement is not terminated by semicolon
Copyright @ IT Series www.itseries.com.pk
 Preprocessor Directive
• These are instructions given to compiler before execution of actual program
• Also known as compiler directive
• It is a part of C++ compiler
• Semicolon is not used at the end of preprocessor directive
• Preprocessor starts with # symbol
• include and define are preprocessor directives
• Preprocessor is used in C to include header files in the program

Copyright @ IT Series www.itseries.com.pk


 Include Preprocessor
• It enables a program to access library
• Each library contains different header files
• include preprocessor directive is used to include header files in program
 Syntax
• # include <Standard header file>
 Example
• #include <stdio.h>
• #include<math.h>
 Description
• The include directive tells the computer where to find the meaning of standard identifiers such as printf

• The meaning are described in standard header files

• Header files stdio.h contains information about standard input and output function such as scanf and printf

• The math.h contains information about common mathematical functions


Copyright @ IT Series www.itseries.com.pk
 Define Preprocessor
• It is used to define a constant
• It starts with # symbol
• It is not terminated with semicolon ;
• It can be written anywhere in the program
 Syntax
# define identifier value
# It indicates the start of preprocessor directive
define It is used to define a constant
identifier It is the name of the constant
value It represents the value associated with the identifier

 Example
# define PI 3.141593

Copyright @ IT Series www.itseries.com.pk


• It is collection of standard library functions to perform different tasks
• Standard library functions or simply C Library functions are built-in functions in C programming
• Each header file contains different types of predefined functions
• Many header files can be included in one program
• Header file must be included before calling
of its function in the program
• Extension of header file is .h
• The include preprocessor directive is used to include header files in program
• These files are provided by C compiler system
• Header files are normally stored in INCLUDE subdirectory
 Syntax
# include<Header file name> or # include “header_file_name”
 Example
# include<stdio.h>
#include<math.h>
#include<conio.h>
Copyright @ IT Series www.itseries.com.pk
• main() function is the place where the execution starts
• When the program is executed, the control enters main() function and starts its execution
• Each program contains main() function
• If a program does not contains main function, it can be compiled but not executed
• Statements are written in the body of main function
• Body is enclosed in curly braces { }
 Syntax
void main ()
{
Body of main function
}
 Explanation
• The definition of main function starts with keyword void
• Void means that the function will return no value
• Keyword void in parenthesis indicates that function does not accept any argument
• Body of main function is enclosed in braces
Copyright @ IT Series www.itseries.com.pk
• It is an instruction for the computer to perform a task
• Statements are written in curly brackets
• Computer performs these instructions one by one in the same sequence in which these
instructions are written
• Each statement in C is terminated with semicolon ;
 Example
#include <stdio.h>
void main()
{
printf("Hello World of C Programming.");
printf("Programming makes life interesting.");
}

Copyright @ IT Series www.itseries.com.pk


 Bug
• An error in a computer program
• Programmer can make different errors while writing programs
• A program cannot be compiled if it contains any syntax error
• The compiler detects and displays error message to describe the cause of error

 Debugging
• Process of finding and removing
• The errors msut be removed from the program before it can be
compiled and executed

Copyright @ IT Series www.itseries.com.pk


 Syntax Errors
• A type of error that occurs when an invalid statement is written in the program
• Compiler detects syntax errors
• A program containing syntax errors cannot be compiled successfully
Syntax is a collection of
 Causes of Syntax Erros rules for writing programs
• The statement terminator is missing at the end of statement in any language
• A misspelled keyword is used in the program
• Any of the delimiters is missing
Example
Typing forr instead of for is an example
 Logical Errors
• A type of error that occurs due to poor logic of the programmer
• A statement with logical error may produce unexpected and wrong results in program
• Difficult to find because translator cannot detect
• It can only be detected by examining the program thoroughly
Example
• Using wrong conditions in program such as writing a<5 instead of a>5
• Using wrong formula as writing average=total *5 instead of average=total/5
Copyright @ IT Series www.itseries.com.pk
 Run Time Errors
• A type of error that occurs during the execution of program
• It occurs when a statement directs the computer to execute an illegal operation
• These are detected and displayed by the computer during execution
• Normally occur due to wrong input from the user
• Computer stops executing the program and displays error message if a run time error occurs
Example
• Dividing a number by zero
• User may enter wrong type of data
• User may ask the program to open a file does not exist

Copyright @ IT Series www.itseries.com.pk


It cannot be detected by the compiler

It does not crash the program

The user needs to review the whole program to find out logical error

It may take a lot of time for detecting logical error

Copyright @ IT Series www.itseries.com.pk


 Unstructured Programming Language
• Entire logic of the program is implemented in a single module or function
• Program written in these languages are:
• Error prone
• Difficult to understand
• Difficult to modify and debug

Copyright @ IT Series www.itseries.com.pk


 Structured Programming Language
• The entire logic of the program is divided into number of
smaller module or functions
• Functions are like building blocks

• Each module is a piece of code that implements a different functionality


• Main module calls other modules when thy are needed to execute
• It is a modular method of writing programs
• It is an easy and simple technique to write program
• Programs written in these languages are easy to understand,
modify and debug
• There is less possibility of errors in the programs
Copyright @ IT Series www.itseries.com.pk
• Debugger provides the facility to find errors by executing one line of program at one time
Single • Enables the programmer to detect the exact place of error
Stepping • Procedure to execute one step at a time with single stepping
• Select Run > Trace Into OR press F7

• Used to check the value of variable as the program executes


• Indicates how the values of variables change during program execution
• Normally used with single stepping
• Place the cursor on the variable whose value is to be checked.
• Select Debug > Watches. A submenu will appear.
Watches • Select Add Watch from the submenu.
• The selected variable will appear in Watch expression field.
• Click OK OR press Enter.
• A new window will appear indicating that the selected symbol is undefined.
• Select Run > Trace Into OR press F7 to execute single stepping.
• The value of the selected variable will appear in Watch window.

Copyright @ IT Series www.itseries.com.pk


• Used to mark a part of program where program execution will stop
• Program executes all statements up to the breakpoint and then stops
• User can check the values of variable at this point by using Watch window by single
Breakpoints stepping the remaining part of the program
• Place the cursor on the line on which the breakpoint is to be inserted.
• Select Debug > Toggle breakpoint OR press CTRL+F8.
• The breakpoint will be inserted and the line will be highlighted.

• Used to change the value of variable during program execution


• Can be useful if the user is single stepping the program and wants to change the
value of certain variable
Evaluate/Modify • Select Debug > Evaluate/Modify.
Window • A new window will appear with three fields.
• Enter the name of the variable whose value is to be modified in Expression field.
• Enter the new value for the variable in New Value field.
• The value of the third field Result will also change automatically.

Copyright @ IT Series www.itseries.com.pk


Copyright @ IT Series www.itseries.com.pk

You might also like