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.
We begin by programming a very simple C program. Consider the following program.
/* Your first program */
#include<stdio.h>
int main()
{
printf("Introduction to C Language\n");
return 0;
}
Compile and execute the program. You will get a text message of 'Introduction to C Language' on the screen.
Now lets look at the elements of the program line-by-line.
/* Your first program */
This line is not actually part of the program. It's simply a comment and 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 codes actually doing since you may not always remember what the codes do.
#include<stdio.h>
This line is very important in C program. The symbol
# indicates that this is a pre-processor directive. This line is a directive to the compiler to include
stdio.h header file.
stdio.h contains input/output routines which can be used in the program --
printf() is included in
stdio.h.
int main()
{
...
}
This line proclaims there is a 'main' function. It is a special function where the program is to begin execution. The
int is the data type to be return to operating system by
main function. The parentheses following
main enclose arguments (information) being passed to the function. These concept will be explained in Chapter "Function".
Then we have
{ } -- open brace specifies the start of the function while the close brace specifies the end of the function. The portion within the braces is called block or body. Within the block contains the statements that defines what the program does. There are two such statements in the code block. The first statement specifies a function to be invoked called
printf. The parentheses signifies
printf is a function.
printf("Introduction to C Language\n");
The
printf takes a string (combination of characters) as
argument which is enclosed by the double quotation mark (
").
"Introduction to C Language\n"
The
printf function simply prints the argument at the terminal (monitor screen). The last two characters which are backlash (
\) and letter
n are not printed on the screen. Collectively,
\n is known as newline character. It tells the computer to print a newline on the screen. Thus any character to be printed after
\n then appears on the next line. In other words, printing newline character is similar as pressing the Enter key of a keyboard. Notice semicolon (
;) at the end of the statement. In C all program statements must be terminated by a
;.
The last statement in the code block is
return 0;
This statement means to end the
main function execution and returns a value of 0 to the operating system. Zero means the program's execution is completed successfully without errors.