Introduction
to C
C is one of the most
popular general-purpose programming languages. C language has been designed and
developed by Dennis Ritchie at AT&T Bell Laboratories, USA, in 1972.
Several important concepts of C are drawn from ‘Basic Combined Programming
Language (BPCL)’ and ‘B’ language. Martin Richards developed BCPL in 1967. The
impact of BCPL on C is observed indirectly through the language B, which was
developed by Ken Thompson in 1970. C has
an extensive set of capabilities and is a true general-purpose programming
language. As such, it can be used for creating simple, interactive programs or
highly sophisticated and complex engineering and scientific programs, within
the context of truly structured language.
Why
Use C?
In today’s world of
computer programming, there are many high-level languages to choose from, such
as C, C++, Java, Python and Ruby. These are all excellent languages suited for
most programming task. Even so, there are several reasons why C is at the top
of the list:
i. The C language is not tied to any
particular operating system. It can be used to develop new operating system.
Major parts of popular operating systems like Windows, UNIX, Linux is still
written in C. This is because even today when it comes to performance (speed of
execution) nothing beats C. Moreover, if one is to extend the operating system
to work with new device one needs to write device driver program which are
exclusively written in C.
ii. C is the base of most high-level and
Object Oriented Programming Language such as C++, Java, C# and Objective C and
it is hard to learn C++ or Java directly without learning C.
iii. Mobile device and embedded system have
limited amount of memory. Considering this constrains C is a very good choice
for building operating system and programs for such device for its efficiency
and faster execution.
iv. At times one is required to very closely
interact with the hardware device. Since C provides several language elements
that make this interaction feasible without compromising the performance it is
the preferred choice of the programmer.
v. C is a structural language. Structured
language facilitates the development of a variety of programs in small modules
or blocks. It is easy for writing, testing, debugging and maintenance with
structured programming.
vi. C programs are efficient, fast and highly
portable, i.e. C programs written on one computer can run on another with small
or almost no modification.
vii. C is a free-form language. Multiple
statements can be on the same line or can continue over multiple lines. White
spaces (i.e. tab space or space bar) are ignored.
As these features show,
C is an excellent choice for your first programming language. In learning C,
you are not only learning one of today’s most powerful and popular programming
languages, but you are also preparing yourself for object-oriented programming.
C
is a middle-level language
C is often called a
middle-level computer language. This does not mean that C is less powerful,
harder to use, or less developed than a high-level language nor does it imply
that C has the cumbersome nature of assembly language. Rather, C is thought of
as a middle-level language because it combines the best elements of high-level
language with the control and flexibility of assembly languages. In C, one can develop
a program first and execute fast. It reduces the gap between high-level and
low-level language; that is why it is known as a middle-level language.
C
is a case-sensitive language
C is a case-sensitive
language means that compiler distinguishes uppercase and lowercase
letters. Thus, in C the word main, Main
and MAIN represent three distinct and different names. For this reason we
should type all names in the same case.
Structure
of a C program
Basic structure of a C
program contains the following sections:
i. Preprocessor Statements: The
preprocessor statements begin with # (pound) symbol and are also called
preprocessor directive. These statements instruct the compiler to include C
preprocessor such as header files and symbolic constants before compiling the
program. It is optional means a C program may not contain any preprocessor
statements, but generally every C program depends upon some header files for
function definition used in the program and for this contains one or more
preprocessor statements. Each header file has extension ‘.h’. Header files
contain the definitions of C library functions.
ii. Global Declaration: This section
declares some variables that are used in more than one function. These
variables are known as global variables and are declared outside any function
including main ( ). Global variables can be accessed by all the used define
function as well as main ( ) function. Global declaration is also optional and
a C program may or may not contain this.
iii. The main ( ) function: Each and every C
program must contain main ( ) function. Every C program starts execution from
main ( ) function. No C program is executed without main ( ) function. The main
( ) function should be written in small (lowercase) letters.
iv. Braces: Every C program must have a pair
of curly braces ({, }) and must start immediately after the closing parenthesis
of main ( ) function. The left brace (opening brace) indicates the beginning of
the definition of main ( ) function and right brace (closing brace) indicates
the end of the main ( ) function. Between these two braces, the program should
have declaration and executable statements. These braces can also be used to
specify the beginning and ending of user-define function. These two braces can
also be used in compound statements.
v. Local Declaration: Some variables are
declared inside a function and are called local variable. A local variable can
only be accessed from a function where it is declared. This portion is optional
for a C program.
vi. Program
Statements: These statements are building blocks of a program. They represent
the instruction for the computer to perform any specific task (operation).This
portion may contain one or more input output statements, arithmetic statements,
decision statements, control statements or assignment statements. Each
statement should be terminated with a semicolon.
vii. User-define
Function: The functions defined by the user (programmer) are called user-define
function. These are subprograms and perform user specific task. They may be
written before or after the main ( ) function and generally called within main
( ) function. Unlike main ( ) function user-define function is optional to the
programmer.
viii. Comments: Comments are not necessary in
a program. However, to make the program understandable programmer can insert
comments in the program. Compiler does not compiler comments but it is useful
for documentation. The clarity of the program can be amplified if it is
properly documented.
Comments are statements that give us
information about the program or logics of program. Comments can be single line
or multiline. Single line comments starts with the delimiters ‘//’ and
terminate with the end of the line. Multiline comments are to be placed between
the delimiters ‘/*’ and ‘*/’. Multiline comments can be nested. Comments are
not a part of executable statements and compiler does not execute comments.
But, for enhancing the readability of the program programmer should use
comments and a good programmer frequently do it.
Advantages
of C
i.
C contains a powerful data definition.
The data types supported are characters, alphanumeric, integers, long integers,
floats and double. It also supports string manipulation in the form of
character array.
ii.
C supports a powerful set of operators.
iii.
It also supports powerful graphics
programming and directly operates with hardware. Execution of the program is
faster.
iv.
An assembly code can also inserted into
C program.
v.
C programs are highly portable on any
type of OS platforms.
vi.
System programs such as compiles,
operating systems and device driver can be developed in C. For example
operating system UNIX is developed in C.
vii.
The C language has 32 keywords and about
145 library functions and near about 30 header files in ANSI C.
viii.
C works closely with machine and matches
assembly language in many ways.
ix.
The programs written in C language are
efficient, fast and easy to understand.
x.
Another important advantage of C is its
ability to extend itself. We can easily add our own functions in standard C
library.
xi.
C language is a structured programming
language. This makes user to think of a problem in terms of function modules or
blocks. Collections of these modules make a complete program. This modular
structure makes program debugging, testing and maintenance easier.