As we have already seen, to solve a problem there are three main things to be considered. Firstly, what should be the output? Secondly, what should be the inputs that will be required to produce this output and thirdly, the steps of instructions which use these inputs to produce the required output. As stated earlier, every programming language follows a set of rules; therefore, a program written in C also follows predefined rules known as syntax. C is a case sensitive language. All C programs consist of one or more functions. One function that must be present in every C program is main(). This is the first function called up when the program execution begins. Basically, main() outlines what a program does. Although main is not given in the keyword list,it cannot be used for naming a variable. The structure of a C program is illustrated below where functions func1( ) through funcn( ) represent user defined functions.
Preprocessor directives
Global data declarations
main ( ) /* main function*/
{
Declaration part;
Program statements;
}
/*User defined functions*/
func1( ) {
…………
}
func2 ( )
{
…………
}
.
.
.
funcn ( )
{
…………
}
A Simple C Program
From the above sections, you have become familiar with, a programming language and structure of a C program. It’s now time to write a simple C program. This program will illustrate how to print out the message “This is a C program”.
We have seen in the previous unit that a computer has to be fed with a detailed set of instructions and data for solving a problem. Such a procedure which we call an algorithm is a series of steps arranged in a logical sequence. Also we have seen that a flowchart is a pictorial representation of a sequence of instructions given to the computer. It also serves as a document explaining the procedure used to solve a problem. In practice it is necessary to express an algorithm using a programming language. A procedure expressed in a programming language is known as a computer program.
Computer programming languages are developed with the primary objective of facilitating a large number of people to use computers without the need for them to know in detail the internal structure of the computer. Languages are designed to be machine-independent. Most of the programming languages ideally designed, to execute a program on any computer regardless of who manufactured it or what model it is.
Programming languages can be divided into two categories:
Prior to writing C programs, it would be interesting to find out what really is C language, how it came into existence and where does it stand with respect to other computer languages. We will briefly outline these issues in the following section.
History of C
C is a programming language developed at AT&T’s Bell Laboratory of USA in 1972. It was designed and written by Dennis Ritchie. As compared to other programming languages such as Pascal, C allows a precise control of input and output. 25 Now let us see its historical development. Basics of C The late 1960s were a turbulent era for computer systems research at Bell Telephone Laboratories. By 1960, many programming languages came into existence, almost each for a specific purpose. For example COBOL was being used for Commercial or Business Applications, FORTRAN for Scientific Applications and so on. So, people started thinking why could not there be a one general purpose language. Therefore, an International Committee was set up to develop such a language, which came out with the invention of ALGOL60. But this language never became popular because it was too abstract and too general. To improve this, a new language called Combined Programming Language (CPL) was developed at Cambridge University. But this language was very complex in the sense that it had too many features and it was very difficult to learn. Martin Richards at Cambridge University reduced the features of CPL and developed a new language called Basic Combined Programming Language (BCPL). But unfortunately it turned out to be much less powerful and too specific. Ken Thompson at AT & T’s Bell Labs, developed a language called B at the same time as a further simplification of CPL. But like BCPL this was also too specific. Ritchie inherited the features of B and BCPL and added some features on his own and developed a language called C. C proved to be quite compact and coherent. Ritchie first implemented C on a DEC PDP-11 that used the UNIX Operating System.
For many years the de facto standard for C was the version supplied with the UNIX version 5 operating system. The growing popularity of microcomputers led to the creation of large number of C implementations. At the source code level most of these implementations were highly compatible. However, since no standard existed there were discrepancies. To overcome this situation, ANSI established a committee in 1983 that defined an ANSI standard for the C language.
Salient features of C
C is a general purpose, structured programming language. Among the two types of programming languages discussed earlier, C lies in between these two categories. That’s why it is often called a middle level language. It means that it combines the elements of high level languages with the functionality of assembly language. It provides relatively good programming efficiency (as compared to machine oriented language) and relatively good machine efficiency as compared to high level languages). As a middle level language, C allows the manipulation of bits, bytes and addresses – the basic elements with which the computer executes the inbuilt and memory management functions. C code is very portable, that it allows the same C program to be run on machines with different hardware configurations. The flexibility of C allows it to be used for systems programming as well as for application programming.
C is commonly called a structured language because of structural similarities to
ALGOL and Pascal. The distinguishing feature of a structured language is
compartmentalization of code and data. Structured language is one that divides the
entire program into modules using top-down approach where each module executes
one job or task. It is easy for debugging, testing, and maintenance if a language is a
structured one. C supports several control structures such as while, do-while and for
and various data structures such as strucs, files, arrays etc. as would be seen in the
later units. The basic unit of a C program is a function - C’s standalone subroutine.
The structural component of C makes the programming and maintenance easier.
As we have already seen, to solve a problem there are three main things to be considered. Firstly, what should be the output? Secondly, what should be the inputs that will be required to produce this output and thirdly, the steps of instructions which use these inputs to produce the required output. As stated earlier, every programming language follows a set of rules; therefore, a program written in C also follows predefined rules known as syntax. C is a case sensitive language. All C programs consist of one or more functions. One function that must be present in every C program is main(). This is the first function called up when the program execution begins. Basically, main() outlines what a program does. Although main is not given in the keyword list,it cannot be used for naming a variable. The structure of a C program is illustrated below where functions func1( ) through funcn( ) represent user defined functions.
Preprocessor directives
Global data declarations
main ( ) /* main function*/
{
Declaration part;
Program statements;
}
/*User defined functions*/
func1( ) {
…………
}
func2 ( )
{
…………
}
.
.
.
funcn ( )
{
…………
}
A Simple C Program
From the above sections, you have become familiar with, a programming language and structure of a C program. It’s now time to write a simple C program. This program will illustrate how to print out the message “This is a C program”.
A C program can be executed on platforms such as DOS, UNIX etc. DOS stores C program with a file extension .c. Program text can be entered using any text editor such as EDIT or any other. To edit a file called testprog.c using edit editor, gives:
C:> edit testprog.c
If you are using Turbo C, then Turbo C provides its own editor which can be used for writing the program. Just give the full pathname of the executable file of Turbo C and you will get the editor in front of you. For example:
C:> turboc\bin\tc
Here, tc.exe is stored in bin subdirectory of turboc directory. After you get the menu just type the program and store it in a file using the menu provided. The file automatically gets the extension of .c.
UNIX also stores C program in a file with extension is .c. This identifies it as a C program. The easiest way to enter your text is using a text editor like vi, emacs or xedit. To edit a file called testprog.c using vi type
$ vi testprog.c
The editor is also used to make subsequent changes to the program
After you have written the program the next step is to save the program in a file with extension . c . This program is in high-level language. But this language is not understood by the computer. So, the next step is to convert the high-level language program (source code) to machine language (object code). This task is performed by a software or program known as a compiler. Every language has its own compiler that converts the source code to object code. The compiler will compile the program successfully if the program is syntactically correct; else the object code will not be produced.
The C Compiler
If you are working on UNIX platform, then if the name of the program file is testprog.c, to compile it, the simplest method is to type
cc testprog.c
This will compile testprog.c, and, if successful, will produce a executable file called a.out. If you want to give the executable file any other, you can type
cc testprog.c -o testprog
This will compile testprog.c, creating an executable file testprog.
If you are working with TurboC on DOS platform then the option for compilation is provided on the menu. If the program is syntactically correct then this will produce a file named as testprog.obj. If not, then the syntax errors will be displayed on the screen and the object file will not be produced. The errors need to be removed before compiling the program again. This process of removing the errors from the program is called as the debugging.
Syntax and Semantic Errors
Every language has an associated grammar, and the program written in that language has to follow the rules of that grammar. For example in English a sentence such a “Shyam, is playing, with a ball”. This sentence is syntactically incorrect because commas should not come the way they are in the sentence.
Likewise, C also follows certain syntax rules. When a C program is compiled, the compiler will check that the program is syntactically correct. If there are any syntax errors in the program, those will be displayed on the screen with the corresponding line numbers.
Apart from syntax errors, another type of Basics of C errors that are shown while compilation are
semantic errors. These errors are displayed as warnings. These errors are shown if a
particular statement has no meaning. The program does compile with these errors, but
it is always advised to correct them also, since they may create problems while
execution. The example of such an error is that say you have declared a variable but
have not used it, and then you get a warning “code has no effect”. These variables are
unnecessarily occupying the memory.
After compilation, the next step is linking the program. Compilation produces a file with an extension .obj. Now this .obj file cannot be executed since it contains calls to functions defined in the standard library (header files) of C language. These functions have to be linked with the code you wrote. C comes with a standard library that provides functions that perform most commonly needed tasks. When you call a function that is not the part of the program you wrote, C remembers its name. Later the linker combines the code you wrote with the object code already found in the standard library. This process is called linking. In other words, Linker is a program that links separately compiled functions together into one program. It combines the functions in the standard C library with the code that you wrote. The output of the linker in an executable program i.e., a file with an extension .exe.
Run the C Program Through the Menu
When we are working with TurboC in DOS environment, the menu in the GUI that pops up when we execute the executable file of TurboC contains several options for executing the program:
All these options create an executable file and when these options are used we also get the output on user screen. To see the output we have to shift to user screen window.
Run From an Executable File
An .exe file produced by can be directly executed.
UNIX also includes a very useful program called make. Make allows very complicated programs to be compiled quickly, by reference to a configuration file (usually called makefile). If your C program is a single file, you can usually use make by simply typing –
make testprog
This will compile testprog.c as well as link your program with the standard library so that you can use the standard library functions such as printf and put the executable code in testprog.
In case of DOS environment , the options provided above produce an executable file and this file can be directly executed from the DOS prompt just by typing its name without the extension. That is if the name of the program is test.c, after compiling and linking the new file produced is test.exe only if compilation and linking is successful.
This can be executed as:
c>test
Linker Errors
If a program contains syntax errors then the program does not compile, but it may happen that the program compiles successfully but we are unable to get the executable file, this happens when there are certain linker errors in the program. For example, the object code of certain standard library function is not present in the standard C library; the definition for this function is present in the header file that is why we do not get a compiler error. Such kinds of errors are called linker errors. The executable file would be created successfully only if these linker errors are corrected.
Logical and Runtime Errors
After the program is compiled and linked successfully we execute the program. Now there are three possibilities:
The first case simply means that the program is correct. In the second case, we get wrong results; it means that there is some logical mistake in our program. This kind of error is known as logical error. This error is the most difficult to correct. This error is corrected by debugging. Debugging is the process of removing the errors from the program. This means manually checking the program step by step and verifying the results at each step. Debugging can be made easier by a tracer provided in Turbo C environment.
John Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
ReplyJohn Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Reply