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
3.1. Basic C Programs - Variable and I/O instructions
1) Print Hello Word
Let us look at a simple code that would print the words "Hello World":
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Let us look various parts of the above program:
1. The first line of the program #include <stdio.h> is a preprocessor command, which tells
a C compiler to include stdio.h file before going to actual compilation.
2. The next line int main() is the main function where program execution begins.
3. The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program. So such lines are called comments in the program.
4. The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.
5. The next line return 0; terminates main()function and returns the value 0.
Note: Compile & Execute C Program:
Let‘s look at how to save the source code in a file, and how to compile and run it. Following are
the simple steps:
1. Open a text editor and add the above-mentioned code.
2. Save the file as hello.c
3. Open a command prompt and go to the directory where you saved the file.
4. Type gcc hello.c and press enter to compile your code.
5. If there are no errors in your code the command prompt will take you to the next line and
would generate a.out executable file.
6. Now, type a.out to execute your program.
7. You will be able to see "Hello World" printed on the screen
2) Declaring Variable Inputing and Outputting Value
A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in C has a specific type, which determines the size and layout of the variable's
memory; the range of values that can be stored within that memory; and the set of operations
that can be applied to the variable.
Page 38 of 172
a) Working with variables - Let's look at an example of how to declare an integer variable in
the C language, assign value and read the value in the variable to output.
For example:
int age;
In this example, the variable named age would be defined as an int.
Below is an example C program where we declare this variable:
#include <stdio.h>
int main()
{
int age;
age = 10;
printf("TechOnTheNet.com is over %d years old.\n", age);
return 0;
}
This C program would print "TechOnTheNet.com is over 10 years old."
Conversion specifiers are made up of two characters: % and a special character.
The special character tells the program how to convert the data.
Conversion Specifier Description
%d
%f
%c
Displays integer value
Displays floating-point numbers
Displays character
Format specifiers are the operators used in printf() function to print the data which is referred by an
object or a variable. when a value is stored in a particular variable, Then we cannot print the value
stored in the variable directly with out the using format specifiers. We can retrieve the data stored in
the variables and can print them on to the console screen by using these format specifiers in a printf
function. Format specifiers start with a percentage(%) symbol and follows a special character to identify
the type of the data. There are basically six types of format specifiers are available in c they are
%d - represent integer values
%f - represent float values
%c - represent single character values
%s - represent string values
%u - represent the address of a variable
%ld – represent Long integer values
Page 39 of 172
More examples: Use the printf function with formatting options.
#include <stdio.h>
main(){
int x;
float y;
char c;
x = -4443;
y = 554.21;
c = 'M';
printf("\nThe value of integer variable x is %d", x);
printf("\nThe value of float variable y is %f", y);
printf("\nThe value of character variable c is %c\n", c);
}
The value of integer variable x is -4443
The value of float variable y is 554.210022
The value of character variable c is M
More examples: Use the scanf function with formatting options assign to assign value to
variables.
This C program would print "Your Name is Kim and you age is over 10 yrs old."
#include <stdio.h>
main(){
int Age;
char Name[10];
printf("\nPlz enter your Name ");
scanf(“%s”,&Name);
printf("\nPlz enter your Age", y);
scanf(“%d”,&Age):
printf("\nYour Name is %s and your age is %d yrs old\n",Name,Age);
}
Page 40 of 172