
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between a definition and a declaration in C++?
In C++, declaration and definition are often confused. A declaration tells the compiler about the name and type, while a definition allocates memory or provides implementation. In this article, we will understand their differences with examples.
What is a Declaration in C++?
A declaration means (in C or C++) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration.
Following is the syntax to the declaration of variable, function and structure in C++:
// Variable extern data_type variable_name; // Structure struct StructName; // Function return_type function_name(parameter_list);
Following is the declarations in C++:
// Declaring a variable a without defining it extern int a; // Declaring a struct struct _tagExample { int a; int b; }; // Declaring a function int myFunc (int a, int b);
Example
In this example, the Declaration (extern, function prototype) tells the compiler: "This variable/function exists somewhere.", but does not allocate memory or provide implementation to the program. So, that is reason the program fails at runtime.
#include<iostream> using namespace std; // Declaration of a variable extern int num; // Declaration of a function int add(int, int); int main() { int result = add(num, 10); cout<<"Result = "<<result<<endl; return 0; }
Following is the output to the above program:
/usr/bin/ld: /tmp/ccztBu7g.o: warning: relocation against 'num' in read-only section '.text' /usr/bin/ld: /tmp/ccztBu7g.o: in function 'main': main.cpp:(.text+0xe): undefined reference to 'num' /usr/bin/ld: main.cpp:(.text+0x1a): undefined reference to 'add(int, int)' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE collect2: error: ld returned 1 exit status
What is Definition in C++?
The Definition on the other hand means that in additions to all the things that declaration does, space is additionally reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION".
Following is the syntax to the definition of variable, function and structure in C++:
// Variable data_type variable_name; data_type variable_name = value; // structure struct StructName { // members }; // Function return_type function_name(parameter_list) { // Function body }
Following is the defining a definitions in C++.
// defining a variable int a; int b = 0; // defining a function int myFunc (int a, int b) { return a + b; } // defining a struct struct _tagExample example;
Example
In this program, the variable 'num' is defined with a value of 60, and the function 'add()' is also fully defined with its logic. Both memory for num and code for add() are available, so the program runs correctly and prints the result of 60+10=70.
#include<iostream> using namespace std; // Definition (memory allocated) int num = 60; // Definition (implementation provided) int add(int a, int b) { return a + b; } int main() { int result = add(num, 10); // Now, num and add() exist in memory! cout<<"Result = "<<result<<endl; return 0; }
Following is the output to the above program:
Result = 70