- 2071 Shrawan · 4 marks
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
| Declaration | Definition |
|---|---|
| Tells the compiler the name and type | Allocates memory or gives the function body |
| Can be repeated | Done 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)
Related questions
- Variable declaration versus definition · Computer Programming, chapter 3
- Structure of a FORTRAN program · Computer Programming, chapter 11 · asked 5×
- Structure tag · Computer Programming, chapter 8
- Structure vs array; accessing members · Computer Programming, chapter 8
- Function definition; sum of two numbers · Computer Programming, chapter 6
- Sending structure members to a function · Computer Programming, chapter 8
- Appropriate vs inappropriate human resources · Organization and Management, chapter 7
- Org structure definition; line organization pros/cons · Organization and Management, chapter 5
- Flowchart vs structure chart · Object Oriented Analysis and Design, chapter 1
- Monolithic vs micro-kernel structure · Operating System, chapter 1