SOMETHING ABOUT PROGRAMMING
What is ‘C’?
As a programming language, C is rather like Pascal or Fortran. Like these programming languages, C uses variables to store values, constructs, structured programs by defining and calling functions, controls program flow using loops, if statement and switch statement. C uses arrays and structures in order to group homogeneous and non-homogeneous elements respectively. Input and output can be directed to the terminal or to files.
Programmers, who are familiar with the use of pointers, will welcome the ease of use compared with some other languages, such as in Pascal. But remember that if you use the pointers in an undisciplined way then they are very hard to trace these. In this book we will deal with the simplest application of pointers.
Of course if you are new to ‘C’ then you will find C as useful and friendly language. You have to take care of many of the extra facilities in order to avoid extra types of program errors. Once you will have to deal with these to successfully make the transition to being a C programmer.
History of C
The programming language ‘C’ was designed and written by a system programmer Dennis Ritchie at AT&T (American Telephone and Telegraph) Bell Labs in New Jersey, USA in 1972. Ritchie goal was to provide a language that would allow the programmer to access the hardware as is done with assembly language but with structured programming features similar to those found in high-level programming languages.
Actually ‘C’ was written for the UNIX operating system on a PDP-11 computer at the AT&T Bell Labs. Initially it was a totally selfish motto because Ken Thompson, another system programmer of Bell Labs, was working on a project – UNIX, a multiuser, multitasking operating system which ran first on PDP-7 and then on PDP-11. Ken Thompson wrote the original Unix system in PDP-7 assembler. It was very difficult for him to debug a program in assembly language because such programs are highly machine dependent. He was soon joined by Dennis Ritchie. Due to lack of portability, they need such a high-level language that may help them.
With these concepts, Ken Thomson developed a new language ‘B’ at AT&T Bell Labs in 1970. He adapted it from a language BCPL (Basic Combined Programming Language) developed by Matrin Richards in 1967 at Cambridge University. BCPL was used during the early 1970s for several interesting projects, among of them the OS6 operating system at Oxford, and parts of the seminal Alto work at Xerox PARC. The major limitations of BCPL was that it was too specific and it could deal with only specific problems. Ken Thomson selected the first of the initials BCPL. But like BCPL, ‘B’ too turned out to very specific. After this Dennis Ritchie modified and improved this language ‘B’, inheriting the features of ‘B’ and BCPL. Additionally he added some of his own features and developed ‘C’. It is mere coincidence that the letter B and C are alphabetic in order. Though an alternate theory holds that the letter ‘B’ is derived from Bon, an unrelated language created by Thompson during the Multics days. Bon in turn was named either after his wife Bonnie or after a religion whose rituals involving the murmuring of magic formulas. The earlier languages belonging to this ancestry are Cambridge’s CPL (Combined Programming Language) in 1963 and Algol-60 (Algorithm) in 1960.
After this UNIX operating system was rewritten in C language. More than 80% of UNIX is in C. UNIX became the first major operating system to be written in something other than assembly language. C rapidly became most programmer’s favorite language for writing program under MS-DOS. MS-DOS itself consists of C programs, as do most of its utility programs. You will be surprised to know that Windows 3.x graphical operating system was written in C. Even Windows 98 and Windows NT are written mostly in C.
Another important factor is that C is not limited to any one operating system or machine. C is one of the most portable programming language existing today. C was originally defined in 1981 in the C Programming Language (Prentice Hall) by Brian Kernighan and Dennis Ritchie. This book described C the way Ritchie implemented it. This book became known simply as K&R C. Some programmers now refer to K&R C as “Classic C”. Over the years, compiler builders added features of C according to the requirements of their own users.
Standards of C
Many different organizations have their own C compilers which may differ from one another to a greater or lesser extent. Some of the well known C compilers are as:
- Aztech
- Boroland International (Turbo C)
- Lattice DeSmet
- MicroSoft
- Power C
- Quick C
- White Smiths
Compilers can be very sensitive even to minor differences of the syntax. Therefore if you experience any difficulties then just consult the manual that comes with your version of C.
In order to provide a standard for C, a technical committee X3J11 on C language standardization, was formed by American National Standard Institute (ANSI) in 1983. The X3J11 committee represents a cross-section of C community: it consists of about fifty active members representing hardware manufacturers, vendors of compilers and other software development tools, software designers, application programmers and so on. The committee took as its reference in the K&R definition and began its lengthy and painstaking work. The job of committee was to try to eliminate ambiguities and preserve the spirit of C. ANSI was joined in their task by a committee of the International Standard Organization (ISO), which formed to define the language for the international programming community.
Development of the standard took a long time, as standards often do. Much of the work of the committee is not just technical but also procedural. The painstaking work of obtaining consensus among committee members is critical to the success of a practical standard, even if at times it means comprising on technical perfection” whatever that might be. The technical work was completed by Dec, 1988. But it took a further year to resolve procedural objections. And on Dec 7th 1989, the C language was finalized by the ANSI-committee and ISO.
The language defined by them is known as Standard C. This book introduces ANSI C since it is supported by the SUN workstation compilers. Most C programming texts are not available in ANSI editors, Unfortunately, not all compilers written adhere strictly to the standard. For one reason or another, various compilers recognize slightly different versions of the C language. Fortunately Turbo C, for example, is a 100% implementation of the ANSI C standard.
Features of ‘C’
C was first used for system programmers because it possesses the features of both low-level and high-level languages. That’s why C language is also called the middle level language. C provides the facility of manipulating bits, bytes and addresses. Such programs run faster. Now-a-days, C is also being used in developing application softwares.
Spirits of C
Some of the facets of the spirit of C can be summarized as follows:
- trust the programmer
- keep the language small and simple
- don’t prevent the program from doing what needs to be done
- provide only one way to do an operation
- make it fast
You can say that the committee kept as a major goal to preserve the traditional spirit of C.
Virtues of ‘C’
C is rapidly becoming one of the most important and popular programming languages. Virtues of C are as:
Modern Language – C is a modern and general purpose programming language that has much in common with the best of the high-level languages.
Efficient – C provides the features of both low-level and high-level language, that’s why C programs run faster than other high-level language.
Limited Data Types – Although C has four basic data types, it is not strong type language as other high-level languages, such as Pascal, Ada.
Portable – C is portable language because C compilers are available for a number of systems. C programs written on one machine can be run with little or no modification on other machine.
Powerful – C is powerful and flexible language. UNIX, compilers and interpreters for other languages, well known packages such as AUTO CAD, ORACLE, are written in C. One of the most interesting things about C language is that its compiler is written in C.
Limitations of ‘C’
No doubt, ‘C’ is reliable, simple and easy to use, but it has some limitations, as follows:
- C provides no direct provision for taking input and displaying output, rather it depends heavily on UNIX function library which is supplied with most C compilers.
- Some of the operators have the wrong precedence.
- Some part of the syntax could be better.
Regardless of these limitations, C is an extremely effective and expressive language for a wide variety of programming applications.
The Structure of a Typical ‘C’ Program
C is a procedural programming language. It means that you can design and code programs as procedural modules. The procedural modules in C programs are called functions. Every C program consists of one or more functions. Every function in a program has a unique name and it can be called through a function call. Any function can call any function. Even a function can call itself.
Functions may contain parameters. When a function is called, the values for its parameters are passed to the called function. Some functions return a value and others do not. The calling function that returns a value can code the function call on the right hand side of an assignment statement, assigning the returned value to a named variable. Each function consists of one or more block statements. In C a block is just a set of statements enclosed in a pair of curly braces ‘{’ and ‘}’. Each block has its own set of local variables, which remains in scope as long as the statements in that block are executed. A C program can also have global variables. Global variables are declared outside all functions. Global variables are called as global because the statements in all functions in the same source file can reference them.
In C statements are one of the following types:
- Declaration Statements – The statements that declare variables and functions are called as declaration statements. A variable declaration specifies the variable’s storage class, data’s type, name and dimensions, if it is an array. A function declaration, also known as function prototype, declares the function’s return type, name and number and types of its arguments.
- Definition Statements – The statements that define the instances of variables and functions are called as definition statements. A variable definition includes the components of declaration and may include an initializer if the variable has an initializing value. A function definition contains the function’s executable code.
Here one should remember that a variable declaration and definition are the same statement but a function declaration and definition are usually in different places. However the function definition may also serve as its prototype only if the function’s definition appears in the source code file ahead of all calls to the function.
- Procedural Statements – These statements include assignments, expressions, or control statements. Expressions consist of variables, constants, operators, and function calls. An expression can stand on its own or be on the right side of an assignment statement.
We have already studied that a function is collection of statements which is designed to perform a specific task. The statements must appear in the function in the same order in which we wish them to be executed. C provides sequential, selective and repetitive statements. C allows separately compiled functions to used without being in the program proper, that is a function can be accessed by any program.
Here is the structure of a typical C program:
[Comments][Preprocessor Directives]
[External Variable Declaration]
main()
{
[Local Variable Declaration]
Program Statements
}
[User-defined Functions]
The square brackets denote optional structures.
Now let us study a simple C program.
A Simple ‘C’ Program
Before going into the depth of C, let us consider a very simple program, examine it and try to discover what it does.
1. /* Program – AB.c */
2. #include
3. main()
4. {
5. printf(“Welcome to C Programming.”);
6. }
Every C program is basically a collection of functions in which main() is an essential function and several other fundamentals. Typically, we organize a program into major tasks, and then design separate functions to carry out these tasks. Here the program chap0101.c has following elements:
- Source code comments, that may contain any message starting with the character sequence /* and ending with the sequence */.
- A preprocessor #include directive
- A function heading : main()
- A function body enclosed within { and }
- A function statement that writes a message on the screen
Comments
Line numbers 1 contains a comment statement. Comments are an important part of any C program. It is helpful to understand what’s going on. Comments have no effect on the executable program because the compiler simply ignores them.
In C, comments begin with the /* character sequence and continue through the */ character sequence as follows:
/* This is a typical C comment’s style */
Usually /* …. */ style can also be used for commenting out a block of code or you can say where comments are spanned several source-code lines as:
/* This is a typicalC-style comment
that may have one or
two or three
or multiple lines */
However you can also insert a /* …. */ anywhere within the text of a program lines as:
main() /* This is main() function */{ /* main starts */
….
} /* main ends */
But we can not use nested comments, therefore following comments cause an error message:
/* It will not /* work properly *//* It will also not /* work */ properly */
However, some compilers, such as Lattice C, allow comments to be nested.
One should clear in one’s mind where to use and how to use comments. Comments are used in a program to make it meaningful, not for yourself only but also for others. Comments document your intentions, so use them, if necessary.
The Preprocessor Directives: #include
A preprocessor is a program that processes a source file before the main compilation takes place. The C language handles directives whose names begin with a number sign #. We don’t need to do anything special to invoke this preprocessor. It is invoked automatically by the compiler when we compile the program
In the program AB.c, we have used the following preprocessor directive
#include
The #include directive tells the compiler to include a different source-code file in your program. This directive tells the compiler to add the contents of the stdio.h file into your source file. This action of preprocessor is similar to pasting a block of text data into a document with your word processor.
Now the question arises – why add the contents of the stdio.h file to the program? Actually the stdio.h describes some functions that are used for console input/output because the C language does not provide any such facility of reading/writing directly on the console. The stdio.h stands for standard input–output and ‘.h’ stands for header file. By input we mean the information brought into the program and output sends out the information from the program. We can receive information either from a standard input device, keyboard, or from a file. Similarly we can send information either to a standard output device, VDU, or to a file.
The stdio.h provides scanf() function to receive information from a standard input device and printf() function to send information to a standard output device. That’s why it is necessary for the programs that use scanf() and printf() functions for input and output, to include the stdio.h file. Without its inclusion, the compiler would not recognize scanf() and printf() functions. However in some compilers, the inclusion of header files are optional. But in other compilers, it is necessary. Remember that files with .h extension are referred as header files.
The filename in the #include directive is enclosed by angle brackets, that is ‘’ (greater than symbol). This usage identifies header files that compiler system provides. However if you want to include your own header files then you would surround these user-defined header files within double quotes (“….”) as:
#include “userfile.h”
Here userfile.h is an user-defined header file.
The function main()
The third line of the program AB.c specifies a function main. Every C program must have a main() function. The word main is followed by a pair of ordinary parentheses ‘(’ and ‘)’. Here you can not use square brackets ‘[’ and ‘]’ or even curly braces ‘{’ and ‘}’. There is nothing enclosed in the parentheses because the main function does not have any parameters. In later chapters, we shall have much more to say about the parameters contained within these parentheses.
The word main() is followed by the statement(s) enclosed within the curly braces ‘{’ and ‘}’. However in C, the curly braces, along with the statements enclosed within them are referred to as a compound statement.
In some books you may also find the following notations for main() function:
- void main()
- void main(void)
- int main()
Actually the part that precedes the function main is called the function return type. The word void that precedes the function main in first and second notations specify that this function does not return any value to the caller. Similarly the word that follows the main in second notation specifies that this function receives no argument from the function that calls it. Now look at the third notation. This third notation specifies that this function returns an int value. Therefore you have to place a return statement before the closing brace of main() function as:
int main()
{
….
return 1;
}
Finally one may ask you – who calls main() function? It is not you (although in fact you can call main() if you want, just as you can call other functions); but it is the startup code who is the primary customer for main() function.
The printf() Function
The printf() function is used to display a message enclosed between the double quotes, to the monitor. It is the most important function used to display text on the screen. Like any function, the printf() function has two parentheses between which comes the string to be displayed, enclosed in double quotations.
Here the printf() function is embedded into a statement whose ending is marked by the semicolon (;). Note that every statement in C is terminated by a semicolon (;). In other words, you can say that the semicolon tells the compiler that a statement is terminated. Now look at the strange character ‘\n’ that comes right after the string and before ending the double quote mark. This character is called new-line character. This new-line character (\n) brings the cursor to the beginning of the next line.
In this ARTICLE we have studied what a program is and what a programming is. A program is basically a sequence of instructions that must be understood by a computer. The process of planning a sequence of instructions for a computer to follow is called computer programming. An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
Before writing a program one must be very careful about naming the variable names or functions. The variable names or function names must be chosen according to the activities performed by them. . Documentation is also desirable. Documentation is just a written text and comments that make a program easier for others to understand, use and modify. The process of detecting and removing errors in a program is referred as program testing and debugging.
Computer is a programmable device that can store, retrieve, and process data. A typical computer system consists of three main part: memory, central processing unit (CPU) and I/O devices. Memory is used to hold data and information. CPU is used to perform arithmetic and logical calculations and control the execution of a program and I/O devices are used to receive information from outside world and produce results to outside world.
C is middle level language which is designed by Dennis Ritchie in 1972 at AT & T Bell Labs in USA. C language is very simply, structured, portable, powerful and modern language.