Computer Science SSC II Notes - AKUEB
8.3.2 Describe the structure of a C program consisting of:
a. pre-processor directives
i. include
ii. define
b. main ( ) function
c. body of main { }
d. global and local variables;
A typical C
program consists of several components, including pre-processor directives, the
main function, the body of the main function, and global and local variables.
a. Pre-processor
directives:
Pre-processor
directives are instructions for the pre-processor, a program that processes the
source code before it is compiled. Two commonly used pre-processor directives
are:
i.
Include:
The `#include` directive is used to include header files in your
program. Header files contain declarations and definitions that are needed by
the program. For example:
#include <stdio.h>
ii.
Define:
The `#define`
directive is used to create symbolic constants or macros. It allows you to
define a name and associate it with a value or a code snippet that will be
replaced by the pre-processor. For example:
#define PI 3.14159
~Defines the value of pi=3.14159
b. Main ( )
function:
The
`main()` function is the entry point
of a C program. It is mandatory and serves as the starting point of execution.
The main function has a specific signature and should return an integer value.
For example:
main()
{
**Code goes here
return 0;
}
c. Body of main { }:
The
body of the `main()` function is
enclosed within curly braces `{}`.
It contains the executable statements that define the behavior of the program.
This is where you write the code that performs the desired tasks. For example:
main()
{
printf("Hello, world!");
return 0;
}
d. Global and local
variables:
Variables
in C can be declared at either the global or local level.
Global
variables are declared outside of any function and have a global scope, meaning they can be
accessed from any part of the program. They retain their values throughout the
execution of the program. For example:
int globalVariable = 10;
Local
variables are declared within a function or a block of code. They
have a local scope, meaning they are
only accessible within the function or block where they are declared. Local
variables are created when the function or block is entered and destroyed when
it is exited. For example:
int main() {
int localVariable = 5;
**Code goes here
return 0;
}
That's a basic structure of a C
program with pre-processor directives, the main function, the body of the main
function, and global and local variables. Keep in mind that this is a
simplified overview, and C programming allows for more complex structures and
concepts.
.png)
.png)
Comments
Post a Comment