By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,951 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,951 IT Pros & Developers. It's quick & easy.

is the definition of declaration is contradictory?????????

P: n/a
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;
printf("%p",p);
return 0;
}

should generate an error bcos x is only declared not defined... but
after executing the above code it was showing some address. the story
is not yet finished... after initialising x with some value , it was
showig the same address....
any answer for that....

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"sushant" <th********@rediffmail.com> wrote:
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;
Yech - Google-unindented code!
p=&x;
printf("%p",p);
return 0;
}

should generate an error bcos x is only declared not defined...


Wrong. That's not just a declaration of x (and p), it's a definition as
well. If you want to declare, but not define, a variable, declare it
using extern.

(BTW, for fully conforming code, you should cast that pointer to void *
before printf()ing it using "%p".)

Richard
Nov 14 '05 #2

P: n/a
sorry for that unindented code, but its not mentioned any where that
for declaration we have to use extern keyword. i just want to declare a
local variable for that matter ...... now what??
Richard Bos wrote:
"sushant" <th********@rediffmail.com> wrote:
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;
Yech - Google-unindented code!
p=&x;
printf("%p",p);
return 0;
}

should generate an error bcos x is only declared not defined...


Wrong. That's not just a declaration of x (and p), it's a definition

as well. If you want to declare, but not define, a variable, declare it
using extern.

(BTW, for fully conforming code, you should cast that pointer to void * before printf()ing it using "%p".)

Richard


Nov 14 '05 #3

P: n/a
On Thu, 20 Jan 2005 03:23:05 -0800, sushant wrote:
sorry for that unindented code, but its not mentioned any where that
for declaration we have to use extern keyword. i just want to declare a
local variable for that matter ...... now what??


C does not permit you to just declare local variables, all such
declarations are also definitions. Why would you want to?

Note that you can declare something like:

int main(void)
{
extern int x;
...
}

Here the scope of this declaration (which is not a definition) is local
to the block but the variable x isn't, this declaration will be "linked"
with other declarations and definition of x in the entire program that
have external linkage. E.g. if the following is in the same program, not
necessarily the same source file

int foo(void)
{
extern int x;
}

then the x in this function refers to the same object as the x in main().
There needs to be a definition for x somewhere but that has to be at file
scope, it cannot be within a function.

Lawrence
Nov 14 '05 #4

P: n/a
"sushant" <th********@rediffmail.com> writes:
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;
printf("%p",p);
return 0;
}

should generate an error bcos x is only declared not defined... but
after executing the above code it was showing some address. the story
is not yet finished... after initialising x with some value , it was
showig the same address....


You're misunderstanding the term "definition". The definition of x is
the thing allocates space for it. Assigning a value to x is not a
definition, it's just an assignment.

With a function, this:
int x;
is both a declaration and a definition of x. This:
extern int x;
is a declaration but not a definition. It declares that x is defined
somewhere else. That's not a useful (or possible) thing to do for a
local variable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.