Lesson-1 Introduction to C Language

1.1 INTRODUCTION

C is a powerful, flexible, portable and structured programming language. Since it allows you to develop programs using well-defined control structures and provides modularity (breaking the task into multiple sub tasks that are simple enough to understand and to reuse). C is often called a middle-level language because it combines the best elements of low-level or machine language with high-level languages. Middle-level implies that it is neither high level (i.e. using easy to use English words and structures that are hard to understand by the machine but easy to understand by the programmer) nor low level (i.e. using obscure assembly language that describes how the computer does its processing at the hardware [chip] level, but works very fast due to it being written using a language the machine understands more readily than humans writing programs).

1.2 HISTORY OF C LANGUAGE

C was developed by Dennis Ritchie at Bell Laboratories in 1972. Most of its principles and ideas were taken from the earlier language B, BCPL and CPL. CPL was developed jointly between the Mathematical Laboratory at the University of Cambridge and the University of London Computer Unit in 1960s. CPL (Combined Programming Language) was developed with the purpose of creating a language that was capable of both machine independent programming and would allow the programmer to control the behavior of individual bits of information. But the CPL was too large for use in many applications. In 1967, BCPL (Basic Combined Programming Language) was created as a scaled down version of CPL while still retaining its basic features. This process was continued by Ken Thompson. He made B Language during working at Bell Labs. B Language was a scaled down version of BCPL. B Language was written for the systems programming. In 1972, a co-worker of Ken Thompson, Dennis Ritchie developed C Language by taking some of the generality found in BCPL to the B language.


The original PDP-11 version of the Unix system was developed in assembly language. In 1973, C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly.


During the rest of the 1970's, C spread throughout many colleges and universities because of its close ties to UNIX and the availability of C compilers. Soon, many different organizations began using their own versions of C Language. This was causing great compatibility problems. In 1983, the American National Standards Institute (ANSI) formed a committee to establish a standard definition of C Language. That is known as ANSI Standard C. Today C is the most widely used System Programming Language.

1.3 LEVEL OF PROGRAMMING LANGUAGES

There are three different levels of programming languages. They are  

  • Machine languages (low level)

  • Assembly languages 

  • Procedure Oriented languages (high level)

1.3.1 Machine Languages:-

Computers are made up of number of electronic components and they all are two – state electronic components means they understand only the 0 – (pulse) and 1 (non – pulse). Therefore the instruction given to the computer must be written using binary numbers i.e. 1 and 0.  Computers do not understand English or any other language but they only understand or respond to binary numbers (0 and 1). So each computer has its own Machine languages.

1.3.2 Assembly Languages:-

As it was very tedious to understand and remember 0’s representing numerous data and instruction. So to resolve this problem mnemonics codes were developed. For example add is used as symbolic code to represent addition. SUB is used for subtraction. They are the symbolic representation of certain combination of binary numbers  for example  S U B Which represents certain actions as computer understand only Machine language instructions a program written in Assembly Language must be translated into Machine languages. Before it can be executed this translation if done by another program called assembler. This assembler will translate mnemonic codes into Machine languages.

1.3.3 Procedure Oriented Languages 

In earlier assembly languages assembler programs produced only one Machine languages instruction for every assembly languages instruction. So to resolve this problem now assembler was introduced. It can produce several machine level instructions for one assembly language instruction. Thus programmer was relieved from the task of writing and instruction for every machine operation performed. These languages contain set of words and symbols and one can write program with the combination of these keywords. These languages are also called high level languages. Basic, FORTRAN, Pascal and COBOL. The most important characteristic of high level languages is that it is machine independent and a program written in high level languages can be run on any computers with different architecture with no modification or very little modification.

1.4 LANGUAGE TRANSLATORS

As we know that computer understands only the instruction written in the Machine language. Therefore a program written in any other language should be translated to Machine language. For these purpose special programs are available they are called translators or language processors. These special programs accept the user program and check each statement and produce a corresponding set of Machine language instructions. There are two types of Translators.

1.4.1 Compiler:-

A Compiler checks the entire program written by user and if it is free from error and mistakes then produces a complete program in Machine language known as object program. But if it founds some error in program then it does not execute the single statement of the program. So compiler translate whole program in Machine language before it starts execution.

1.4.2 Interpreters:-

Interpreters perform the similar job like compiler but in different way. It translates (Interprets) one statement at a time and if it is error – free then executes that statement. This continues till the last statement in the program has been translated and executed.  Thus Interpreter translates and executes the statement before it goes to next statement. When it founds some error in statement it will immediately stop the execution of the program.    Since compiler of Interpreter can translate only a particular language for which it is designed one has to use different compiler for different languages. 

1.5 Difference between Compiler – Interpreter:-

Error finding is much easier in Interpreter because it checks and executes each statement at a time. So wherever it finds some error it will stop the execution. Where Compiler first checks all the statement for error and provide lists of all the errors in the program.  Interpreter take more time for the execution of a program compared to Compilers because it translates and executes each statement one by one.

1.6 FEATURES OF C LANGUAGE  

There are several reasons why many computer professionals feel that C is at the top of the list:

1.6.1 FLEXIBILITY

C is a powerful and flexible language. C is used for projects as diverse as operating system, word processor, graphics, spreadsheets and even compilers for other language. C is a popular language preferred by professional programmers. As a result, a wide variety of C compilers and helpful accessories are available.

1.6.2 PORTABILITY

C is a portable language. Portable means that a C program written for one computer system can be run on another system with little or no modification. Portability is enhanced by the ANSI standard by C, the set of rules for C compilers.

1.6.3 COMPACTNESS

C is a language of few words, containing only a handful terms, called keywords, which serve as the base on which the language’s functionality is built. You might think that a language with more keyword would be more powerful. This isn’t true. As you will find that it can be programmed to do any task.

1.6.4 REUSABILITY:

C is modular, C code can and should be written in routine called functions. These functions can be reused in other applications or programs. By passing pieces of information to the function, you can create useful, reusable code.

1.7 BASIC STRUCTURE OF C PROGRAM 

C program can be viewed as group of building blocks called functions. A function is a subroutine that may include one or more statement designed to perform a specific task. C program may contain one or more section as shown below:

Documentation Section

Link Section

Definition Section

Global declaration Section

Main() function section

{

Declaration Part

Executable Part

}

Subprogram Section

Function1 ()

{

 

}

Function2 ()

{

 

}

1.7.1 Documentation Section:

This section contains set of comments lines consist of details like program name, author name and purpose or functionality of the program.

1.7.2 Link Section:

This section consists of instructions to be given to the compiler to link functions from the system library. For example if you want to use some mathematical function then you have to define link for math.h file in link section. For Example

# include<stdio.h> 

# include<math.h>

1.7.3 Definition Section: 

This section defines all the symbolic constants. For example PI=3.14. By defining symbolic constant one can use these symbolic constant instead of constant value. 

# define PI 3.14

# define Temp 35

1.7.4 Global Declaration Section:

This section contains the declaration of variables which are used by more than one function of the program.

1.7.5 Main Function Section:

A main() function is a heart of any ‘C’ language program. Any C program is made up of 1 or more then 1 function. If there is only 1 function in the program then it must be the main program. An execution of any C program starts with main() function.

1.7.6 Subprogram or Sub Function Section:

They are the code sections which are define outside the boundary of main function. This function can be called from any point or anywhere from the program. Generally they are defining to solve some frequent tasks.

1.8 An Example of C Program

/* This program prints a one-line message */

#include <stdio.h>

void main()

{

printf("Hello World\n");

}

/* This program ... */        The symbols /* and */ delimit a comment. Comments are ignored by the compiler, and are used to provide useful information for humans that will read the program.

 

void                          void keyword indicates that the main function is not going to return any value. More about function and return type in chapter 8. User defined function.

 

main()                        C programs consist of one or more functions. One and only one of these functions must be called main. The brackets following the word main indicate that it is a function and not a variable.

 

{ }                           braces surround the body of the function, which consists of one or more instructions (statements).

 

printf()                      is a library function that is used to print on the standard output  stream usually the screen.

 

"Hello World\n"        is a string constant.

 

\n                            is the newline character.

 

;                             a semicolon terminates a statement.

C is case sensitive, so the names of the functions ( main and printf ) must be typed in lower case as above.

With a few exceptions, any amount of white space (spaces, tabs and newlines) can be used to make your programs more readable. There are many conventions for program layout, just choose one that suits you, or alternatively get a program to format your code for you (such as the indent program).

Last modified: Friday, 25 October 2013, 6:14 AM