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

1_Structure of C Program

Uploaded by

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

1_Structure of C Program

Uploaded by

saisudhiraicte
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Structure of C Program

What is the structure of C?

• The structure of a program refers to how the program is organized and


the way its components interact with each other. It determines the flow
of information and helps in problem-solving.
• Without a proper structure, it becomes difficult to analyse the problem
and the solution.
• It also gives us a reference to write more complex programs.
Need?
• The structure of a C program is crucial as it dictates how the code is
organized and executed.
• A well-structured program enhances readability, making it easier for
developers to understand, maintain, and debug.
• A clear structure ensures logical flow, improves code modularity, and
promotes code reusability. Moreover, following to a standardized
structure adopts collaboration among team members and facilitates
efficient project management.
• Sometimes, when we begin with a new programming language, we are not
aware about the basic structure of a program. The sections of a program
usually get shuffled and the chance of omission of error rises.
• The structure of a language gives us a basic idea of the order of the sections
in a program.
• We get to know when and where to use a particular statement, variable,
function, curly braces, parentheses, etc. It also increases our interest in that
programming language.

• Thus, the structure helps us analyse the format to write a program for the
least errors. It gives better clarity and the concept of a 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
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 that are mentioned below:

1. Documentation
2. Pre-processor / linking
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs
1-Documentation
• 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.
• Documentation 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.

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

• Anything written as comments will be treated as documentation of the program and this will not
interfere with the given code. Basically, it gives an overview to the reader of the program.
2-linking
• Linking refers to adding necessary header files that are useful in your
code. 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 pre-processor to insert the
contents of the specified header file into the source code.
• 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.
3- Definition

• The define section contains of different constants declared using the


define keyword.
• The #define pre-processor is used to create a constant throughout the
program. Whenever this constant-name is encountered by the
compiler, it is replaced by the actual piece of defined code.
• Pre-processors 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.
• Pre-processor directives start with the ‘#’ symbol.
4-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.

• 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.
5-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 that
the program executed successfully.

• Any code that you want to execute when the program starts should be placed within the main() function.
6-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.
• Functions in C are subprograms return a value to the caller, while
procedures (also known as void functions) do not return a value. Both
functions and procedures can have parameters that are used to pass data
between the caller and the subprogram.
Example:

You might also like