Skip to study material
← Question index

QUESTION 40 · C fundamentals

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

  1. Header inclusion: #include <stdio.h> makes declarations for standard input/output functions available. The directive does not print anything by itself.
  2. Main function: int main(void) defines main with no parameters and an int return value. Execution begins here in the hosted environment.
  3. Braces: Opening and closing braces mark the beginning and end of a block.
  4. Declarations: A declaration introduces a name and its type, such as int first = 8;.
  5. Statements: Statements perform actions. For example, total = first + second; calculates and stores a sum.
  6. 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
PartPurpose
#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
MistakeWhy it mattersCorrection
Writing Printf instead of printfC identifiers are case-sensitive.Use the declared function name.
Omitting a closing braceThe block structure becomes invalid.Match every opening brace.
Using void main in these examplesIt is not the standard no-argument hosted form used here.Use int main(void).
Placing a semicolon after every line indiscriminatelyDirectives and block syntax have their own rules.Understand what construct each line belongs to.

C program and output

/* 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.