STURCTURE /SYNTAX of C program
//syntax of C program
/*Name:Author name
Documentation section
date created:21-Aug-2024
purpose:Example program to explain structure of c program*/
link section
# include<stdio.h>
#define A 10 Definition section
int b; Global declaration section
int sum(int); //user defined function prototype
int diff(int b){
return A-b;
int main()
int c;
b=25;
c=sum(b); //function calling statement
main section
printf("\nsum of %d and %d = %d",A,b,c);
c=diff(b);
printf("\ndiff of %d and %d = %d",A,b,c);
return 0;
}
int sum(int b)
{ //user defined function definition Sub-routine section
return A+b;
The basic structure of a C program is divided into 6 essential parts which helps in readability,
comprehensibility, easy for debugging and for better maintainance in a particular format. C
program must follow the below-mentioned outline in order to successfully compile and execute.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
Documentation section::This section consists of the description of the program, the name of
the program, and the creation date and time of the program. It is specified at the start of the
program in the form of comments. Documentation can be represented using single line
comment(//) and multiline comment (/* */) as:
// description, name of the program, programmer name, date, etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and is not
processed by compiler and this will not interfere with the given code. Basically, it gives an overview
to the reader of the program.
Good documentation can make your code easier to understand, maintain, and debug and can also
make it more accessible to other developers who may need to use or modify your code in the
future.
Link section( Preprocessor Section):
All the header files of the program will be declared in the preprocessor section of the program.
Header files in C are files that contain function prototypes, data type definitions, macros, and
other declarations that are needed by other files in a C program.
They are typically included at the beginning of a source code file using the #include directive,
which tells the preprocessor to insert the contents of the specified header file into the source code.
Sample Linking
#include <stdio.h>
#include <math.h>
<stdio.h>: The stdio.h header file contains declarations for input and output functions, such as pri
ntf and scanf.
<math.h>: The math.h header file contains declarations for mathematical functions, such as trigon
ometric functions, logarithmic functions, and other mathematical operations.
Note** include only required header files
Definition section:
It includes a preprocessor directive, which as a result, contains symbolic constants.
For example: #define allows us to use constants in our code. It replaces all the constants with its
value in the code.
The #define preprocessor directive in C is used to define symbolic constants, also known as macros.
A macro is a piece of code that represents a value or expression, and it can be used anywhere in
the program where that value or expression is needed.
For example, you can define a macro like this:
#define PI 3.14159
This associates the name 'PI' with the value of pi. Then, you can use this macro in your program:
double area = PI * radius * radius;
When the program is compiled, the preprocessor replaces every occurrence of 'PI' with its value, so
the above line of code would be equivalent to:
double area = 3.14159* radius * radius;
Global Declaration:
In C programming, a global declaration refers to the declaration of a variable or function that is
accessible from any part of the program, not just from the block or function where it was declared.
A global variable is declared outside of a function, typically at the top of the program file, and it
can be used by any function in the program.
Main() function
In C programming, the main() function is a special function that serves as the entry point for the
program. When a C program is executed, the operating system loads the program into memory
and starts executing instructions from the beginning of the main() function.
The main() function has a specific syntax in C:
int main() {
// code goes here
return 0;
}
The int in the function declaration indicates that the main() function returns an integer value to
the operating system when it finishes executing. The return 0; statement at the end of the function
is used to indicate the OS that the program executed successfully.
Any code that you want to execute when the program starts should be placed within the main()
function. For example, you might print a message to the console, read input from the user, or call
other functions that perform some task.
Now, what if we don’t have to return any value? For that case, we can add void as a return type on
the place of int.
Subprograms:
In C programming, subprograms refer to functions or procedures that perform a specific task or
set of tasks and can be called from other parts of the program.
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main()
function. These are specified as per the requirements of the programmer.
The user-defined function in C can be divided into three parts:
1. Function Prototype
2. Function Definition
3. Function Call
C Function Prototype
A function prototype is also known as a function declaration which specifies the function’s name,
function parameters, and return type.
C Function Definition
Once the function has been called, the function definition contains the actual statements that will
be executed. All the statements of the function definition are enclosed within { } braces.
C Function Call
In order to transfer control to a user-defined function, we need to call it. Functions are called
using their names followed by round brackets. Their arguments are passed inside the brackets.
Program characteristics:
Readability
Readability is the most crucial aspect of good programming code. The code should be easy to read
and understand for anyone who works on it, including other developers. Well-organized and
structured code makes it easier to comprehend, maintain, and update. Always use appropriate
naming conventions, indentation, and comments to improve the readability of the code.
Maintainability
Maintainability is the ability of code to remain functional and supportable over time. Good
programming code is easy to maintain, modify, and update. You should write modular code,
meaning each part is separate and can be changed without affecting the rest of the code.
Efficiency
Efficiency is another crucial aspect of good programming code. Efficient code uses minimal
resources like memory and CPU power, resulting in faster execution.
Reliability
Good programming code must be reliable and predictable. It should always produce the expected
results, and not crash or malfunction. The code should be thoroughly tested, and all edge cases
should be considered to ensure reliability.
Scalability
Scalability is the ability of code to handle an increasing amount of data and traffic. Good
programming code is designed to handle large volumes of data without slowing down or crashing.
Consistency
Consistency is the key to good programming code. The code should be consistent in style,
structure, and formatting. This makes it easier to read and understand, and multiple
programmers can work on the same codebase without any confusion.
Portability
Portability refers to the software’s ability to run on different platforms or environments without
significant modifications.
Writing/Editing
The first step in creating programs is, writing or editing the program. A program can be written in
any text editor like notepad. After writing a program, the program must be saved, In C language,
the program is saved with the extension “.c”. This is the source program written in a high-level
language.
Preprocessor Directives
Preprocessor programs provide preprocessor directives that tell the compiler to preprocess the
source code before compiling. All of these preprocessor directives begin with a ‘#’ (hash) symbol.
Inclusion of header files,Macro expansion are done here.
Compilation
After writing and saving the source program, the next step is compilation. Here we will use a
software called as compiler, which converts a program written in high-level language into machine
language. The resultant file is known as an object file in C. The extension of that file is “.obj”.
Linking
After compilation the next step is linking. Here software called linker is used. The linker links the
program with external library files which contains the code for predefined functions and creates
an executable file. The extension of the executable file is “.exe”.
Loader:
The loader is the program of the operating system which loads the executable from the disk into
the primary memory(RAM) for execution. It allocates the memory space to the executable module
in the main memory and then transfers control to the beginning instruction of the program
Execution
Finally after the executable file is created after linking it is loaded onto RAM, the next step is
execution. The operating system executes the executable file which is the machine code with the
help of the CPU and other hardware components thus finally fetch the output.