Structure of a C program

Structure of a C program

A user-created C program follows or uses a special, unique program syntax structure, which makes it easier for a new C language user to create, understand, and compile or maintain the source code. A user-created small C program may have only a few lines or code instructions defined. However, a large-scale program with hundreds or millions of lines of source code is well-managed and organized. A simple to advanced C program may typically have many sections and blocks created.

Structure of a C program

A simple C program structure.

A typical C program might look something like this.

/* C program documentation section */

#include <stdio.h>

/* Global program variable declaration area block */

int global_variable;

/* User-defined program function declaration area */

int total(int, int);

/* main() function */

int main()

{

/* Program single to multiple variable declaration area */

int p, q, output;

/* Main program process executable section */

p = 9;

q = 12;

output = total(p, q);

printf(“The output is = %d”, output);

return 0;

}

/* User-defined custom function calling section */

int total(int r, int s)

{

return r + s;

}

Let’s understand all sections of a C program.

Documentation program section.

This section of a custom C program contains user-defined program comments that indicate every aspect of the current C program, from start to end, including all processes. The C compiler ignores all user-defined custom comments, and documentation is written to make the source code syntax of both large and small C programs easier to understand.

There are two types of comments in a C program.

// This is a single-line C program comment

and

/*

This is a multi-line comment C program comment

*/

For example.

/* A program to calculate the sum of two integer data type number values ​​*/

Remember, the documentation section in C programming is an optional user choice. But it is useful for understanding large-scale program source code, especially when working on large-scale projects or millions of lines of program source code.

Preprocessor Section Concept in C Programming.

The preprocessor section in the C programming language defines preprocessor directives or header files. These header files or preprocessor directives process actual C program instructions before compilation into the program.

The most common preprocessor directive or header file in C programming is.

#include <stdio.h>

In C programming, stdio.h is a standard input/output header file or preprocessor directive. It provides input/output standard program functions like printf() and scanf() to any C program.

Other popular C programming header file or preprocessor directive examples include.

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

This header file or preprocessor directive includes the contents of the specified header file as a library.

C programming program definition section.

In a C program, this section is used to define or declare a custom constant data type variable value using the #define directive.

For example.

#define PI_Value 3.14

#define COUNT 200

This directive causes the preprocessor to replace the value PI with PI_Value 3.14 whenever it occurs in the current program.

Remember, this section is an optional choice in a C program.

Global C program variable declaration section.

In a C program, global variables and other program variable declarations that need to be accessed and processed by multiple functions in the current program can be displayed outside the function program declaration.

For example.

int total;

float temp;

In a C program, these are known as global variable declarations.

Functions can also be declared in the global variable declaration section.

int total(int, int);

This is also known as the prototype of a program function. It indicates to the C program compiler about the function before it is used.

main() function in C programming.

The main() function is one of the most important elements of any C program. The execution of every C program starts with the main() function.

A basic main() function in a C program looks like this.

int main()

{

return 0;

}

This indicates the int data type value in a C program, so that the main() function returns an integer value to the current program.

return 0; This is commonly used in every C program to indicate that the current program has successfully completed its run.

C Program Declaration Section.

The program variables required within any C program function are manually user-defined or declared in the declaration section.

For example.

int p, q, total;

float percent;

char text;

These user-defined variables in a C program can be used by the programmer through statements in the appropriate scope.

Example.

int main()

{

int p = 8;

int q = 3;

int total;

total = p + q;

printf(“%d”, total);

return 0;

}

C Program Executable Section.

The executable section in any C program contains user-defined custom program line statements that perform the actual actions in the C program.

These C program statements may mainly include the following.

  • Input Program Statement
  • Output Program Statement
  • Assignment Program Statement
  • Arithmetic Program Operation
  • Conditional Program Statement
  • Program Loop
  • Custom Function Call

For example.

total = p + q;

printf(“%d”, total);

These C program statements are executed when the C program is run or executed.

C Program User-Defined Function Section.

A C program may declare several custom functions created or defined by the user programmer.

For example.

int total(int m, int n)

{

return m + n;

}

A user-defined custom function in a C program helps facilitate execution by dividing a large program into smaller, more manageable portions.

A user-defined custom function typically contains the following.

return_type function_name(parameters)

{

// User-defined program statements

}

For example.

int square_value(int number)

{

return number * number;

}

A complete example in C programming.

Here we create a simple C program that explains all aspects of the current program.

/* Here we create a C program to add two different numbers*/

#include <stdio.h>

#define DISPLAY “Let’s add two integer numbers in C.”

int total(int, int);

int main()

{

int p, q, output;

p = 3;

q = 4;

output = total(p, q);

printf(“%s\n”, DISPLAY);

printf(“output = %d\n”, output);

return 0;

}

int total(int s, int t)

{

return s + t;

}

Explanation of a complete example of C programming.

  • /* This program to calculate… */ → Indicates documentation/comment in the current C program.
  • #include <stdio.h> → This is a preprocessor directive or header file declaration in a C program.
  • #define DISPLAY… → This is a macro/definition declaration method in a C program.
  • int total(int, int); → This is a function declaration/prototype method in a C program.
  • int main() → This is the main function declaration method in a C program.
  • int p, q, output; → This is a global variable declaration method in a C program.
  • output = total(p, q); → This is an executable statement/function call method in a C program.
  • printf() → This is for printing output statements in a C program.
  • return 0; → This successfully terminates main() in a C program.
  • int total(int s, int t) → This is a user-defined custom function calling method in a C program.

Leave a Reply