Structured Programming
Structured programming is a programming approach that emphasizes writing clear, organized, and logical code by dividing a program into smaller, manageable sections or functions. It uses three main control structures—sequence, selection, and iteration—to control the flow of the program instead of using unstructured jumps like goto statements. This method promotes top-down design, making programs easier to read, debug, test, and maintain. It is commonly used in languages such as C, Pascal, and Python, and forms the foundation of modern programming practices.
3. PROGRAM STRUCTURE
Introduction to program structure
Program structure The overall form of a program, with particular emphasis on the individual
components of the program and the interrelationships between these components. Programs are
frequently referred to as either well-structured or poorly structured.
Format of a structured programming language
A C program basically has the following form:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
The following program is written in the C programming language. Open a text file hello.c using
vi editor and put the following lines inside that file.
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello, World! \n");
return 0;
}
Preprocessor Commands: These commands tells the compiler to do preprocessing before doing
actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler
to include stdio.h file before going to actual compilation. You will learn more about C
Preprocessors in C Preprocessors session.
Functions: are main building blocks of any C Program. Every C Program will have one or more
functions and there is one mandatory function which is called main() function. This function is
prefixed with keyword int which means this function returns an integer value when it exits. This
integer value is retured using return statement.
The C Programming language provides a set of built-in functions. In the above example printf()
is a C built-in function which is used to print anything on the screen.
Variables: are used to hold numbers, strings and complex data for manipulation.
Page 36 of 172
Statements & Expressions : Expressions combine variables and constants to create new values.
Statements are expressions, assignments, function calls, or control flow statements which make
up C programs.
Comments: are used to give additional useful information inside a C Program. All the comments
will be put inside /*...*/ as given in the example above. A comment can span through multiple
lines.
Note:
There should be a main function somewhere in the program to determine where to
start the executions.
Usually all C statements are entered in small case letters.
The group of statements in main are executed sequentially.
The left brace indicates the program opening.
The right brace indicates the program closing.
In C language Comments are enclosed with /* -- */ means these statements won‘t execute
when the program is complied.
A C program consists of one or more functions. Each function performs a specific task. A
function is a group or sequence of C statements that are executed together.
Every C program starts with a function called main(). This is the place where program
execution begins. Hence, there should be main() function in every C program. The functions are
building blocks of C program. Each function has a name and a list of parameters.
The following are some rules to write C programs
1. All C statements must end with semicolon.
2. C is case – sensitive. That is, upper case and lower case characters are different.
Generally the statements are typed in lower case.
3. A C statement can be written in one line or it can split into multiple lines.
4. Braces must always match upon pairs, i.e., every opening brace ‗{‗ must have a matching
closing brace ‗}‘.
5. A comment can be split into more than one line.
Page 37 of 172