0% found this document useful (0 votes)
17 views

Structure of The C Program

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Structure of The C Program

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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.

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

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:

// description, name of the program, programmer name, date, time etc.

or

/*

description, name of the program, programmer name, date, time etc.

*/

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:

#define long long ll

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:

int num = 18;

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;

Structure of C Program with example

Example: Below C program to find the sum of 2 numbers:

 C

// Documentation

/**

* file: sum.c

* author: you

* description: program to find sum.

*/

// Link

#include <stdio.h>

// Definition

#define X 20

// Global Declaration

int sum(int y);

// Main() Function
int main(void)

int y = 55;

printf("Sum: %d", sum(y));

return 0;

// Subprogram

int sum(int y)

return y + X;

Output

Sum: 75

Steps involved in the Compilation and execution of a C program:

 Program Creation

 Compilation of the program

 Execution of the program

 The output of the program

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:

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

C Identifiers

Last Updated : 06 Sep, 2023

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.

char section = 'A';

For the naming of identifiers, we have a set of rules in C to be followed for valid identifier names.

Rules to Name an Identifier in C

A programmer has to follow certain rules while naming variables. For the valid identifier, we must follow
the given below set of rules.

1. An identifier can include letters (a-z or A-Z), and digits (0-9).


2. An identifier cannot include special characters except the ‘_’ underscore.

3. Spaces are not allowed while naming an identifier.

4. An identifier can only begin with an underscore or letters.

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.

6. The identifier must be unique in its namespace.

7. C language is case-sensitive so, ‘name’ and ‘NAME’ are different identifiers.

C Variables

Last Updated : 11 Jun, 2024

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.

data_type variable_name = value; // defining single variable


or
data_type variable_name1, variable_name2; // defining multiple variable
Here,

 data_type: Type of data that a variable can store.

 variable_name: Name of the variable given by the user.

 value: value assigned to the variable by the user.

int var; // integer variable


char a; // character variable
float fff; // float variables

Note: C is a strongly typed language so all the variables types must be specified before using them.

Variable Syntax Breakdown

There are 3 aspects of defining a variable:

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

int var; // variable definition


var = 10; // initialization
or
int var = 10; // variable declaration and definition

You might also like