Skip to main content

Declaration vs definition; C program structure

Short answer

A declaration introduces a name/type without memory allocation (extern int counter; int add(int,int);), while a definition allocates memory or provides the function body (int counter=10; int add(int a,int b){...}). A C program typically consists of documentation, link, definition, global declaration, main(), and user-defined function sections.

Differentiate between declaration and definition. Explain structure of C program with an appropriate example.

Declaration versus definition

DeclarationDefinition
Tells the compiler the name and typeAllocates memory or gives the function body
Can be repeatedDone only once
extern int counter;, int add(int,int);int counter = 10;, int add(int a,int b){...}
#include <stdio.h>
extern int counter;          /* declaration only: no memory allocated here */
int add(int, int);           /* function declaration (prototype) */
int counter = 10;            /* definition: memory allocated, value given */
int main(void)
{
    int x;                   /* declaration that is also a definition */
    x = add(counter, 5);
    printf("%d\n", x);
    return 0;
}
int add(int a, int b)        /* function definition */
{
    return a + b;
}

Output: 15

Structure of a C program

 Documentation section   (comments about the program)
 Link section            (#include header files)
 Definition section      (#define constants/macros)
 Global declaration      (global variables, prototypes)
 main() function         (program starts here)
     declaration part
     executable part
 User-defined functions  (function definitions)

Example:

/* Program to find area of a rectangle */      /* documentation section */
#include <stdio.h>                             /* link section */
#define UNIT "sq. units"                       /* definition section */
float area(float, float);                      /* global declaration */
int main(void)                                 /* main function */
{
    float l = 5, b = 3, a;
    a = area(l, b);
    printf("Area = %.1f %s\n", a, UNIT);
    return 0;
}
float area(float l, float b)                   /* user-defined function */
{
    return l * b;
}

Output:

Area = 15.0 sq. units

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 3 questions →