473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 1192
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1629
by: Razvan | last post by:
Hi ! Consider the following XML element: <!ELEMENT value (#PCDATA)> <!ATTLIST value year CDATA #IMPLIED>
8
2446
by: newmans | last post by:
Perhaps one of the experts can straighten me out on this point... In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition, section 4.9, indicates that typedef complex<short>...
19
2869
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
10
20835
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...
2
2674
by: xllx.relient.xllx | last post by:
I have a few quetions about definitions vs declarations concerning defined types (user defined), specifically classes. From what I understand, a class contained in a header file is a definition as...
8
1688
by: caferias | last post by:
Hello, I'm curious about the difference between the term "declaration" and "definition" in the context of XML. Where is the difference? Greetings: Fernando
9
8863
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...
2
4682
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
15
14889
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
0
7226
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
7125
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
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
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,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.