Solved

Question on defining or declaring a variable

Posted on 1998-01-10
5
225 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
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:mlev
Comment Utility
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
Comment Utility

 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
memory mapped I/O query 6 136
Estimate porting issues. 9 116
negation in C function 14 132
Socket Programming (Unix) 8 98
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now