A Simple C Program 

Let`s have a look at your first C program,demonstrated in List 1.1.

List 1.1

/*Begin the program*/

#include <stdio.h>

main( )

{

printf("Hello.This is my first C Program.\n");

return 0;

}

/*End of the program*/

This is a very simple C Program.What you have to learn from this basic C Program is what is need in a C Program.Firstly let`s see what will appear at screen when you run this program.

Output:Hello.This is my first C Program.

The #include Directive

Now let`s see the meaning of keywords that we us in this program.In the first line there is #include .In PC #include forms a preprocessor diretive that tell the C preprocessor to look for a file and place the contents of that file in the location where the #include directive indicates.The preprocessor is a program that does some preparations for the C compiler before your code is compiled.

Also in this line you will see that <stdio.h> follows #include.The #include directive does ask the C preprocessor to look for and place stdio.h at the location of the directive in the C program.The name of the stdio.h file stands for standard input-output header file.The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C program.

Header Files

The files that are included by the #include directive,like stdio.h are called header files because the #include directives are almost always placed at the start,or head oc C program.Actually,the extension name of .h does mean "header" and these are sometimes referred to in conversation as dot-h files.Besides stdio.h,there are more header files , such as stdlib.h,string.h,math.h and soon on.

The main( )Function

You also will see the main( ) that you use in the second line.This is very specila function in C.Every C program must have a main ( ) function,and every C program can only have one main () function.You can put the main ( )function whereever you want in your C program.If you create other function in your program,main ( ) will always execute first,even if it is at the bottom of you program file. One more important thing about main ( ) is that the execution of every C program ends with main( ).A program ends when all the statements within the main ( ) function have been executed.

The Newline Character ( \n )

The newline character tells the computer to move the cursor to the beginning of the next line on the screen.

The Return Statement

All function in C return values.For instance,when you create a function to add two numbers,you can make such a function that returns to you the value of the addition.The main( ) function itselt returns an integer value. In C ,integers are decimal numbers without fraction portion.Therefor in the program listed in List 1.1 ,there is a statement return 0; ,that indicates that 0 is returned from the main ( ) function and the program is terminated normally.

Exercises

Let`s do this exercises to make sure you understand.

1. Write a C program that the output will be the same as this...

Hello.

This is my first C program.

2. Write a C program to calculate the multiplication of 4 times 6 and then print out on the screen.

Click here to see the answers.

©All right reserved 2000.