The-structure-of-a-C-program (1)
The-structure-of-a-C-program (1)
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.
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
*/
Preprocessor 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 is typically used to make a source program easy to modify and compile in different execution environments.
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.
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)
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.
This function is called in the main function. It returns an integer to the main function.
Below is the simple structure of c program with example code to calculate our age.
Code:
/** //Documentation
* file: age.c
* author: you
*/
return 0;
https://www.scaler.com/topics/c/online-c-compiler/?content_slug=92ad13dacda3d4d5b1fe
// Documentation
/**
* file: sum.c
* author: you
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
// 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
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.