Solved

Question on defining or declaring a variable

Posted on 1998-01-10
5
230 Views
Last Modified: 2010-04-02
"C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and
it generates only a single variable for the multiple definitions. C++, however, does not permit redefinition of a
variable or any other entity for a very definite reason that we will discuss later. " 

 This is what a C++ tutorial that I am learning from states.

 My question(s).
 
 1. What is the difference between declaring and defining a variable ?
 2. Why would someone want to define more then one time the same variable ?
 
 
0
Comment
Question by:simi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:mlev
ID: 1256963
For variables:
int x; /* definition */
extern int x; /* declaration */

For functions:
int f() { return 0; ] /* definition */ /* oops, this keyboard won't type braces */
int f(); /* declaration */
extern int f(); /* declaration */

There's no good reason, but it happens sometimes.
For example, some people define variables in header files
(though it's a bad thing to do). That results in multiple definitions.
0
 

Author Comment

by:simi
ID: 1256964

 This is a paragraph from the tutorial I am reffering to:

 "Examine the file named VARDEF.CPP for a few more additions to the C++ language which
aid in writing a clear and easy to understand program. In C++, as in ANSI-C, global and
static variables are automatically initialized to zero when they are declared. The variables
named index in line 4, and goofy in line 27 are therefore automatically initialized to zero. Of
course, you can still initialize either to some other value if you so desire. Global variables
are sometimes called external since they are external to any functions.

Automatic variables, those declared inside of any function, are not automatically initialized
but will contain the value that happens to be in the location where they are defined, which
must be considered a garbage value. The variable named stuff in line 8, therefore does not
contain a valid value, but some garbage value which should not be used for any meaningful
purpose. In line 11, it is assigned a value based on the initialized value of index and it is
then displayed on the monitor for your examination."

 This is the program is talking about:

 "                               // Chapter 1 - Program 5 - VARDEF.CPP
#include <iostream.h>

int index;

int main()
{
int stuff;
int &another_stuff = stuff; // A synonym for stuff

   stuff = index + 14;      //index was initialized to zero
   cout << "stuff has the value " << stuff << "\n";
   stuff = 17;
   cout << "another_stuff has the value " << another_stuff << "\n";

int more_stuff = 13;        //not automatically initialized

   cout << "more_stuff has the value " << more_stuff << "\n";
   
   for (int count = 3 ; count < 8 ; count++)
   {
      cout << "count has the value " << count << "\n";
      char count2 = count + 65;
      cout << "count2 has the value " << count2 << "\n";
   }
   
static unsigned goofy;      //automatically initialized to zero

   cout << "goofy has the value " << goofy << "\n";

   return 0;
}"

 mlev , you state in your answer:

 "sed Answer from mlev...
              For variables:
              int x; /* definition */
              extern int x; /* declaration */

              For functions:
              int f() { return 0; ] /* definition */ /* oops, this
              keyboard won't type braces */
              int f(); /* declaration */
              extern int f(); /* declaration */ "

 or that does not fit with what the turorial says, more preciselly :

 "Automatic variables, those declared inside of any function, are not automatically initialized
but will contain the value that happens to be in the location where they are defined, which
must be considered a garbage value."

 It looks to me like those automatic variables that are declared are not declared as extern, and still the tutorial talks about a declaration and not about a definition.

 About your function example, sorry, but I do not get it.

 What do you mean by int f(){return 0};
 (I think is that what you wanted to spell)
 And what is the difference between that and
 int f();

  ????

 Your second answer reffers to people declaring variables in headef files, while the tutorial is stating that the C language permits multiple definitions of a variable( that is what II do not understand).

 Thank's anyway



0
 
LVL 3

Accepted Solution

by:
q2guo earned 20 total points
ID: 1256965
The definition is the declaration that actually allocates space, and provides an initialization value, if any.
e.g.
struct foo{ int a; int b };  // this is a declaration
                             // not this statement doesn't
                             // make you compiler allocate
                             // space

struct foo c;                // this is a definition
                             // and it requires your compiler
                             // to allocate space

There can be many declarations in a single namespace of a variable, but there can only be one definition. (which makes
sense)

Your second question, I don't really know the answer.
but, it's got something to do with helping your compiler
catching error and following the ANSI standard.

Hope this helps

Terry
0
 

Author Comment

by:simi
ID: 1256966
That doesn't clear it.

 What about this:

 int i;
 int j = 2;
 struct STR
 {
  int k;
  char m;
 };

 struct STR STR1;

 typedef struct
 {
  int a;
  char b;
 }STR2;

 STR2 STR3;


 Which of those are definitions and wich declarations ?
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1256967
What about this:

                          int i;  // both
                          int j = 2;  // both
                          struct STR  // declaration
                          {
                           int k;
                           char m;
                          };

                          struct STR STR1;  // definition
                                            // memory space is
                                            // allocated

                          typedef struct  // declaration
                          {               // no memory space
                           int a;         // is allocated yet
                           char b;
                          }STR2;

                          STR2 STR3;      // definition
                                          // space is allocated

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 24
Detect CR LF to each line 12 185
Super Scope, DHCP 5 100
I want to transfer a file from source folder to destination folder. 35 70
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question