영어로 쓰기/C

C -Declaration and Definition

나맘임 2025. 4. 10. 17:04

In C programming, declaration and definition are different concepts taht serve different purposes.

Here's a detailed explanation

 

Declaration

First of all, A declaration is kind of blueprint for a house. It specifies the structure, such as the number of rooms, dimensions, and materials, but it does not actually build the house. Similarly, in c language, a declaration tells the compiler code structure. A declaration introduces a variable, function, or type to the compiler, specifying its name and attributes (such as type) but does not allocate memory or provide implementation details.

"Don't allocate memory"
it is important point of Declaration.

Declaration: Purpose

To inform the compiler about the existence and type of an entity.

Declaration: Why c compiler uses declaration?

(1) Compiler Awareness

Declarations inform the compiler about the existence and attributes of an identifier, such as its type, name, and scope.

This allows the compiler to understand how the identifier should be used in the program without requiring its full definition at the point.

 

(2) Single-Pass Compilation

Early C compilers were designed to process code in a single pass, meaning they did not look ahead to find definitions later in the code. So declarations were required before any usage to errors during compilation.

 

(3) Separation of Interface and Implementation

Declarations enable separation between interface and implementation, which is especially useful in large programs with multiple source files. For example, .h file so-called Header files contain declarations, allowing other files to use functions or variables without needing direct access to their definitions.

Declaration: Characteristics

  • Declarations can used by many different files like interface.
  • Does not allocate storage for variables unless it is combined with a definition.
  • For functions, it specifies the function's name, return type, and parameters but does not include the function body.

Declaration: Examples

File: myHeader.h

#ifndef MYHEADER_H
#define MYHEADER_H

// Function declaration
int add(int a, int b);

extern int x;

#endif

 

File: myFunctions.c

#include "myHeader.h"

int add(int a, int b) {
    return a + b;
}

 

File: main.c

#include <stdio.h>
#include "myHeader.h"

int main() {
    int sum = add(5, 7);
    printf("The sum is: %d\n", sum);
    return 0;
}

Definition

A definition provides all necessary information to allocate memory for a variable or implement a function. It creates the entity in its entirety. At the beginning of the article, Declaration is blueprint but Definition is actual construction of the house based on the blueprint. This involves allocating resources (materials, land, labor) and physically building the house. In C language, A definition allocates memory for variables or provides the full implementation of functions.

Definition: Purpose

To allocate storage for variables or provide the implementation of functions.

Definition: Characteristics

  • A defintion implicitly acts as a declaration.
  • For variables, it allocates memory and may optionally initialize the.
  • For functions, it includes the function body (implementation)

Definition: Examples

Variable Definition

int x = 10;

 

Function Definition

int add(int a, int b) {
    return a + b;
}

 


Conclusion

We talked about difference between declaration and definition. When you use c lang, this concept helps you understanding C code.

 

 

 

'영어로 쓰기 > C' 카테고리의 다른 글

[영어로 쓰기]C – Dynamic Memory Allocation  (0) 2025.04.11