History of Programming in ANSI C
‘C’ seems a strange name for a programming in ansi C language. But ANSI C strange sounding language is one of the most popular computer languages today because it is a structured, high level machine independent language. It allows software developers to develop programs without worrying about the hardware platform where they will be implemented.
The root of all modem languages is ALGOL, introduced in the early 1960s, ALGOL was the first computer language to use a block structure. Although it never become popular in USA, it was widely used in Europe. ALGOL gave the concept of structured programming to the computer Edsger Dijkstra popularized this concept during 1960s. Subsequently, several languages were announced.
In 1967, Main Richard developed a language called Basic Combined Programming Language primarily for writing system software. In 1970, Ken thompson created a language using many feature of BCPL and called it simply B. B was used to create early versions of UNIX operating system at Bell Laboratories. Both BCPL and B were “typeless” system programming languages.
ANSI C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in 1972, C uses many concepts from these languages and added the concept of data types and other powerful features. Since it was developed along with the UNIX operating system, it is strongly associated with UNIX. This operating system which was also developed at Bell Laboratories, was coded almost entirely in C. UNIX is one of the most popular network operating system in use today and the heart of the Internet data superhighway.
For many years, Ansi C was used mainly in academic environments, but eventually with the release of many C compilers for commercial use and the increasing popularity of UNIX, it began to gain widespread support among computer professional. Today C is running under a variety of operating system and hardware platforms.
During 1970s, Ansi C had evolved into what is now known as “traditional C”. The language become more popular after publication of the book ‘The C Programming Language’ by Brian Kerningham and Dennis Ritchine in 1978. The book was so popular that the language came to be know as among the programming community. The rapid growth of C led to the development of different versions of the language that were similar but often incompatible.
To assure that the C language remains standard in 1983, American National Standards Institute (ANSI) appointed a technical committee to define a standard for C. The committee approved a version of C in Dec 1989 which is now know as ANSI C. It was then approved by the in ternational standard organization in 1990.
During 1990’s C++, a language entirely based on C , Underwent a number of improvement and change and become an ANSI/ISO approved language in 1977. C++ added several new features to C to make it not only a true objec oriented language but also a more verstile language.
Importance of Programming in ansi c
The increasing popularity of C is probably due to many desirable qualities. It is a robust language whose rich set of built in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with features of a high level language and therefore it is well suited for writing both system software and business packages. In fact, many of the C compiler available in the market written in C.
Programs written in C are efficient and fast. This is due to its variety of data types and powerful operatons. It is many times faster than BASIC. For example, a program to increment a variable from 0 to 15000 takes about one second in C while it takes more than 50 seconds in an interpreter BASIC.
There are only 32 keywords ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs.
C is hughly portable. This means that C programs written for one computer can be run on another with little or no modification. Portability is important if we plan to use a new computer with a disserent operating system.
C language is well suited for structured progrmming, this requiring the user to think of a problem in terms of function modules or blocks. A proper collection of these modules would make a complete program.
Another important feature of C is its ability to extend itself. A C program is basically a collection of functions that are supported by the C libray.
Sample Program 1 : Printing A Message
main() { /*......printing begins......*/ printf("I see, I remember"); /*......printing ends......*/ }
Let us have a close look at the program. The first line informs the system that the name of the program is main and the execution begins at this line. The main() is a special function used by the C system to tell the computer where the program starts. Every program must have exactly one main function. If we use more yhan one main function, the compiler connot understand which one marks the beginning of the program.
The empty pair parantheses immediately following main indicates that the function main main has no arguments. The concept of arguments will be discussed in detail later when we discuss functions.
The opening brace “{” in the second line marks the beginning of the function main and the closing brace “}” in the last line indicates the end of the function. In this case, the closing brace also marks the end of the program. All the statements between these two braces from the function body. The function body contains a set of instructions to perform the given task.
In this case the function body contains three statement out of which only printf line is an executable statement The lines beginning with /* and ending with */ are known as commentment lines. These are used in a program to enhance its readability and understanding. Comment lines are not executable statements and therefore anything between /* and */ is ignored by the compiler. In general, a comment can be inserted wherever blank spaces can occure at beginning, middle or end of a line but never in the middle of a word.
Although comments can appear anywhere, they cannot be nested in C. That means, we cannot have comments inside comments. Once the compiler finds an opening token, it ignores everything until it finds a closing token. The comment line
/*====/*====*/====*/
is not valid and therefore results in an error.
Since comments do not affect the execution speed and the size of a compiled program, we sholud use them liberally in our programs. They help the programmers and other users in understanding the various functions and operations of a program and serve as an aid to debugging and testing. We shall see the use of comment lines more in the examples that follow.
Let us now look at the printf() function, the only executable statement of the program.
printf("I see, I remember");
Printf is a predefined standard C function for printing output. Predefined means that it is a function that has already been written and compilation and linking are explained later in this chater. The printf function causes everything between the starting and the ending quotation marks to be printed out. In this case, the output will be:
I see, I remember
Note that the print line ends with a semicolon. Every statement in C should end with a semicolon (;) mark.
Suppose we want to print the above quotation in two lines as
I see, I remember!
This can be achieved by adding another printf function as shown below:
Printf("I see, \n"); printf("I remeber !");
The information contained between the parentheses is called the argument of the funtion. This argument of the first printf function is “I see, \n” and the second is “I remember !”. These arguments are simply strings of characters to be printed out.
Notice that the argument of the printf contains a combination of two characters \ and n at the end of the string This combination is collectively called the newline character. A newline character instructs the computer to go to the next (new) line. It is similar in concept to the carriage return key on a typewriter. After printing the character comma (,) the presence of the newline character \n causes the string “I remember !” to be printed on the next line. No space is allowed between \ and n.
I see. I remember !
This is similar to the output of the program. However, note that there is no space between, and i. It is also possible to produce two or more lines of output by one pfintf statement with the use of newline character at appropriate places.
printf("I see,\n Iremember !");
will output
I see, I remember !
while the statement
printf("I\n.. see,\n... ... ...I\n... ... ... remember !");
will print out
I .. see, ... ... ... I ... ... ... REMEMBER !
#include <stdio.h>
at the beginning of all programs that use any input/output library functions. However, this is not necessary for the functions printf and scanf which have been defined as a part of the C language.
Before we proceed to discuss to further examples, we must note one important point. C does make a distinction between uppercase and lowercase letters. For example, printf and PRINTF are not the same. In C, everything is written in lowercase letters. However, uppercase latters are used for symbolic names representing constants. We may also use uppercase letters in output strings like “I SEE” and “I REMEMBER“.
The main is a part of every C program. C permits different forms of main statement. Following forms are allowed.
-
- main()
- int main()
- void main()
- main(void)
- void main(void)
- int main(void)
The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using keyword void inside the parentheses. We may also specify the keyword int or void before the word main.
The keyword void means that the function does not return any information to the operating system and int means that the function returns an integer value to the operating system. When int is specified, the last statement in the program must be “return 0”.
/* Programm ADDITION line-1 */ /* Written by EBG line-2 */ main() /* line-3 */ { /* line-4 */ int number; /* line-5 */ float amount; /* line-6 */ /* line-7 */ number = 100; /* line-8 */ /* line-9 */ amount = 30.75 + 75.35; /* line-10 */ printf("%d\n",number); /* line-11 */ printf("%5.2f",amount); /* line-12 */ } /* line-13 */
This program when executed will produce the following output : 100, 106.10
The first two lines of the program are comment lines. It is a good practice to use comment lines in the beginning to give information such as name of the program, author, date, etc. The words number and amount are variable names that are used to store numeric data. The numeric data may be either integer form or in real form.
In Programming in ansi c all variables should be declared to tell the compiler what be either variable names are and what type of data they hold. The variable must be declared before they are used. In lines 5 and 6, the declarations
int number; float amount;
tell the compiler that number is an integer(int) and amount is a floating (float) point number. Declaration statements must appear at the beginning of the functions as given diagram. All declaration statements end with a semicolon; C supports many other data types and they are discussed in detail in next chapter.
Data is stored in a variable by assigning a data value to it. This is done in lines 8 and 10. In line-8, an integer value 100 is assigned to the integer variable number and in line-10, the result od addition of two real numbers 30.75 and 75.35 is assigned to the floating point variable amount.
number = 100; amount = 30.75 + 75.35;
are called the assigned statements. Every assignment statement must have a semicolon at the end. The nest statement is an output that prints the value of number. The print statement
printf("%d\n",number);
contains two arguments. The first argument “%d” tells the compiler that the value of the second argument argument number should be printed as a decimal integer. Note that these argument are separated by a comma. The newline character \n causes the next output appear on new line.
printf("%5.2f",amount);
prints out the value of amount in floating point format. The format specification %5.2f tells the compiler that the output must be in floating point, with five places in all and two places to the right of the decimal point.
Sample Program 3: Interest Calculation – Programming in ansi c
Calculates the value of money at the end of each year of investment, assuming an interest rate of 11 percent and the corresponding amount, in two columns. The output is shown in image for a period of 10 years with an initial investment of 5000.00. The program uses the following formula:
Value at the end of year = value at start of year (1+interest rate) in the program, the variable value represent the value of money at the end year while amount represents the value of money at the start of the year.
amount = value ;
makes the value at the end of current year as the value at start of the year.
/*...............INVESTMENT PROBLEM...............*/ #define PERIOD 10 #define PRINCIPAL 5000.00 /*...............MAIN PROGRAM BEGINS...............*/ main() { /*..........DECLARATION STATEMENTS..........*/ int year; float amount, value, inrate; /*...............ASSIGNMENT STATEMENTS................*/ amount = PRINCIPAL; inrate = 0.11; year = 0; /*.............COMPUTATION STATEMENTS............*/ /*.............COMPUTATION USING While LOOP.............*/ while(year <= PERIOD) { printf("%2d %8.2f\n",year, amount); value = amount + inrate * amount; year = year + 1; amount = value; } /*...............while LOOP ENDS...........*/ } /*..............PROGRAM ENDS..............*/
Let us consider the new features introduced in this program. The second and third lines begin with #define instructions. A #defines value to a symbolic constant for use in the program. Whenever a symbolic name is encountered, the compiler subsititutes the value associated with the name automatically. To change the value, we have to simply change the definition.
In this example, we have defined two symbolic constants PERIOD and PRINCIPAL and assigned value 10 and 5000.00 respectively. These value remain constant throughout the execution of the program.
The #define Directive
A #define is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon. Symbolic constants are generally written in uppercase so they are easily distingushed from lowercase variable names. #define instructions are usually placed at the begginning before the main() function.
We must note that the defined constants are not variables. We may not change their values within the program by using an assignment statement.
PRINCIPAL = 10000.00;
is illegal.
The declaration section declars year as integer and amount, value and inrate as floating point numbers. Note all the folating-poin variables are declared in one statement. They can also be declared as
float amount; float value; float inrate;
When two or more variables are declared in one statement, they are separated by a comma. All computations and printing are accomplished in a while loop. while is a mechanism for evaluating repeatedly a statement or a group of statements. In this case as long as the value of year is less than or equal to the value of PERIOD the grouped by braces. We exit the loop when year becomes greater than PERIOD. The concept and types of loops are discussed next chapter.
C supports the basic four arithmetic operations (-,+,*,/) along with several others. They are discussed next chapter.
Basic Structure of Programming in ansi c
The examples discussed so far illustrate that a C program can be viewed as a group of building blocks called functions. A function is a subroutine that may include one or more statements designed to perform a specific task. To write a C program, we first create functions and then put them together. A Programming in ansi c may contain one or more sections as image.
The documentation section consists of a set of comment lines giving the name of the program, the other and other details, which the programmer would like to use later. The link section provides instruction to the compiler to link functions from the system library. The definition section defines all symbolic constants. Programming in ansi c
There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions.
Every C program must have one main() function section. This section contains two parts, declaration part and ececutable part. The declaration part declares all the variables used in the executable part. There is at least one statement in the executable part. Those two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing of the main function section is the logical end of the program. All statement in the declaration and executable parts end with a semicolon(;).
Read More Topics |
Operator Overloading in C++ |
Dynamic Initialization Through Constructors |
A Constructor has the Following Characteristics |
Static Member Function in C++ |