The Structure of A C Program Means The Specific Structure To Start The Programming in The C Language
The Structure of A C Program Means The Specific Structure To Start The Programming in The C Language
language. Without a proper structure, it becomes difficult to analyze the problem and the solution. It
also gives us a reference to write more complex programs.
C programming
C language combines the power of a low-level language and a high-level language. The low-level
languages are used for system programming, while the high-level languages are used for application
programming. It is because such languages are flexible and easy to use. Hence, C language is a widely
used computer language.
It supports various operators, constructors, data structures, and loop constructs. The features of C
programming make it possible to use the language for system programming, development of
interpreters, compilers, operating systems, graphics, general utilities, etc. C is also used to write
other applications, such as databases, compilers, word processors, and spreadsheets.
Memory allocation: At the time of definition, memory is assigned to a variable name, allowing
dynamic allocation of the memory. It means that the program itself can request the operating system
to release memory for use at the execution time.
Bit-manipulation: It refers to the manipulation of data in its lowest form. It is also known as bits. The
computer stores the information in binary format (0 and 1).
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 analyze the format to write a program for the least errors. It gives better
clarity and the concept of a program.
Here, we will discuss the sections of a C program, some practical examples with explanations, steps
to compile and execute a C program.
Sections of a C program
The sections of a C program are listed below:
1. Documentation section
2. Preprocessor section
3. Definition section
4. Global declaration
5. Main function
Documentation section
It includes the statement specified at the beginning of a program, such as a program's name, date,
description, and title. It is represented as:
1. //name of a program
Or
1. /*
3. .
4. */
Both methods work as the document section in a program. It provides an overview of the program.
Anything written inside will be considered a part of the documentation section and will not interfere
with the specified code.
Preprocessor section
The preprocessor section contains all the header files used in a program. It informs the system to link
the header files to the system libraries. It is given by:
1. #include<stdio.h>
2. #include<conio.h>
The #include statement includes the specific file as a part of a function at the time of the
compilation. Thus, the contents of the included file are compiled along with the function being
compiled. The #include<stdio.h> consists of the contents of the standard input output files, which
contains the definition of stdin, stdout, and stderr. Whenever the definitions stdin, stdout, and stderr
are used in a function, the statement #include<stdio.h> need to be used.
There are various header files available for different purposes. For example, # include <math.h>. It is
used for mathematic functions in a program.
Define section
The define section comprises of different constants declared using the define keyword. It is given by:
1. #define a = 2
Global declaration
The global section comprises of all the global declarations in the program. It is given by:
2. int a = 5;
3. char ch ='z';
char = 1 byte
float = 4 bytes
int = 4 bytes
We can also declare user defined functions in the global variable section.
Main function
main() is the first function to be executed by the computer. It is necessary for a code to include the
main(). It is like any other function available in the C library. Parenthesis () are used for passing
parameters (if any) to a function.
1. main()
We can also use int or main with the main (). The void main() specifies that the program will not
return any value. The int main() specifies that the program can return integer type data.
1. int main()
Or
1. void main()
Main function is further categorized into local declarations, statements, and expressions.
Local declarations
The variable that is declared inside a given function or block refers to as local declarations.
1. main()
2. {
3. int i = 2;
4. i++;
5. }
Statements
The statements refers to if, else, while, do, for, etc. used in a program within the main function.
Expressions
An expression is a type of formula where operands are linked with each other by the use of
operators. It is given by:
1. a - b;
2. a +b;
The user defined functions specified the functions specified as per the requirements of the user. For
example, color(), sum(), division(), etc.
The program (basic or advance) follows the same sections as listed above.
Return function is generally the last section of a code. But, it is not necessary to include. It is used
when we want to return a value. The return function returns a value when the return type other
than the void is specified with the function.
Return type ends the execution of the function. It further returns control to the specified calling
function. It is given by:
1. return;
Or
1. return expression ;
For example,
return 0;
Examples
1. #include<stdio.h>
2. int main()
3. {
4. int a, b, sum;
7. // calculating sum
8. sum = a + b;
11. }
Output
It is given by:
2.
4. #include<conio.h>
6. {
9. /* puts accepts, as parameter, a string constant, or a variable enclosed within the double quo
tes for display on the standard output*/
10.
12. /* It accepts a parameter and allows the character to be read during the program execution.
*/
15. {
16. puts("-----------");
18. puts("-----------");
19. }
20. else if(num=='2') /*if-else performs two different operations depending on the true or false c
ondition of the expression.*/
21. {
22. puts("-----------");
25. puts("-----------");
26. }
28. {
29. puts("-----------");
33. puts("-----------");
34. }
35. else
36. {
38. }
39. }
We have saved the file with the name boxexample.c. Let's compile and execute the code with the
help of the command prompt. The file was created in the Notepad.
Output
The steps to create, compile, and execute the code from the beginning have been explained later in
the topic. It will help us compile any type of C code with the help of text editor (Notepad here)
and cmd (Command Prompt).
We can collectively say that the program includes preprocessor commands, variables, functions,
statements, expressions, and comments.
Here, we will discuss the method to compile and run the C program with the help of the command
prompt.
1. Create a program
2. Compile a program
Create a program
It refers to the code created in any text editor. We can also compile and run the C code in any
software, such as Visual studio.
Compile a program
If refers to the process of checking the errors in the code. The computer displays all the errors (if any)
found in our specified C code. We can make further changes to correct those errors.
The next step is the run or execution part. A program is assembled and linked without any error. The
computer performs different operations, such as decoding, ALU operations to run a program.
It is the last part of the program where the output of the specified code is produced as the result.
But, where to write a program and how to open a command prompt to run that program. Do not
worry; it is very easy to do all these steps. Let's start with the step to compile and run a C program.
Step to compile and run a C program
We need first to ensure that the gcc compiler is already present on our PC or not. If it is not installed,
we need first to install the gcc compiler. We can also try other methods to run a C program. But here,
we have used the gcc compiler.
We can directly install the latest gcc complier through the link: https://jmeubank.github.io/tdm-gcc/
Complete all the steps during the installation till the process gets completed.
Create a C program using the simple text editor. Here, we have used notepad. It is shown below:
1. #include<stdio.h>
2. main()
3. {
5. return;
6. }
Now, save the file in any directory with the extension (.c). For example, we have saved the file with
the name 'welcome.c' on the desktop.
Open the cmd or command prompt on our computer. We can simply type cmd on the search or the
run option. The Command prompt will appear.
After the command prompt opens, type 'gcc -v' and press Enter. It will appear as the image shown
below:
We now need to specify the source directory on the cmd. Type 'cd space source directory' and
press Enter. Since, we have saved our text editor file on the desktop, so we will specify the source
directory as desktop. It is given by:
1. cd desktop
Run the command 'gcc space full name of the file saved with the extension (.c)' and press Enter, as
shown below:
1. gcc welcome.c
If there is any error in our file, it will appear. Otherwise, move on to the step 7.
The executable file is not yet named because we have not told the compiler to perform any such
task. So, we will first name the executable file by running the command 'gcc space -o space (name of
executable file) space (name of the original file with the extension)' and press Enter. It is given by:
Here, we have given the executable name as hello. We can define the name as per our convenience.
It is the last step to run a program. We will run the program in the command prompt only. Here, we
will type the name of the executable file without any extension. The executable name will be the
same as specified in step 7. It is given by: