---
title: "Variable declaration versus definition"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:declaration-vs-definition"
source: https://www.bctengineer.com/q/semester_1/computer_programming/declaration-vs-definition
site: BCT Engineering
---

# Variable declaration versus definition

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2074 Chaitra, 4 marks
- Video: [Declaration vs. Definition of a variable in C](https://www.youtube.com/watch?v=TtJw4VUYsiM) — CodeVault
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/declaration-vs-definition
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

What is the difference between variable declaration and variable definition? Explain with example.

## Short answer

A declaration introduces a variable’s or function’s name and type without allocating memory; it can appear multiple times. A definition allocates memory or gives the function body and occurs only once. For example, extern int x; is a declaration; int x = 10; is a definition.

## Answer

A **declaration** tells the compiler the name and type of a variable or function, without necessarily allocating memory. A **definition** creates the entity: it allocates memory (and may give an initial value) or provides the function body.

| Declaration | Definition |
|---|---|
| Introduces name and type | Allocates memory / gives function body |
| No memory is reserved (for `extern` and prototypes) | Memory is reserved |
| Can appear many times | Appears only once |
| Example: `extern int counter;` | Example: `int counter = 10;` |
| Example: `int add(int, int);` | Example: `int add(int a, int b) { return a + b; }` |

In C, `int x;` inside a function is both a declaration and a definition.

```c
#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:
```text
15
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does a declaration do?
   A. Allocates memory always
   B. Tells the compiler the name and type
   C. Gives the function body
   D. Deletes a variable
   Answer: B. A declaration introduces name and type without necessarily allocating memory. (Key point: Declaration)

2. Which is a definition?
   A. extern int counter;
   B. int add(int, int);
   C. int counter = 10;
   D. None of these
   Answer: C. int counter = 10; allocates memory and gives a value. The others are declarations only. (Key point: Definition)

3. How many times can each appear?
   A. Declaration many times, definition only once
   B. Both only once
   C. Declaration once, definition many times
   D. Both many times
   Answer: A. A name may be declared repeatedly but defined only once. (Key point: How many times)

4. What is int x; inside a function?
   A. A declaration only
   B. A definition only
   C. Both declaration and definition
   D. An error
   Answer: C. A local declaration also allocates memory, so it is a definition too. (Key point: Local variable)

## Related in Computer Programming

- [Declaration vs definition; C program structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adeclaration-definition-structure-of-c) — Computer Programming, chapter 3
- [Why datatype in pointer declaration](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-datatype-needed) — Computer Programming, chapter 9

## Related in other subjects

- [Negative binomial: definition and difference](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Anb-define-differ-binomial) — Probability and Statistics, chapter 2, asked 4×
- [Potential difference versus emf](https://www.bctengineer.com/question.md?key=semester_1%2Fbasic_electrical_engineering%3Apotential-difference-vs-emf) — Basic Electrical Engineering, chapter 1
- [Fibonacci difference equation: closed form and limit](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Afibonacci-general-solution) — Digital Control System, chapter 2, asked 2×
- [Third-order difference equation: limit x(k+2)/x(k)](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Athird-order-difference-limit-k) — Digital Control System, chapter 2, asked 2×
