# Introduction to C Language

## Chapter 4: Programming Basics

#### Unit 1

Chapter 1: Computer Basic*

*CS101 Computer Basic is a small separate course prerequisite for KCS101/KCS201 Programming for Problem Solving – Using C

Chapter 2: Terminology

Chapter 3: Idea of Algorithm

Chapter 4: Programming Basics

#### Unit 2

Chapter 5: Arithmetic Expressions and Precedence

Chapter 6: Conditional Branching

#### Unit 3

Chapter 7: Iteration and Loops

Chapter 8: Functions

#### Unit 4

Chapter 9: Arrays, Structure, Union, and Enumeration

Chapter 10: Basic Algorithms and their Complexity Coexistence

#### Unit 5

Chapter 11: Pointers

Chapter 12: File Handling

Chapter 13: Macros &amp; Command-Line Arguments

#### Appendix

KCS151/KCS251 PPS Lab Exercise

PPS Previous Year Question Papers

Each programming language uses its own format of coding. C is not an exception. We explain the basic structure of the C program in this tutorial.

## C Program consists of the following 6 sections:

1. Documentation Section
3. Definition Section
4. Global Declaration Section
5. The main Function Section
• Local Declaration Part
• Execution Part
6. Sub Program Section
• Function 1
• Function 2
• Function 3
…
• Function n

Let us explore these sections with an example.

				
/*
File: Circle.c
Date: May 21, 2019
Description: Program to find the area and parameter of a circle.
*/

#include <stdio.h>

#define PI 3.1416

float area (float);
float par (float);

int main (void)
{
printf ("Area: %.2f", area (radius) );
printf ("\nParameter: %.2f", par (radius) );
return 0;
};

{
};

{
return 2 * PI * radius;
};



## Explanation

### 1. Documentation Section

The documentation section usually contains the comments. It includes the name of the programmer, author, date of writing the program, a brief description of the program, and other details. It helps anyone to get an overview of the program.

/*
File: Circle.c
Date: May 21, 2019
Description: Program to find the area and parameter of a circle.
*/

We will discuss the comments in detail later.

The link section instructs the compiler to connect the various function with the libraries. It includes these libraries in the program as header files.

#include <stdio.h>

stdio.h is a standard library for input/output functions. It allows us to use functions like printf, scanf, getc, putc, fprintf, and fscanf, etc. We will discuss various header files in detail later.

### 3. Definition Section

The definition section contains symbolic constants. These symbolic constants are called Macros.

#define PI 3.1416

We will discuss Macros in detail later.

### 4. Global Declaration Section

The global declaration section defines global variables and declares user define functions. Global variables are those variables that can be accessed within any user-defined function written in the program. The above example contains two user-defined functions other than the main function.

float area (float);
float par (float);

We will discuss global variables and user-defined functions in detail later.

### 5. The main Function Section

This section contains a user-defined function called main. Every C program must have one and only one main function. It is the starting point of the program for execution. Each function has two parts a. Local Declaration part, and b. Execution part. The local declaration part used to define local variables that can be accessed within the function. The execution part contains the statements to execute. Both, local declaration part and executable part are written in between a set of opening and closing braces. All the statements of both parts must end with a semicolon.

int main( )
{

printf (“Area: %.2f”, area (radius) );
printf (“\nParameter: %.3f”, par (radius) );
return 0;

};

### 6. Sub Program Section

The subprogram section contains user-defined functions that are used to perform specific tasks. It may be written before or after the main function.

{

};

{

return 2 * PI * radius;

};