Computer Programming – I C-Programming
Chapter-1
Introduction to C Programming
Objectives
This section is designed to give you a general overview of the C programming
language. Although much of this section will be expanded in later sections it gives
you a taste of what is to come.
Why use C?
C has been used successfully for every type of programming problem imaginable
from operating systems to spreadsheets to expert systems - and efficient compilers
are available for machines ranging in power from the Apple Macintosh to the Cray
supercomputers. The largest measure of C's success seems to be based on purely
practical considerations:
• The portability of the compiler
• The standard library concepts.
• A powerful and varied repertoire of operators
• An elegant syntax
• Ready access to the hardware when needed
• The ease with which applications can be optimised by hand-coding isolated
procedures
C is often called a "Middle Level" programming language. This is not a reflection
on its lack of programming power but more a reflection on its capability to access
the system's low-level functions. Most high-level languages (e.g., Fortran) provides
everything the programmer might want to do already built into the language. A low-
level language (e.g., assembler) provides nothing other than access to the
machines basic instruction set. A middle level language, such as C, probably
doesn't supply all the constructs found in high-languages - but it provides you with
all the building blocks that you will need to produce the results you want!
Uses of C
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
,Computer Programming – I C-Programming
C was initially used for system development work, in particular the programs that
make-up the operating system. Why use C? Mainly because it produces code that
runs nearly as fast as code written in assembly language. Some examples of the
use of C might be:
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Data Bases
• Language Interpreters
• Utilities
In recent years C has been used as a general-purpose language because of its
popularity with programmers. It is not the world's easiest language to learn and
you will certainly benefit if you are not learning C as your first programming
language! C is trendy, many well established programmers are switching to C for
all sorts of reasons, but mainly because of the portability that writing standard C
programs can offer.
A Brief History of C
C is a general-purpose language which has been closely associated with the Unix
operating system for which it was developed - since the system and most of the
programs that run it are written in C.
Many of the important ideas of C stem from the language BCPL, developed by
Martin Richards. The influence of BCPL on C proceeded indirectly through the
language B, which was written by Ken Thompson in 1970 at Bell Labs, for the first
UNIX system on a DEC PDP-7. BCPL and B are "type less" languages whereas
C provides a variety of data types.
In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the
computing world.
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
,Computer Programming – I C-Programming
In 1983, the American National Standards Institute (ANSI) established a committee
to provide a modern, comprehensive definition of C. The resulting definition, the
ANSI standard, or "ANSI C", was completed late 1988.
C for Personal Computers
With regards to personal computers Microsoft C for IBM (or clones) PC's. and
Borlands C are seen to be the two most commonly used systems. However, the
latest version of Microsoft C is now considered to be the most powerful and efficient
C compiler for personal computers.
Running c Program
This section is primarily aimed at the beginner who as no or little experience of
using compiled languages. I cover the various stages of program development.
The basic principles of this section will apply to what ever C compiler you choose
to use, the stages are nearly always the same
The Edit-Compile-Link-Execute Process
Developing a program in a compiled language such as C requires at least four
steps:
1. editing (or writing) the program
2. compiling it
3. linking it
4. executing it
Editing
You write a computer program with words and symbols that are understandable to
human beings. This is the editing part of the development cycle. You type the
program directly into a window on the screen and save the resulting text as a
separate file. This is often referred to as the source file (you can read it with the
TYPE command in DOS or the cat command in unix). The custom is that the text of
a C program is stored in a file with the extension .c for C programming language
Compiling
You cannot directly execute the source file. To run on any computer system, the
source file must be translated into binary numbers understandable to the
computer's Central Processing Unit (for example, the 8088 microprocessor). This
process produces an intermediate object file - with the extension .obj, the .obj
stands for Object.
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
, Computer Programming – I C-Programming
Linking
The first question that comes to most people’s minds is Why is linking necessary?
The main reason is that many compiled languages come with library routines which
can be added to your program. Theses routines are written by the manufacturer of
the compiler to perform a variety of tasks, from input/output to complicated
mathematical functions. In the case of C the standard input and output functions
are contained in a library (stdio.h) so even the most basic program will require a
library function. After linking the file extension is .exe which are executable files.
Executable files
Thus, the text editor produces .c source files, which go to the compiler, which
produces .obj object files, which go to the linker, which produces .exe executable
file. You can then run .exe files as you can other applications, simply by typing
their names at the DOS prompt or run using windows menu.
Using Microsoft C
Edit stage:
Type program in using one of the Microsoft Windows editing packages.
Compile and link:
Select Building from Make menu. Building option allows you to both
compile and link in the same option.
Execute:
Use the Run menu and select Go option.
Errors:
First error highlighted. Use Next Error from Search menu for further
errors if applicable.
If you get an error message, or you find that the program doesn't work when you
finally run it (at least not in the way you anticipated) you will have to go back to the
source file - the .c file - to make changes and go through the whole development
process again!
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
Chapter-1
Introduction to C Programming
Objectives
This section is designed to give you a general overview of the C programming
language. Although much of this section will be expanded in later sections it gives
you a taste of what is to come.
Why use C?
C has been used successfully for every type of programming problem imaginable
from operating systems to spreadsheets to expert systems - and efficient compilers
are available for machines ranging in power from the Apple Macintosh to the Cray
supercomputers. The largest measure of C's success seems to be based on purely
practical considerations:
• The portability of the compiler
• The standard library concepts.
• A powerful and varied repertoire of operators
• An elegant syntax
• Ready access to the hardware when needed
• The ease with which applications can be optimised by hand-coding isolated
procedures
C is often called a "Middle Level" programming language. This is not a reflection
on its lack of programming power but more a reflection on its capability to access
the system's low-level functions. Most high-level languages (e.g., Fortran) provides
everything the programmer might want to do already built into the language. A low-
level language (e.g., assembler) provides nothing other than access to the
machines basic instruction set. A middle level language, such as C, probably
doesn't supply all the constructs found in high-languages - but it provides you with
all the building blocks that you will need to produce the results you want!
Uses of C
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
,Computer Programming – I C-Programming
C was initially used for system development work, in particular the programs that
make-up the operating system. Why use C? Mainly because it produces code that
runs nearly as fast as code written in assembly language. Some examples of the
use of C might be:
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Data Bases
• Language Interpreters
• Utilities
In recent years C has been used as a general-purpose language because of its
popularity with programmers. It is not the world's easiest language to learn and
you will certainly benefit if you are not learning C as your first programming
language! C is trendy, many well established programmers are switching to C for
all sorts of reasons, but mainly because of the portability that writing standard C
programs can offer.
A Brief History of C
C is a general-purpose language which has been closely associated with the Unix
operating system for which it was developed - since the system and most of the
programs that run it are written in C.
Many of the important ideas of C stem from the language BCPL, developed by
Martin Richards. The influence of BCPL on C proceeded indirectly through the
language B, which was written by Ken Thompson in 1970 at Bell Labs, for the first
UNIX system on a DEC PDP-7. BCPL and B are "type less" languages whereas
C provides a variety of data types.
In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the
computing world.
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
,Computer Programming – I C-Programming
In 1983, the American National Standards Institute (ANSI) established a committee
to provide a modern, comprehensive definition of C. The resulting definition, the
ANSI standard, or "ANSI C", was completed late 1988.
C for Personal Computers
With regards to personal computers Microsoft C for IBM (or clones) PC's. and
Borlands C are seen to be the two most commonly used systems. However, the
latest version of Microsoft C is now considered to be the most powerful and efficient
C compiler for personal computers.
Running c Program
This section is primarily aimed at the beginner who as no or little experience of
using compiled languages. I cover the various stages of program development.
The basic principles of this section will apply to what ever C compiler you choose
to use, the stages are nearly always the same
The Edit-Compile-Link-Execute Process
Developing a program in a compiled language such as C requires at least four
steps:
1. editing (or writing) the program
2. compiling it
3. linking it
4. executing it
Editing
You write a computer program with words and symbols that are understandable to
human beings. This is the editing part of the development cycle. You type the
program directly into a window on the screen and save the resulting text as a
separate file. This is often referred to as the source file (you can read it with the
TYPE command in DOS or the cat command in unix). The custom is that the text of
a C program is stored in a file with the extension .c for C programming language
Compiling
You cannot directly execute the source file. To run on any computer system, the
source file must be translated into binary numbers understandable to the
computer's Central Processing Unit (for example, the 8088 microprocessor). This
process produces an intermediate object file - with the extension .obj, the .obj
stands for Object.
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD
, Computer Programming – I C-Programming
Linking
The first question that comes to most people’s minds is Why is linking necessary?
The main reason is that many compiled languages come with library routines which
can be added to your program. Theses routines are written by the manufacturer of
the compiler to perform a variety of tasks, from input/output to complicated
mathematical functions. In the case of C the standard input and output functions
are contained in a library (stdio.h) so even the most basic program will require a
library function. After linking the file extension is .exe which are executable files.
Executable files
Thus, the text editor produces .c source files, which go to the compiler, which
produces .obj object files, which go to the linker, which produces .exe executable
file. You can then run .exe files as you can other applications, simply by typing
their names at the DOS prompt or run using windows menu.
Using Microsoft C
Edit stage:
Type program in using one of the Microsoft Windows editing packages.
Compile and link:
Select Building from Make menu. Building option allows you to both
compile and link in the same option.
Execute:
Use the Run menu and select Go option.
Errors:
First error highlighted. Use Next Error from Search menu for further
errors if applicable.
If you get an error message, or you find that the program doesn't work when you
finally run it (at least not in the way you anticipated) you will have to go back to the
source file - the .c file - to make changes and go through the whole development
process again!
Dr.Sachin Bojewar (M) 9323232781
M.Tech (Comp, PhD