Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Question on defining or declaring a variable

"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
simi
Asked:
simi
  • 2
  • 2
1 Solution
 
mlevCommented:
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
 
simiAuthor Commented:

 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
 
q2guoCommented:
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
 
simiAuthor Commented:
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
 
q2guoCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now