473,396 Members | 2,017 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.

declaration and definitions

Expand|Select|Wrap|Line Numbers
  1. int a;
The above snippet is a declaration. Whether it is a definition too.....
Because one website that i referred tells that only
Expand|Select|Wrap|Line Numbers
  1. extern int i;
is a declaration.
All the other types are both declaration and definition.
Is it true?
If yes kindly clarify when a variable is declared and when the same is defined?
Thanks in advance.......
Aug 27 '10 #1

✓ answered by weaknessforcats

A variable is defined when it occupies memory.

Otherwise, it is declared.

Therefore:

1) int i; is a definition. The variable i is created and occupies memory.

2) extern int i; is a declaration. The variable i is an int but is is not in this source file. That is, it is defined in some ther file with external linkage.

3) extern int i = 10; is a definition. A variable i is reated with a value of 10. The variable is to be accessible from other files that use extern int i;

Based on (3), you can define variables that are accessible from other files that use extern. This is important for const variables which are by default not accessible from other files by using extern. That is you define your const variable with external linkage:

extern const int i = 10;

Now other files can access this variable by using:

extern const int i;

1 1203
weaknessforcats
9,208 Expert Mod 8TB
A variable is defined when it occupies memory.

Otherwise, it is declared.

Therefore:

1) int i; is a definition. The variable i is created and occupies memory.

2) extern int i; is a declaration. The variable i is an int but is is not in this source file. That is, it is defined in some ther file with external linkage.

3) extern int i = 10; is a definition. A variable i is reated with a value of 10. The variable is to be accessible from other files that use extern int i;

Based on (3), you can define variables that are accessible from other files that use extern. This is important for const variables which are by default not accessible from other files by using extern. That is you define your const variable with external linkage:

extern const int i = 10;

Now other files can access this variable by using:

extern const int i;
Aug 27 '10 #2

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

Similar topics

2
by: Chris Gordon-Smith | last post by:
I am currently in India and have treated myself to the Indian reprint of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to 1/2 of the price in Britain.) I thought that I would...
6
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...
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...
5
by: Kobu | last post by:
Does anyone know how old style function definitions differ in "behaviour" to new style function definitions? I heard the old style function definitions caused integeral promotion and floating...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
3
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
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...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
11
by: mdh | last post by:
I decided to make a single file containing all the repetitive functions in K&R so that I could concentrate on the new discussions. This went along just fine, and with each new function, added the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.