Explain the structure of a C program with a complete example.
5 marks
Answer in brief
A simple hosted C program contains header inclusion, the main function, declarations and executable statements.
Parts of a C program
Header inclusion: #include <stdio.h> makes declarations for standard input/output functions available. The directive does not print anything by itself.
Main function: int main(void) defines main with no parameters and an int return value. Execution begins here in the hosted environment.
Braces: Opening and closing braces mark the beginning and end of a block.
Declarations: A declaration introduces a name and its type, such as int first = 8;.
Statements: Statements perform actions. For example, total = first + second; calculates and stores a sum.
Return statement: return 0; reports successful termination to the host environment.
Explanation of the example
The program creates first and second with values 8 and 5. It adds them and stores 13 in total. printf displays that value using %d. The newline moves output to the next line. The program then returns zero.
Comments and punctuation
Comments explain the program and are not executed. Declarations and expression statements normally end in semicolons. A preprocessing directive such as #include does not need a semicolon. Match every opening brace with a closing brace.
Important points
C is case-sensitive, so Printf is not the same name as printf. Use the proper header for a library function. Global variables, macros and helper functions are included only when needed; they are not compulsory sections in every program. A larger program can contain several functions and source files.
Reference tables
↔ Scroll horizontally on a small screen
Parts of the example
Part
Purpose
#include <stdio.h>
Makes declarations for standard input/output facilities available.
int main(void)
Defines main with an int result and no parameters.
{ and }
Delimit the function body.
int first = 8;
Declares and initialises a local variable.
printf(...)
Writes formatted output.
return 0;
Indicates successful termination to the host environment.
↔ Scroll horizontally on a small screen
Problems in a first program
Mistake
Why it matters
Correction
Writing Printf instead of printf
C identifiers are case-sensitive.
Use the declared function name.
Omitting a closing brace
The block structure becomes invalid.
Match every opening brace.
Using void main in these examples
It is not the standard no-argument hosted form used here.
Use int main(void).
Placing a semicolon after every line indiscriminately
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
int first = 8;
int second = 5;
int total = first + second;
printf("Total = %d\n", total);
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
Total = 13
Key points
Use int main(void) for these no-argument hosted examples.
Match braces and terminate declarations and expression statements correctly.