Computer Science ICS P 2
Chapter 8 - Getting Started with C
What is a Program?
● A well defined set of instructions given to the computer.
● All tasks performed by a computer are controlled by a computer program.
● Written in programming languages.
Examples: • Microsoft Word • Adobe Photoshop • Skype
Programmer: A person who develops a program is known as Programmer.
History of C Language
● 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
AU
● provided the basis for the development of C
● 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
Characteristics of C Language
1. Convenient Language
● Provides many facilities in easier ways that are difficult to use in low-level languages
● Write complex programs more easily as compared to low-level languages
N
2. Well-Structured Language
● Syntax is very easy to use
● Program written in C language are easy to maintain and modify
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
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
1
Computer Science ICS P 2
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.
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
● Compilers of C language generate very fast code.
AU
● The code executes very efficiently → programs take less time to execute
Why do we Write Program in C language rather than Machine Language?
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
N
● 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
TURBO C++
● 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
2
Computer Science ICS P 2
● The menu bar of IDE contains menus to create, edit, compile, execute and debug C
program
● The user can open the menu by clicking it with the mouse.
● The user can also press a combination of the ALT key and the first highlighted character
of the name of the menu.
● For example, ALT+F is used to open File menu
AU
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
N
● Edit has a double lined border
● Cursor blinks in the window
● Position of the cursor indicates the starting point to write a program
3
Computer Science ICS P 2
● User can expand window by clicking the arrow in the upper right corner
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
AU
N
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
4
Computer Science ICS P 2
● Program will be translated into object program
● In case of error, compiler will generate error message
Linking a C program
● Linking is process of linking library files with object program
●
●
●
●
●
●
AU
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 after compiling and linking
● The program must be loaded into memory to execute
● In Turbo C++,
N
○ The program can be loaded in the memory by selecting Run > Run from the
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
5
Computer Science ICS P 2
Necessary Steps to Prepare a Program for Execution
AU
N