C was developed in 1972 by Dennis Ritchie based on a programming language called B (which is derived from BCPL language). It is one the most used programming language in software development area. At the beginning, C was designed and developed for the development of UNIX operating system. Today C has been widely used to develop many type of application software such as basic calculator, word processor, games, operating system etc.
Getting Started - New to C Language?
Before we discuss C syntax in details, we look at the fundamental stages of C program development. Typically there are four development stages namely editing, pre-processing and compiling, linking and executing.
Editing
Editing is the process of writing the C source code. Programmers write C source code using editor program. There are many editor program available for C. But two editors widely used on Linux platform are vi and emacs. On Windows platform, we can use Notepad as the editor program but most peoples use software package for C such as C++ Builder, Code::Blocks, Dev-C++, Microsoft Visual Studio etc. that have integrated editor. These software provides complete programming environment for writing, managing, compiling, debugging and testing C source code. This is also known as Integrated Development Environment (IDE).
Pre-processing and Compiling
A C source has to be translated into machine language code. Machine language is a language that computer can understand in the form of binary number. The process of code translation is called compiling. During compilation process, the compiler can detect any syntax error due to unrecognised program code as well as structural errors. The output of compilation process is an object code which is saved in a file called object file. In C, before a source code is compiled, there is a stage called pre-processing. During this stage, C pre-processor performs directives which are usually including header or other C files to be compiled and text substitution.
Linking
Linking is a process of combining generated object files, add required codes from C standard library and produce an executable file. C standard library is collection of header files. Header files contain C source code that performs common operation such as input/output, mathematical operation and string handling.
Execution
The execution stage is where you run the program to check whether it produce the desired output. This stage can be a testing stage where you check the output of your program. If it does not produces the suppose output, then you have made logic (semantic) error in your source code. Unlike syntax error, logic error won't be detected during compilation process. Therefore you have to find out the error's location by yourself.
C Program Layout
Basically, a C program consists of two sections:
1. pre-processor directives
2. main function

The pre-processor section is the place where we specify what compiler should do before compiling the source code. Usually we would specify the included header file which will be used in our program. A header file keeps all the information about functions available in standard C library. All pre-processor directives begins with the # symbol. The second section begins with the function main() definition. The function main() is where the program starts its execution. Thus function main() must present in any C program.
An Example of C Program
We begin by programming a very simple C program. Write the following program using your editor program. When you finish writing it, compile it and execute the program. You will get a text message displayed on your monitor screen. The program's output is given at the sample output section. Don't worry if what you type does not make sense. I'll explain everything later.

Sample output:
C program example
Explanation:
The first line of the program is

This line is not actually part of the program. It is simply a comment and it is included to provide information on what the program does. A comment begins with /* and end with */. It is a good habit to include comments in every part of your program especially when you are writing a big program/project. It will keep you inform what are the code actually doing since you may not always remember what they do.
Then we have the next line which is

This is also not part of the program, but it is a very important line in C program. In fact, if this line is omitted, the program won't actually works. The symbol # indicates that this is a pre-processor directive. Remember about header file that I mentioned during compilation process. This line is a directive to the compiler to include stdio.h header file in our program. stdio.h contains information on input/output routines which will be needed during compilation process. In this case we are using the printf() function from stdio.h.
The next line is

This is also another comment line.
Then we have the main function section.

The main function has only two (2) statements. The first is printf() statement and the second is return statement. I'll explain both statements in details later.
Now we look at the first line of function main() definition.

This defines the start of the function main(). Function main() has the keyword int signifies what type of value will be returned by the function. In this case the function main() will return an integer value. There parentheses following the main. This is the place where we specify information to be transferred to function main(). However, in this example we don't transfer any information.
Then we have

The open brace specifies the start of the function whilehe close brace specifies the end of the function. The portion within the braces is called block or body. Within the block is where we write the statements that defines what our program does.
Now lets look at the first statement.

printf() function instructs the computer to display characters or a string enclosed by the qoutation marks on the monitor screen. A string is a combination of numerous characters. Items enclosed by the parentheses are called arguments.
The last statement in this program is

At the end of function main() we have to include return 0 statement because function main is defined to return a value of integer type. This statement also indicates that the program has terminated successfully.
Symbolic Constant - #define Directive
I would like to discuss another pre-processor directive that is usually used in C programming. #define directive is used to define a symbol (name) that represents a constant value. Basically #define directive acts as a constant converter. It causes the compiler to go through all the program's codes and whenever it encounters the defined symbol, it will substitute the symbol with the specified value. For example,
#include<stdio.h>
#define PI 3.14159
int main() {
...
...
}
Symbol PI is defined and it represents the value of 3.14159.
1 comments:
Nice blog! Keep it up! I have a blog on SAP ABAP :=)
Post a Comment