Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A C program comprises six essential sections i.e Documentation, Link definition, Global Declaration, Main() Function, and Subprograms that contribute to its structure and functionality.
Overall, C is a powerful language that has been used to develop a wide range of software applications and systems. While it may take some time to master, it is a valuable skill for any programmer to have. But if your basics are clear, then you can master the C language easily.
Let us start by understanding the Structure of C Program.
What is the structure of the C program?
The structure of a C program typically follows a sequential order of statements, with the program starting at the main function and proceeding line by line until it reaches the end of the function. There are a set of defined parts in a Structure of C program, which we are going to discuss in this section.
Importance of structure of a C program
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. It typically consists of sections such as preprocessor directives, global variable declarations, function prototypes, the main function, and user-defined functions. A clear structure ensures logical flow, improves code modularity, and promotes code reusability. Moreover, adhering to a standardized structure fosters collaboration among team members and facilitates efficient project management.
Sections of the C Program
A C program is divided into six sections:
Documentation
Link, Definition
Global Declaration
Main() function
Subprograms
Six major sections, each with a specific function, make up the structure of a C program. The interesting fact is that only the main() is mandatory; the rest are optional. That means you can skip writing other parts if not required.
Let us now understand each section separately:
Documentation
Documentation in C programming refers to the practice of adding comments and other information to your code to explain its purpose, design, and usage.
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.
Sample Documentation
// C program to print the Pyramid Pattern
// Author: Alisha
Or
/* C program to print the Pyramid Pattern
Author : Alisha
*/
In the above example, by adding comments, we can make the code more readable. It is a good practice to add comments whenever required.
Adding comments can make the code readable to other programmers as well. Henceforth, documentation is recommended to add to make your code beautiful and understandable.
Link
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 preprocessor to insert the contents of the specified header file into the source code.
Sample Linking
#include <stdio.h>
#include <math.h>
<stdio.h>: The stdio.h header file contains declarations for input and output functions, such as printf and scanf. Including this header file is necessary if you want to use these functions in your C program.
<math.h>: The math.h header file contains declarations for mathematical functions, such as trigonometric functions, logarithmic functions, and other mathematical operations. Including this header file is necessary if you want to use these functions in your C program.
Definition
It includes a preprocessor directive, which as a result, contains symbolic constants.
For example: #define allows us to use constants in our code. It replaces all the constants with its value in the code.
The #define preprocessor directive in C is used to define symbolic constants, also known as macros. A macro is a piece of code that represents a value or expression, and it can be used anywhere in the program where that value or expression is needed.
For example, you can define a macro like this:
#define PI 3.141592653589793
This associates the name 'PI' with the value of pi. Then, you can use this macro in your program:
double area = PI * radius * radius;
When the program is compiled, the preprocessor replaces every occurrence of 'PI' with its value, so the above line of code would be equivalent to:
double area = 3.141592653589793 * radius * radius;
Using macros in this way can make your code more readable and easier to maintain because you can define meaningful names for constants that are used throughout your program.
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.
A global variable is declared outside of a function, typically at the top of the program file, and it can be used by any function in the program.
Let us understand with one story:
The boy requested the rope from a nearby function. The rope cannot be shared because of the local declaration.
The global declaration is where we can access the variable from inside the function.
For example
C
C
#include <stdio.h>
int count = 0; // global variable void increment() { count++; } int main() { increment();
In this example, the variable 'count' is declared as a global variable outside of any function. The 'increment' function can access and modify the value of 'count', and the 'main' function can also access and print the value of 'count'.
Note: Global declarations can be useful in some cases, but they should be used with caution because they can make it harder to reason about the behavior of the program and can introduce unintended side effects.
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. For example, you might print a message to the console, read input from the user, or call other functions that perform some task.
Now, what if we don’t have to return any value? For that case, we can add void as a return type on the place of int.
Here is the syntax:
void main(){
// code goes here
}
The void in the function indicates that no value is returned after the execution. You can use it with your sub programs as well.
Here's an example of a simple C program that uses the main() function to print a message to the console:
C
C
#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
This program uses the printf() function from the stdio.h library to print the message "Hello, world!" to the console. When the program is executed, the main() function is called, the message is printed to the console, and the program exits with a return value of 0.
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.
Functions in C are subprograms that 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.
Here's an example of a function in C:
C
C
#include <stdio.h> int sum(int a, int b) { return a + b; } int main() { int x = 3, y = 4; int result = sum(x, y); printf("The sum of %d and %d is %d\n", x, y, result); return 0; }
In this example, the sum() function takes two integer parameters and returns their sum. The main() function calls the sum() function with the values of x and y and then prints the result to the console.
In this example, the print_hello() procedure prints the message "Hello, world!" to the console. The main() function calls the print_hello() procedure to print the message to the console.
Subprograms in C are a powerful way to organize code into reusable and modular components, making programs easier to read, maintain, and debug.
Let us now see how to compile and execute a C program in your local machine.
To compile and execute a C program, you can follow these steps:
Step 1: Open a text editor (e.g. Notepad, Sublime Text, Visual Studio Code) and create a new file with a .c extension (e.g. hello.c). This file will contain your C code.
Step 2: Write your C code in the file. For example, you could copy and paste the code from the above code.
Step 3: Save the file.
Step 4: Open a command prompt (Windows) or terminal (Mac/Linux) and navigate to the directory where you saved the file.
Step 5: Compile the C program by running the following command:
gcc -o hello hello.c
This will generate an executable file called hello from the C source code in hello.c.
Step 6: Run the executable file by entering the following command:
./hello
This will execute the hello program and display the output in the command prompt or terminal.
That's it! You've now compiled and executed a C program.
Note that these instructions assume that you have a C compiler installed on your system (such as gcc), which is necessary to compile C code into an executable file.
If you don't have a C compiler installed, you'll need to install one before you can compile and execute C programs.
Let us now see some faqs based on the discussion above.
In this example, the structure of the C program is evident. It begins with including necessary header files (#include <stdio.h>), followed by function prototypes (void greet();). The main() function serves as the entry point, calling the greet() function, which is defined later. This organization ensures clarity and readability, facilitating understanding of the program's functionality and promoting maintainability.
Frequently Asked Questions
How does C support structured programming?
C supports structured programming using functions and control structures like if-else, for, and while loops.
How to print a structure in C?
To print a structure in C, use the printf() function with appropriate format specifiers for each member of the structure. For example: printf("%d %s", structVar.member1, structVar.member2);
Why C is a structure program?
C is known as a structured programming language because it emphasizes organizing code into well-defined structures. It promotes using functions, loops, and conditionals, creating clear and maintainable code.
What is array of structure in C?
An array of structures in C is a collection of elements, where each element is a structure containing multiple data members. It allows easy storage and manages related data as a single unit, making it easier to access.
Conclusion
In this article, we discussed the structure of C program that contains Documentation, Link, definition, Global Declaration, Main() function, subprograms, and Compiles and Executes a C program. Further, we discussed the brief introduction to C and details about the structure of C program.
If you wish to learn more about C, you can refer to blogs on: