history of c programming language by 12play

VIEWS: 561 PAGES: 12

									                                                                                            Chapter 1

    Introduction - C Programming

           Introduction
           History of C
           Characteristics of C
           The form of a C program
           A First Example
           Steps in Executing a C program
           Structure of a C Program
           Brief Introduction to Hardware
           Brief Introduction to Software
           Q&A, Example Programs
           Exercises.


    As a programming language, C is rather like Perl or Java. Values are stored in variables.
    Programs are structured by defining and calling functions. Program flow is controlled using loops,
    if statements and function calls. Input and output can be directed to the terminal or to files.
    Related     data     can     be      stored      together     in     arrays     or     structures.

    Of the three languages, C allows the most precise control of input and output. C is also rather
    more terse than Java or Perl. This can result in short efficient programs, where the programmer
    has made wise use of C's range of powerful operators. It also allows the programmer to produce
    programs           which            are            impossible            to          understand.

    Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct
    term) will welcome the ease of use compared with some other languages. Undisciplined use of
    pointers can lead to errors which are very hard to trace. This course only deals with the simplest
    applications                                      of                                      pointers.

    It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using
    C. Many of the extra facilities which it offers can lead to extra types of programming error. You
    will have to learn to deal with these to successfully make the transition to being a C programmer.

    C is a remarkable language. Designed by, Dennis Ritchie, working at AT&T Bell Laboratories in
    New Jersey, USA, in early 1970s. It has increased in use until now it may well be one of the most
    widely-written computer languages in the world. The success of C is due to a number of reasons,
    none of them key, but all of them important. Perhaps the most significant of all is that C was
    developed by real practitioners of programming and was designed for practical day-to-day use,
    not for show or for demonstration. Like any well-designed tool, it falls easily to the hand and feels

good to use. Instead of providing constraints, checks and rigorous boundaries, it concentrates on
providing you with power and on not getting in your way.

C is referred to as a “high-level programming language”. A programming language is a tool,
and no tool can perform every task unaided. C imposes relatively few built-in ways of doing things
on the programmer. Some common tasks, such as manipulating strings, allocating memory, and
doing input/output (I/O), are performed by calling on library functions. Other tasks which you
might want to do, such as creating or listing directories, or interacting with a mouse, or displaying
windows or other user-interface elements, or doing color graphics, are not defined by the C
language at all. You can do these things from a C program, of course, but user will be calling on
services which are unusual for the programming environment (compiler, processor, and operating
system) and which are not defined by the C standard.

History of C

The milestones in C's development as a language are listed below:

       UNIX developed c. 1969 -- DEC PDP-7 Assembly Language
       BCPL -- a user friendly OS providing powerful development tools developed
        from BCPL. Assembler tedious long and error prone.
       A new language “B” a second attempt. c. 1970.
       A totally new language “C” a successor to “B”. c. 1971
       By 1973 UNIX OS almost totally written in “C”.

Characteristics of C

Briefly list some of C's characteristics that define the language and also have lead to its popularity
as a programming language.

       Small size
       Extensive use of function calls
       Loose typing -- unlike PASCAL
       Structured language
       Low level (BitWise) programming readily available
       Pointer implementation - extensive use of pointers for memory, array, structures and

C has now become a widely used professional language for various reasons.

       It has high-level constructs.
       It can handle low-level activities.
       It produces efficient programs.
       It can be compiled on a variety of computers.

The Form of a C Program

C is a block-structured programming language. If the user experience lies in the FORTRAN &
PASCAL, then will find it closer to what already know, but the inner level will look quite different.
C has borrowed from both kinds of language, and from a lot of other places too. The input from so
many varied sources has spawned a language a bit like a cross-bred terrier: inelegant in places,
but a tenacious brute that the family is fond of.

At the coarsest level, an obvious feature is the multi-file structure of a program. The language
permits separate compilation, where the parts of a complete program can be kept in one or more
source files and compiled independently of each other. The idea is that the compilation process
will produce files that can then be linked together using whatever link editor or loader that your
system provides. The block structure of the Algol-like languages makes this harder by insisting
that the whole program comes in one chunk, although there are usually ways of getting around it.

The reason for C's approach is historical and more interesting. It is supposed to speed things up:
the idea is that compiling a program into relocatable object code is slow and expensive in terms
of resources; compiling is hard work. Using the loader to bind together a number of object code
modules should simply be a matter of sorting out the absolute addresses of each item in the
modules when combined into a complete program. This should be relatively inexpensive. The
expansion of the idea to arrange for the loader to scan libraries of object modules, and select the
ones that are needed, is an obvious one. The benefit is that if you change one small part of a
program then the expense of recompiling all of it may be avoided; only the module that was
affected has to be recompiled.

All, the same, it's true that the more work put on to the loader, the slower it becomes, in fact
sometimes it can be the slowest and most resource consuming part of the whole procedure. It is
possible that, for some systems, it would be quicker to recompile everything in one go than to
have to use the loader: Ada has sometimes been quoted as an example of this effect occurring.
For C, the work that has to be done by the loader is not large and the approach is a sensible one.
Figure 1.1 shows the way that this works.

                                   Fig 1.1 Separate compilation

This technique is important in C, where it is common to find all but the smallest of programs
constructed from a number of separate source files. Furthermore, the extensive use that C makes

of libraries means that even trivial programs pass through the loader, although that might not be
obvious at the first glance or to the newcomer.

First Example

The best way to learn programming is to start writing real programs. This way, concepts, which
would otherwise seem abstract, make sense, and the positive feedback you get from getting even
a small program to work gives you a great incentive to improve it or write the next one.

Besides the occasional necessity to take things on faith, there is a more serious potential
drawback of this “dive in and program'' approach: it's a small step from learning-by-doing to
learning-by-trial-and-error, and when you learn programming by trial-and-error, you can very
easily learn many errors. When you're not sure whether something will work, or you're not even
sure what you could use that might work, and you try something, and it does work, you do not
have any guarantee that what you tried worked for the right reason. You might just have
``learned'' something that works only by accident or only on your compiler, and it may be very
hard to un-learn it later, when it stops working.

My First Program

The first example program in any language: print or display a simple string, and exit. Here is my
version of “Hello, world” program: Let's write our first C program.

#include <stdio.h>

int main(void)


printf("Hello Students");

return (0);


First step for running this program is to make a text file containing above code. Be sure
that the file is a pure text file. You must save the text file with .c extension.

Then you must compile the source code. The result of compile step is an executable file
that you can run it.Program output must appear on screen.

    Hello Students

Details of First program

#include <stdio.h> - Tells C compiler to include the file “stdio.h” in this point of your C program
before starting compile step. This "include file” contains several definitions, declarations etc.

int main(void) - C program consist of one or more functions. Functions are building blocks of C
programs. main() function is different from other functions by that it is the start point of program
execution. Our program contains only function while complicated programs may contain

{ - Opening brace marks the start of a block. Closing brace will mark its end. This one marks main
() function start.

printf("Hello world"); - This line of code prints the statement between quotation marks on your
output screen.

Each command line in C ends with ";" character. Control statements are exceptions. You will
soon be able to determine when you must use ; to end a line of code.

The second line in the main function is

   return (0);

In general, a function may return a value to its caller, and main is no exception. When main
returns (that is, reaches its end and stops functioning), the program is at its end, and the return
value from main tells the operating system (or whatever invoked the program that main is the
main function of) whether it succeeded or not. By convention, a return value of 0 indicates

} closes main() function.

Now, you have to save your program in the disk and must check your program name must be
with .c, i.e. hello.c

Steps in Executing a C program

Executing a program written in C involves a series of steps. These are

    1.   Creating the program and save (i.e. XY.C).
    2.   Compiling the program.
    3.   Linking the program with functions that are needed from the C library.
    4.   Executing the program.

Higher-level languages and compiler:

Instructions that are written using English language with symbols and digits are called high-level
languages. The high level language is closer to our natural language. The commonly used high-
level languages are FORTRAN, BASIC, COBOL, PASCAL, PROLOG, C, C++ etc. The complete
instruction set written in one of these languages is called a high-level language program or
computer program or source program.

In order to execute the instructions, the source program is translated into binary form by a
compiler or interpreter. A compiler is also used to translate source program into an object
program. An interpreter is a program which takes the source program line by line and converts
into machine code line by line. .

C language uses a compiler as its translator to translate or compile the complete C program. It is
also necessary to create an executable program to execute the instructions given in a source
program by linking the input and output devices with your program. A linker (another program) is
used to link library routing and generate an executable program from an object
program. Compiler converts source program into object program in terms of stages called
passes. Normally, most of the compilers uses two passes to convert source program into the
machine language program.


 Compilers convert the program instructions from human understandable form to the machine
understandable form and the translated program instruction is called object code. Compiler is
nothing but a language translator used to translate the entire program of the high level language
into machine language. Every programming language requires its own compiler to translate the
program. For example, the programming language PASCAL requires PASCAL compiler and C
uses C compiler.


    Interpreters also convert the source program to machine language instruction but execute
each line as it is entered. The translation of the source program takes place for every run and is
slower than the compiled code. An interpreter must accompany the object code to run a program.
Programming languages BASIC and LISP use interpreters.

Structure of a C Program

                #LIBRARY FILE(S)
                # CONSTANT MACROS



#include <stdio.h>
int main(void)
printf(“\n Hello Studenst”);
printf(“\n This is my First lesson with C Programming”);

I will have more to say later about program structure, but for now let's observe a few basics. A
program consists of one or more functions; it may also contain global variables. At the top of a
source file are typically a few boilerplate lines such as #include <stdio.h>, followed by the
definitions (i.e. code) for the functions. (It's also possible to split up the several functions making
up a larger program into several source files, as we'll see in a later chapter.)

Each function is further composed of declarations and statements, in that order. When a
sequence of statements should act as one (for example, when they should all serve together as
the body of a loop) they can be enclosed in braces (just as for the outer body of the entire
function). The simplest kind of statement is an _expression statement, which is an _expression
(presumably performing some useful operation) followed by a semicolon. Expressions are further
composed of operators, objects (variables), and constants.

C source code consists of several lexical elements. Some are words, such as for, return, main,
and i, which are either keywords of the language (for, return) or identifiers (names) we've chosen
for our own functions and variables (main, i). There are constants such as 1 and 10 which
introduce new values into the program. There are operators such as =, +, and >, which
manipulate variables and values. There are other punctuation characters (often called delimiters),
such as parentheses and squiggly braces {}, which indicate how the other elements of the
program are grouped. Finally, all of the preceding elements can be separated by whitespace:
spaces, tabs, and the ``carriage returns'' between lines.

The source code for a C program is, for the most part, “free form”. This means that the compiler
does not care how the code is arranged: how it is broken into lines, how the lines are indented, or
whether whitespace is used between things like variable names and other punctuation. (Lines like
#include <stdio.h> are an exception; they must appear alone on their own lines, generally

unbroken. Only lines beginning with # are affected by this rule; we'll see other examples later.)
You can use whitespace, indentation, and appropriate line breaks to make your programs more
readable for yourself and other people (even though the compiler doesn't care). You can place
explanatory comments anywhere in your program--any text between the characters _/* and */ is
ignored by the compiler. (In fact, the compiler pretends that all it saw was whitespace.) Though
comments are ignored by the compiler, well-chosen comments can make a program much easier
to read (for its author, as well as for others).

C is Structured

A structured computer language forces users to obey strict rules about how a program can be
created. This is important for beginners because it prevents many common programming errors
that can make a program cumbersome or even unreadable. C’s structure also helps students
learn the importance of standards in coding.

C is Efficient

C programs are comparatively small and fast. This is important when running and storing a
program on a server shared by many other students. Because of C’s speed and small size, it’s
the preferred language for many professional applications.

C is Powerful and Flexible

C is powerful and flexible. The C language is used extensively in operating systems and systems
software. In fact, the UNIX system you’ll be working on when writing your programs was written in
C. C programs have also been used for solving physics and engineering problems, as well as for
creating movie special effects. In IPC144, you’ll be learning a powerful language currently used in
the real world.

Shortcomings of C

C does have some drawbacks. Because of its power and conciseness, it is possible to create C
programs that are difficult to follow. Pointers, the bane of many beginner C programmers, can
create programming errors that are very hard to trace. But these challenges can be overcome by
good programming practices. All in all, the C language is an excellent choice for a beginner
programming course where the students will be moving on to more complicated subjects.

So now we’re set to learn programming using the language C. But before we begin, we need to
learn a bit about how computers work, what computer programmers actually do, and how
computer programs are created. With this background, we’ll be set to learn the details of
programming in C.

Something about Computers

A computer is made up of Hardware and Software. Computer Hardware is the actual physical
equipment. Software is a collection of programs (computer instructions) which allow the hardware
to function. Both are critical to the computer; you can’t have a working machine without hardware
or software. Hardware and software can both be broken down into groups. For hardware, there
are five distinct components, and software has two types.

Brief Introduction to Hardware

Hardware Components

   CPU – the brain of the computer, the CPU (central processing unit) performs all the
   Primary Storage – also known as Main Memory, primary storage is used by the computer to
    store information and instructions while the machine is operating. In PCs, this is called RAM.
    Most RAM only retains data when the machine is powered on. Shut down the computer, and
    you lose any data stored in Main Memory
   Secondary Storage – secondary storage is used by the computer to store information long
    term. Powering off a computer does not affect data in Secondary storage. The most common
    type of secondary storage is the hard drive
   Input Devices – input devices allow users to enter data into the computer. The most
    common input devices are a keyboard and mouse
   Output Devices – output devices allow computer to pass information to the user. Common
    output devices include the printer and monitor.

Brief Introduction to Software

Types of Software

   System Software – system software helps the system run. System software includes
    Operating Systems, System Support Software and System Development Software
   Application Software – application software includes General Purpose Software and
    Application Specific Software

Computer Environments

Before we move into the specifics of computer programming, let’s take a moment to understand
the different types of computing environments there are.

PC Environment

First and most common is the PC environment. This is a computer that sits on its own, or may be
connected to the Internet as a peer. All the work is done on this machine, and may have attached
to it a printer or Internet connection. You probably have a PC environment in your home.

Client Server Environment

In a client/server environment, multiple PCs are attached to Servers, creating a Network of
computers. The PCs still do most of their own work, but dedicated PCs (Servers) exist on the
Network to help facilitate certain things. For example, a Server may control access to the Internet,
another may control printing to shared printers, while another may have special shared programs
or data. These machines are called Servers because they act like servers in a restaurant where
you are the client. You request something, and the Server brings it to you.

There are three main things you need to know, because they are the questions most commonly
asked by people when you tell them that you program computers.

   What exactly is a computer program?
   What does a computer programmer actually do?
   How are computer programs created?

Let’s address each question, so we know exactly what we’re getting ourselves into.

Table of Some Computer Languages

COMPUTER LANGUAGE                 WHEN INTRODUCED                   EXAMPLES
Machine Language                  1940s                             0s and 1s only
Symbolic Language                 Late 1950s                        Assembly Language
High Level Languages              Mid to Late 1960s                 PL1, BASIC, Fortran, C

Brief Introduction: System Development Life Cycle

There is one thing we need to understand before we try our hand at programming, and that is the
System Development Life Cycle. The System Development Life Cycle (or SDLC) is followed
when we create any type of system, big or small. There are very few if any businesses that do not
follow some type of SDLC when creating programs. The SDLC is a very high-level set of
instructions that are followed in order to produce a program or system in the most efficient way. A
sample SDLC is shown below.

    1. System Requirements (understand the problem and create a high level solution)
    2. Analysis (understand what is needed to create the system and see if it’s technically
       possible and economically beneficial)
    3. Design (break the problem down into simple components)
    4. Development (also known as coding, translate the simple components into computer
    5. Test (test the working solution and fix the problems)
    6. Maintain (release the product to the end-user, and make requested changes/fixes)

Q: What exactly do you understand by a computer program?

Answer: A computer program is a set of instructions that tell a computer how to solve a problem.
A problem exists when what you have is not the same as what you want. What you have is
commonly referred to in programming as Data. What you want is commonly referred to as
Information. So it’s the job of this set of computer instructions to take Data and change it into
useful Information.

Q: What is the role of Computer Programmer?

Answer: A computer programmer is the person, who writes programs in the computer. A
computer needs very simple step-by-step instructions to function. Person, on the other hand, can
solve problems. What is difficult to do is take the method that humans use to solve a problem,
and break it down into steps simple enough for a computer to understand. Once this has been
done, the steps can be translated into computer code(by compilation of the program. So
programmers need to do three things - Create a solution to a problem, break that solution down
into simple steps, and translate these steps into computer instructions. There are many good
problem solvers, and there are many good translators, but the middle step of breaking a problem
down into simple steps is the key to good programming. It’s also the hardest.

Q: How computer programs can be written?

Answer: Once the programmer has a simplified solution, the programmer translates the solution
into computer code using a Text Editor and a specific Computer Language. Once the computer
code (“Source Code”) has been entered into the text editor and saved on the computer, then, it is
compiled. Compiling takes instructions written in a language like C and translates it into Machine
Language (computer can understand directly). Keep in mind, computers are very simple and can
only work with zeros and ones (called Binary). But humans don’t want to type just 0 or 1, we
would rather write instructions that are more “human-like”. For example, if the machine language
computer instruction for printing something on the screen is 0101010001011010100001, we
could have a program that takes the word PRINT and converts it to 0101010001011010100001
for us. This is what a compiler does. Compliers make our lives as programmers much easier!

Q What effect do comments have on a program?

Answer: Comments are for the programmer. When the compiler converts the source code to
object code, it throws the comments and the white space away. This means that they have no
effect on the executable program. Comments do make your source file bigger, but this is usually
of little concern. To summarize, you should use comments and white space to make your source
code as easy to understand and main-tain as possible.

Q Define difference between a statement and a block?

Answer: A block is a group of statements enclosed in braces ({}). A block can be used in most
places that a statement can be used.

Q How can I find out what library functions are available?

Answer: Many compilers come with a manual dedicated specifically to documenting the library
functions. They are usually in alphabetical order. Another way to find out what library functions
are available is to buy a book that lists them. Appendix E, "Common C Functions," lists many of
the available functions. After you begin to understand more of C, it would be a good idea to read
these appendixes so that you don't rewrite a library function. (There's no use reinventing the

Example Programs: Enter, compile and run the example programs

Example 1:

#include <stdio.h>

int main(void)


    printf("This is ");
    printf("another C ");
    return 0;


Example: 2

#include <stdio.h>

int main(void)

    printf(“My first program in C.”);
    return 0;


1). Define characteristics of C programming languages.

2). What do you understand by compilation?

3). What do you understand by “RUN” program?

4). Write a program in C, to print your Name, Address, and Age in separate lines.


To top