473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Difference between Declaration and Definition of a variable?

Hello all,

I am despondent to handle difference between declaration and definition of a variable. I have been totally confused.

Please help me,

Thanx in advance

Suyash
Jun 7 '07 #1
6 24660
AdrianH
1,251 Expert 1GB
Hello all,

I am despondent to handle difference between declaration and definition of a variable. I have been totally confused.

Please help me,

Thanx in advance

Suyash
Declaration of a variable is stating that it exists.
Definition of a variable says it exists and it is here, thus it implies that it is declared.

That doesn't only relate to variables but functions, structs/classes. enums... did I forget anything?

The following are declared but not defined:
Expand|Select|Wrap|Line Numbers
  1. extern int var;
  2. void function();
  3. struct myStruct;
  4. class myClass;
  5. enum myEnum;
  6.  
The following are defined:
Expand|Select|Wrap|Line Numbers
  1. int var;
  2. void function()
  3. {
  4. }
  5.  
  6. struct myStruct {
  7. // stuff in here
  8. };
  9.  
  10. class myClass {
  11. // stuff in here
  12. };
  13.  
  14. enum myEnum {
  15. // stuff in here
  16. };
  17.  
Hope that helps.


Adrian
Jun 7 '07 #2
Thanx Adrian,

But can you please tell me

Why extern int var; is declared and int var; is defined?

Thanx;

Suyash
Jun 7 '07 #3
AdrianH
1,251 Expert 1GB
Thanx Adrian,

But can you please tell me

Why extern int var; is declared and int var; is defined?

Thanx;

Suyash
That is how C/C++ defines it. extern can be used for functions and probably the others as well, it is just not needed.


Adrian
Jun 7 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Quote:
Originally Posted by Suyash Upadhyay
Thanx Adrian,

But can you please tell me

Why extern int var; is declared and int var; is defined?

Thanx;

Suyash

That is how C/C++ defines it. extern can be used for functions and probably the others as well, it is just not needed.
extern is called a storage class specifier. It is used to tell the compiler that the variable or function has external linkage. That is, that it can be accessed from another file. There are various storage class specifers:

Expand|Select|Wrap|Line Numbers
  1. extern int var;   //declaration. var is an int in another file with external linkage
  2. extern int var = 20;  //definition. var is created here with a value of 20
  3.                              //and is to accessible from other files.
  4. int var = 20;     //definition. Same as extern int var = 20;
  5. static int var = 20;  //definition. var is an int with a value of 20 that has internal linkage
  6.                             //var CANNOT by accessed from another file.
  7.  
Jun 7 '07 #5
AdrianH
1,251 Expert 1GB
extern is called a storage class specifier. It is used to tell the compiler that the variable or function has external linkage. That is, that it can be accessed from another file. There are various storage class specifers:

Expand|Select|Wrap|Line Numbers
  1. extern int var;   //declaration. var is an int in another file with external linkage
  2. extern int var = 20;  //definition. var is created here with a value of 20
  3.                              //and is to accessible from other files.
  4. int var = 20;     //definition. Same as extern int var = 20;
  5. static int var = 20;  //definition. var is an int with a value of 20 that has internal linkage
  6.                             //var CANNOT by accessed from another file.
  7.  
Yeah, I'm lazy today. :D ;)


Adrian
Jun 7 '07 #6
then how come this code

int a;

produces only

.file "a.c"
.comm a,4,4
.ident "GCC: (GNU) 4.0.0 20050519 (Red Hat 4.0.0-8)"
.section .note.GNU-stack,"",@progbits

in its assembly ie no storage allocation. Clearly this is only a declaration
Apr 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gianni Mariani | last post by:
I posted a gcc bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332 here is the text: ----------------------------------------------------------- This code will compile fine but fail on...
5
by: ideas2050 | last post by:
Observed a compilation error reporting differences between VC++ 6.0 and 7.0 VS .NET 2003. Seeking opinions. Here is the code abbreviated:- main() {
5
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
4
by: sushant | last post by:
according to the definition of declaration of a variable "the variable is not allocated any space in the memory till it is defined". so the code: int main(void) { int x,*p; p=&x;...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book and sometimes they use the term declaration and sometimes they use the term definition. I just wonder if there exist any kind of definition about what exactly a...
5
by: arun121185 | last post by:
I have been studying c language in depth and i could not find the difference between declaration and definition. my lecturers said when a variable is decleared space will be alloted for that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.