C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Basic structure of a C program

C Programming

In this tutorial we will learn about the basic structure of a C program.

Following is the basic structure of a C program.

DocumentationConsists of comments, some description of the program, programmer name and any other useful points that can be referenced later.
LinkProvides instruction to the compiler to link function from the library function.
DefinitionConsists of symbolic constants.
Global declarationConsists of function declaration and global variables.
main( )
{

}
Every C program must have a main() function which is the starting point of the program execution.
SubprogramsUser defined functions.

Lets explore the sections with an example.

Write a program to print area of a circle.

In the following example we will find the area of a circle for a given radius 10cm.

Formula

The formula to compute the area of a circle is πr2 where π is PI = 3.1416 (approx.) and r is the radius of the circle.

Lets write the C code to compute the area of the circle.

/**
 * file: circle.c
 * author: yusuf shakeel
 * date: 2010-11-25
 * description: program to find the area of a circle
 *              using the radius r
 */

#include <stdio.h>

#define PI 3.1416

float area(float r);

int main(void)
{
  float r = 10;
  printf("Area: %.2f", area(r));
  return 0;
}

float area(float r) {
    return PI * r * r;
}

The above code will give the following output.

Area: 314.16

Different sections of the above code

Documentation

This section contains a multi line comment describing the code.

/**
 * file: circle.c
 * author: yusuf shakeel
 * date: 2010-11-25
 * description: program to find the area of a circle
 *              using the radius r
 */

In C, we can create single line comment using two forward slash // and we can create multi line comment using /* */.

Comments are ignored by the compiler and is used to write notes and document code.

Link

This section includes header file.

#include <stdio.h>

We are including the stdio.h input/output header file from the C library.

Definition

This section contains constant.

#define PI 3.1416

In the above code we have created a constant PI and assigned 3.1416 to it.

The #define is a preprocessor compiler directive which is used to create constants. We generally use uppercase letters to create constants.

The #define is not a statement and must not end with a ; semicolon.

Global declaration

This section contains function declaration.

float area(float r);

We have declared an area function which takes a floating number (i.e., number with decimal parts) as argument and returns floating number.

main( ) function

This section contains the main() function.

int main(void)
{
  float r = 10;
  printf("Area: %.2f", area(r));
  return 0;
}

This is the main() function of the code. Inside this function we have created a floating variable r and assigned 10 to it.

Then we have called the printf() function. The first argument contains "Area: %.2f" which means we will print floating number having only 2 decimal place. In the second argument we are calling the area() function and passing the value of r to it.

Subprograms

This section contains a subprogram, an area() function that is called from the main() function.

float area(float r) {
    return PI * r * r;
}

This is the definition of the area() function. It receives the value of radius in variable r and then returns the area of the circle using the following formula PI * r * r.