Skip to main content

Structure of a C program

Lets understand the  basic structure of a C program. A C program is divided into different sections. There are six main sections to a basic c program.

The six sections are,

Documentation

Include/Link/Preprocessor   section

Definition

Global Declarations ( Functions and variables)

Main function

Subprogram functions

 

 Now let us learn about each of this layer in detail.



Figure: Basic Structure Of C Program


Sample Program

The C program here will find the area of a circle using a user-defined function and a global variable PI holding the value of pi

/* File Name: areaofcircle.c

Author: Abhijith

date: 06/05/2021

 description: a program to calculate area of circle

user enters the radius

*/

#include<stdio.h>  //link section

#define PI  3.14;//definition section

float area(float r);//global declaration

/****************************/

int main()//main function

{

float r;

printf(" Enter the radius:\n");

scanf("%f",&r);

printf("the area is: %f",area(r));

return 0;

}

/**************************/

float area(float r)

{

return pi * r * r;//sub program

}

Documentation Section

The documentation section is the part of the program where the programmer gives the details associated with the program. He usually gives the name of the program, the details of the author and other details like the time of coding and description. It gives anyone reading the code the overview of the code.

/* File Name: areaofcircle.c

Author: Abhijith

date: 06/05/2021

 description: a program to calculate area of circle

user enters the radius

*/

 Link Section

This part of the code is used to declare all the header files that will be used in the program. This leads to the compiler being told to include the header files to the source program and link the system libraries.

#include<stdio.h>

Definition Section

In this section, we define different constants. The keyword define is used in this part.

#define PI 3.14

Global Declaration Section

This part of the code is the part where the global variables are declared. All the global variable used are declared in this part. The user-defined functions are also declared in this part of the code.

float area(float r);

int a=7;

Main Function Section

Every C-programs needs to have the main function. Each main function contains 2 parts. A declaration part and an Execution part. The declaration part is the part where all the variables are declared. The execution part begins with the curly brackets and ends with the curly close bracket. Both the declaration and execution part are inside the curly braces.

int main()//main function

{

float r;

printf(" Enter the radius:\n");

scanf("%f",&r);

printf("the area is: %f",area(r));

return 0;

}

Sub Program Section

All the user-defined functions are defined in this section of the program.

float area(float r)

{

return pi * r * r;//sub program

}

 

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo