Structure of The C Program
Structure of The C Program
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify,
document, and understand in a particular format. C program must follow the below-mentioned outline
in order to successfully compile and execute. Debugging is easier in a well-structured 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
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 as:
or
/*
*/
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program. Header
files help us to access other’s improved code into our code. A copy of these multiple files is inserted into
our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of compilation. There
are multiple steps which are involved in the writing and execution of the program. Preprocessor
directives start with the ‘#’ symbol. The #define preprocessor is used to create a constant throughout
the program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of
defined code.
Example:
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables.
Variables and functions which are declared in this scope can be used anywhere in the program.
Example:
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this
section. Operations like declaration and execution are performed inside the curly braces of the main
program. The return type of the main() function can be int as well as void too. void() main tells the
compiler that the program will not return any value. The int main() tells the compiler that the program
will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
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.
Example:
int sum(int x, int y)
return x+y;
C
// Documentation
/**
* file: sum.c
* author: you
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
// Main() Function
int main(void)
int y = 55;
return 0;
// Subprogram
int sum(int y)
return y + X;
Output
Sum: 75
Program Creation
Keywords in C
In C Programming language, there are many rules so to avoid different types of errors. One of such rule
is not able to declare variable names with auto, long, etc. This is all because these are keywords. Let us
check all keywords in C language.
What are Keywords?
Keywords are predefined or reserved words that have special meanings to the compiler. These are part
of the syntax and cannot be used as identifiers in the program. A list of keywords in C or reserved words
in the C programming language are mentioned below:
C Identifiers
In C programming language, identifiers are the building blocks of a program. Identifiers are unique
names that are assigned to variables, structs, functions, and other entities. They are used to uniquely
identify the entity within the program. In the below example “section” is an identifier assigned to the
string type value.
For the naming of identifiers, we have a set of rules in C to be followed for valid identifier names.
A programmer has to follow certain rules while naming variables. For the valid identifier, we must follow
the given below set of rules.
5. We cannot name identifiers the same as keywords because they are reserved words to perform
a specific task. For example, printf, scanf, int, char, struct, etc. If we use a keyword’s name as an
identifier the compiler will throw an error.
C Variables
A variable in C language is the name associated with some memory location to store data of different
types. There are many types of variables in C depending on the scope, storage class, lifetime, type of
data they store, etc. A variable is the basic building block of a C program that can be used in expressions
as a substitute in place of the value it stores.
What is a variable in C?
A variable in C is a memory location with some name that helps store some form of data and retrieves it
when required. We can store different types of data in the variable and reuse the same variable for
storing some other data any number of times.
They can be viewed as the names given to the memory location so that we can refer to it without having
to memorize the memory address. The size of the variable depends upon the data type it stores.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the type of the variable.
Note: C is a strongly typed language so all the variables types must be specified before using them.
1. Variable Declaration
2. Variable Definition
3. Variable Initialization
1. C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given name and
data type.When the variable is declared, an entry in symbol table is created and memory will be
allocated at the time of initialization of the variable.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some value to it. A defined
variable will contain some random garbage value till it is not initialized.
Example
int var;
char var2;
Note: Most of the modern C compilers declare and define the variable in single step. Although we can
declare a variable in C by using extern keyword, it is not required in most of the cases. To know more
about variable declaration and definition, click here.
3. C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to the variable.
Example