What is the Difference Between Declaration and Definition in C

September 2022 · 3 minute read

The main difference between declaration and definition in C is that declaration of a variable indicates the compiler of the existence of a variable, while the definition of a variable indicates the compiler where and how much storage to create for a variable.

Generally, a variable is a name of the storage location in the memory. It is possible to change the value stored in that variable in the program. Furthermore, each variable has a variable type which describes the type of data the variable can store. For example, the programmer can use ‘int’ type to store integers and ‘float’ type to store decimal values. Moreover, there is a set of values we can store in a specific variable. Overall, it is possible to declare and define a variable.

Key Areas Covered

1. What is a Declaration in C
     -Definition, Functionality
2. What is a Definition in C
    -Definition, Functionality
3. Difference Between Declaration and Definition in C
    -Comparison of key differences

Key Terms

C, Compiler, Declaration, Definition, Variable

Difference Between Declaration and Definition in C - Comparison Summary

What is a Declaration in C

The declaration of a variable provides information to the compiler about the variable type and the name. In other words, this information indicates the compiler of the existing variable. Therefore, the compiler can proceed further compilation without all the details about the variable.

Variable declaration is useful when the programmer uses multiple files and when he defines the variables in one of the files. They are available when linking the program. He can use the “extern” keyword to declare a variable in any place. Even though it is possible to declare a variable multiple times in a C program, it is only possible to define it only once such as in a file or a function.

Difference Between Declaration and Definition in C

Figure 1: Declaration in C

In the above program, line 3 declares the variables. They are defined and initialized in the main function. The values of num1 and num2 are calculated and assigned to the result variable. Finally, the sum displays on the console.

What is a Definition in C

The definition of a variable indicates the compiler where and how much storage to create for the variable. A variable definition defines the data type and a set of one or more variables of that type. Below is an example of a declaration.

int a, b;

It declares and defines the variables a and b. It tells the compiler to create three integer variables called a and b.

Initialization is the process of assigning initial values to the variables. It is also possible to initialize values to the variables in the declaration. An example is as follows.

int a= 3, b=3;

Difference Between Declaration and Definition in C

Definition

The declaration is a statement that assures the compiler of the existing variable so that the compiler can proceed for further compilation without requiring the complete details about the variable. On the other hand, the definition is a statement that explains the compiler on where and how much storage to create for the variable. Thus, this is the main difference between Declaration and Definition in C.

Basis

Moreover, the declaration indicates the compiler of the existence of a variable, whereas the definition indicates the compiler where and how much storage to create for a variable.

Conclusion

A variable can be declared and defined in C language. Furthermore, it is possible to declare and define functions. The main difference between Declaration and Definition in C is that declaration of a variable indicates the compiler about the name and the type of the variable, while the definition of a variable indicates the compiler where and how much storage to create for a variable.

References:

1.“Variables in C – Javatpoint.” www.javatpoint.com, Available here.

ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2ZsqS4wKuYraGfo3qiusNmm56emaO2tbXOp2Sipl2YfA%3D%3D