0% found this document useful (0 votes)
3 views3 pages

C Program Structure

Uploaded by

balakumaran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

C Program Structure

Uploaded by

balakumaran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Study Material: Structure of a C Program

1. Documentation Section
Includes comments to explain what the program does.
Starts with /* ... */ or //

Example:
/* Program to find the largest of two numbers */

2. Preprocessor Section
Contains header files that provide built-in functions.
Written using #include.

Example:
#include <stdio.h> // standard input-output functions

3. Global Declaration Section (optional)


Variables or functions declared outside main().
Accessible throughout the program.

Example:
int max; // global variable

4. main() Function Section


Every C program must have main() → program starts execution here.
Contains declarations and executable statements.

Example:
int main() {
int a, b; // local variable declaration
// statements
}

5. Variable Declaration Section


Inside main(), we declare variables before using them.

Example:
int a, b, max;

6. Executable Statements Section


Actual logic of the program → calculations, decisions, loops, output, etc.

Example:
scanf("%d %d", &a, &b);
if(a > b)
max = a;
else
max = b;
printf("Largest = %d", max);

7. Subprogram Section (optional)


User-defined functions, written outside main().

Example:
int add(int x, int y) {
return x + y;
}

Complete Example Program


/* Program to find the largest of two numbers */

#include <stdio.h> // Preprocessor section

// Global Declaration (optional)


// int max;

int main() { // main() function starts


int a, b, max; // variable declaration

// input
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

// logic
if(a > b)
max = a;
else
max = b;

// output
printf("Largest number = %d", max);

return 0; // end of main()


}
// end of program
Summary Table
Section Purpose

Documentation Comments (explain program)

Preprocessor Include header files

Global Declaration Declare global variables/functions

main() Program starts here

Variable Declaration Define variables inside main

Executable Statements Write program logic

Subprograms Extra user-defined functions

You might also like