0% found this document useful (0 votes)
2 views

The-structure-of-a-C-program (1)

The document outlines the structure of a C program, which consists of six key sections: Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. It explains the purpose of each section, including the use of comments, header files, constants, global variables, and user-defined functions, with examples provided for clarity. The document emphasizes the importance of the Main() function as the starting point of execution in a C program.

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

The-structure-of-a-C-program (1)

The document outlines the structure of a C program, which consists of six key sections: Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. It explains the purpose of each section, including the use of comments, header files, constants, global variables, and user-defined functions, with examples provided for clarity. The document emphasizes the importance of the Main() function as the starting point of execution in a C program.

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

The structure of a C program, foundational to the basic structure of C programming, is categorized into six key sections: Documentation, Link,

Definition, Global Declaration, Main() Function, and Subprograms. The Main() function is essential in every C program, highlighting what is
required in each C program. This organization facilitates readability, modification, and documentation, providing a clear structure of C language
with examples to illustrate the concepts.

Sections of the C Program

Documentation

In a C program, single-line comments can be written using two forward slashes i.e., //, and we can create multi-line comments using /* */. Here,
we've used multi-line comments.

/**

* file: age.c

* author: you

* description: program to find our age.

*/

Preprocessor Section

All header files are included in this section.

A header file is a file that consists of C declarations that can be used between different files. It helps us in using others' code in our files. A copy of
these header files is inserted into your code before compilation.

#include <stdio.h>

Definition

A preprocessor directive in C is any statement that begins with the "#" symbol. The #define is a preprocessor compiler directive used to create
constants. In simple terms, #define basically allows the macro definition, which allows the use of constants in our code.

#define BORN 2000


We've created a constant BORN which is assigned a value of 2000. Generally, uppercase letters are preferred for defining the constants. The
above constant BORN will be replaced by 2000 throughout our code wherever used.

#define is typically used to make a source program easy to modify and compile in different execution environments.

The define statement does not end with a semicolon.

Global Declaration

This section includes all global variables, function declarations, and static variables. The variables declared in this section can be used anywhere
in the program. They're accessible to all the functions of the program. Hence, they are called global variables.

int age(int current);

We've declared our age function, which takes one integer argument and returns an integer.

Main Function

In the structure of a C program, this section contains the main function of the code. The C compiler starts execution from the main() function. It
can use global variables, static variables, inbuilt functions, and user-defined functions. The return type of the main() function can be void and also
not necessarily int.

int main(void)

int current = 2021;

printf("Age: %d", age(current));

return 0;

Here, we've declared a variable named current and assigned the value as 2021. Then we've called the printf() function, with calls the age()
function, which takes only one parameter.
Sub programs

This includes the user-defined functions called in the main() function. User-defined functions are generally written after the main() function
irrespective of their order.

When the user-defined function is called from the main() function, the control of the program shifts to the called function, and when it
encounters a return statement, it returns to the main() function. In this case, we've defined the age() function, which takes one parameter, i.e.,
the current year.

int age(int current) {

return current - BORN;

This function is called in the main function. It returns an integer to the main function.

Structure of C Program with Example

Below is the simple structure of c program with example code to calculate our age.

Code:

/** //Documentation

* file: age.c

* author: you

* description: program to find our age.

*/

#include <stdio.h> //Link

#define BORN 2000 //Definition


int age(int current); //Global Declaration

int main(void) //Main() Function

int current = 2021;

printf("Age: %d", age(current));

return 0;

int age(int current) { //Subprograms

return current - BORN;

https://www.scaler.com/topics/c/online-c-compiler/?content_slug=92ad13dacda3d4d5b1fe
// Documentation

/**

* file: sum.c

* author: you

* description: program to find sum.

*/

// Link

#include <stdio.h>

// Definition

#define X 20

// Global Declaration

int sum(int y);

// Main() Function

int main(void)

int y = 55;
printf("Sum: %d", sum(y));

return 0;

// Subprogram

int sum(int y)

return y + X;

COMPILER

Output

Age: 21

Explanation of the Example

Sections Description

/** <br>* file: age.c<br>* author: you<br>* It is the comment section and part of the documentation of the code, providing a brief
description: program to find our age.<br>*/ overview including the file name, author, and purpose of the program.

This header file is included for standard input-output operations. It is part of the
#include <stdio.h>
preprocessor directive section.

This definition section defines constants to be used throughout the code, here defining
#define BORN 2000
the year of birth.
Sections Description

This global declaration section includes the declaration of the age function, which can be
int age(int current)
used anywhere within the program.

int main(void) main() is the entry point of the C program and is the first function to be executed.

These curly braces denote the beginning and end of the main function and also enclose
{...}
the body of the age function.

The printf() function within the main function is used to display the calculated age on the
printf("Age: %d", age(current));
screen.

This statement is used to indicate that the program has executed successfully without
return 0;
any errors. It marks the exit point of the main function.

This is the subprogram section where the age function is defined. It calculates the age by
int age(int current) { return current - BORN; }
subtracting the defined birth year from the current year passed as an argument.

You might also like