c
C Programming Tutorial

C Program structure - First C Program.

In this tutorial we are going to discuss abut the structure of a C program where we will explain about the each section step by step. basically in every program should have following five sections. 

  1. Comment Line Section
  2. Preprocessor Directives 
  3. Main() Function section
  4. Program Body or Source Code
  5. Function Termination Section

Now we will take a look at a small conventional ‘C’ program example “Hello World!”.

Pro Section

Comment line section

Comments can be placed anywhere in a program and are enclosed between the delimiters /* at */. Comments are generally used for documentation purposes.

Preprocessor Directives 

Preprocessor directives are processed through preprocessor before the C source code passes through compiler. The commonly used preprocessor directives are #include and #define. #include is used for including header files. #define is used to define symbolic constants and macros.

#include <stdio.h>, stdio is the library where the function printf() is definedPrintf() is used for generating output. Before using this function, we must first include the required file, also known as a header file (.h).

You can also create your own functions, and group them in header files and declare them at the top of the program to use them.

Main Function Section

Every C program has one or more functions. If a program has only one function, then it must be main (). Execution of every C program starts with main () function. It has two parts, declaration of local variables and statements. The scope of the local variable is local to that function only. Statements in the main () function are executed one by one. Other functions are the user-defined functions, which also have local variables and C statements. They can be defined before or after main (). It may be possible that some variables must be used in many functions, so it is necessary to declare them globally. These variables are called global variables.

Following are the Some declaration form of a main Function.

  1. main()
  2. int main()
  3. void main()
  4. main(void)
  5. void main(void)
  6. int main(void)

Program Body or Source Code

In every program, after the main function it must be declared it, and for the declaration of the source code body we must specify the opening and closing parentheses. Curly brackets {}, these brackets indicate that the starting and end of a program. These brackets must be always place after the main function. All the program code will be written inside these brackets, such as declarative and executable part.

In the source code there is a common and important function that we always used that is Printf (). The printf function is used to generates the output of the written program like, “Hello World!”.

#include<stdio.h>

void main ()

{

printf(“Hello World!”);

Return 0;

}

The semicolon; determines the end of the statement. In C, each statement must end with a semicolon.

Environment For C

The steps for the execution of C program are as.

  1. Program creation
  2. Program compilation
  3. Program execution

The C programs are written in mostly two environments, UNIX and MS-DOS.

Unix Environment:

Generally, a command line C compiler is provided with the UNIX operating system. This compiler is named cc or gcc.

a) Program creation:

In unix environment, file can be created with vi editor as-

$ vi filename.c

Here $ is the unix prompt. The file can be saved by pressing ESC and SHIFT+zz.

b) Program compilation:

After creation of C program, it can be compiled as­-

$cc filename.c

If the program has mathematical function then it is compiled as­

$cc filename.c  -1m

After compilation, the executable code is stored in the file a.out .

c) Program execution:

after the compilation of program, it can be executed as­

$ a.out

MS-DOS Environment:

In MS-DOS environment creation, compilation and execution can be done using command line or IDE

(integrated Development Environment).

Command Line:

In Borland C, the command line compiler is bcc.exe and in Turbo C the command line compiler is tcc.exe.

a) Program creation:

The program file can be created using any editor and should be saved with .c extension.

b) Program compilation

After saving the file, C program Gan be compiled at DOS prompt by writing­

C:\>tcc filenaame (in Turbo C)

C:\>bcc filename (in Borland C)

c) Program execution

After compilation of C program, the executable file filename.exe is created.

It

is executed at DOS prompt

by writing-

C:\>filename

IDE (Integrated Development Environment)

All these steps can be performed in an IDE using menu options or shortcut keys. In Borland C the

program bc.exe is the IDE and in Turbo C the program tc.exe is the IDE. So, we can open the IDE

by typing bc or tc at the command prompt.

 

a) Program creation

A new file can be created from menu option New. The file can be saved by menu option Save. If the,

file is unnamed then it is saved by menu option Save as. An existing file can be opened from the menu option Open.

b) Program compilation

The file compiled by the menu option Compile. (Alt+F9)

c) Program execution

The file can be executed by the menu option Run. (Ctrl+F9). The output appears in the output window

that can be seen using the keys Alt+F5.

We have’ given you just a preliminary knowledge of how to execute your programs. There are several

other options that you can explore while working and it is best to check the manual of your compiler

to know about these options.