Structure of a C program
The structure of a C program means the specific structure to start the programming in the C 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.
Let's first discuss about 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.
The essential features of a C program are as follows:
Pointers: it allows reference to a memory location by the name assigned to it in a program.
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.
Recursion: When a function calls itself, it is known as recursion.
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).
Let's start with the importance of specifying the structure of a C program.
Importance of structure of a C program
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:
Let's discuss it in detail.
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:
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.
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:
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.
The define section comprises of different constants declared using the define keyword. It is given by:
The global section comprises of all the global declarations in the program. It is given by:
The size of the above global variables is listed as follows:
char = 1 byte
float = 4 bytes
int = 4 bytes
We can also declare user defined functions in the global variable section.
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.
The main function is declared as:
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.
Main function is further categorized into local declarations, statements, and expressions.
The variable that is declared inside a given function or block refers to as local declarations.
The statements refers to if, else, while, do, for, etc. used in a program within the main function.
An expression is a type of formula where operands are linked with each other by the use of operators. It is given by:
User defined functions
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:
Let's begin with a simple program in C language.
Example 1: To find the sum of two numbers given by the user
It is given by:
The detailed explanation of each part of a code is as follows:
Example 2: To draw a box by accepting a number from the user
It is given by:
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.
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.
Compile and execution of a C program
Here, we will discuss the method to compile and run the C program with the help of the command prompt.
The steps involved in a complete program execution are as follows:
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.
Run or execute a program
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.
Output of the 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.
Step: 1 - gcc complier installation
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.
Step: 2 - creating a C program
Create a C program using the simple text editor. Here, we have used notepad. It is shown below:
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.
Step: 3 - Opening the command prompt
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.
Step: 4 - rechecking the gcc complier
After the command prompt opens, type 'gcc -v' and press Enter. It will appear as the image shown below:
It shows that gcc is successfully installed on our PC.
Step: 5 - go to the source directory
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:
Step: 6 - compile the source code
Run the command 'gcc space full name of the file saved with the extension (.c)' and press Enter, as shown below:
If there is any error in our file, it will appear. Otherwise, move on to the step 7.
Step: 7 - Compile the source code
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.
Step: 8 - Run the program
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:
Press Enter. The output will appear on the command prompt, as shown below:
Similarly, we can run multiple C programs using the same steps, as discussed above.