Is it declaration or defination?

J

jmcgill

eahaa said:
int x; /*is a declaration */

Inside the function body it is also a definition, since it's not
declared "extern." The example was inside some function called main,
which is a separate issue.
 
J

jmcgill

declaration

The top-poster says it's a declaration, which it is, but it is also a
definition (not a "defination").

The declaration of the function in its definition in the example should
be according to one of the the prototypes int main(void); or int
main(int, char**);
 
W

whyglinux

venky said:
main() {
int x; /* it declaration or defination??*/
}

Both.

Definition is a special case of declaration, that is, all definitions
are also declarations.
 
M

Mark McIntyre

Hi


main() {
int x; /* it declaration or defination??*/
}

Its a declaration and a tentative definition, which will become a firm
definition if no other definition is encountered.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jmcgill

Mark said:
Its a declaration and a tentative definition, which will become a firm
definition if no other definition is encountered.

Hi Mark,

Now I'm wondering:

How would another definition in the same scope be syntactically valid?
 
K

Keith Thompson

Mark McIntyre said:
Its a declaration and a tentative definition, which will become a firm
definition if no other definition is encountered.

It could only be a tentative definition if it were at file scope.
It's inside a function, so it's a definition (of an object with
automatic storage duration) -- and, like all definitions, it's also a
declaration.
 
M

Mark McIntyre

Hi Mark,

Now I'm wondering:

How would another definition in the same scope be syntactically valid?

I misspoke myself. Inside a function, it wouldn't.

At file-scope however, if another appeared, the first would become
just a declaration.

void foo()
{
int x; // declaration and definition
int x; // illegal
}

int x; // declaration and tentative definition
int x; // firm definition

void foo(){}



--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

Herbert Rosenau

The top-poster says it's a declaration, which it is,

No, it is at block level, so it is clearly a _definition_ of an
uninitialised variable of type int named x on storage class auto.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
H

Harald van =?UTF-8?B?RMSzaw==?=

[un-snipped] but it is also a definition (not a "defination").
No, it is at block level, so it is clearly a _definition_ of an
uninitialised variable of type int named x on storage class auto.

What exactly was your point? You left out the part of jmcgill's message
where he states it is a definition, so that you could tell him that it is a
definition? He clearly already knows that!
 
J

Joe Wright

venky said:
Hi


main() {
int x; /* it declaration or defination??*/
}

A declaration is simply information for the compiler. A definition
causes the compiler to allocate memory. A definition is also a declaration.

In your example, within a function block, 'int x;' is a definition
because the compiler must allocate space for it.

Outside any function block, 'int x;' is a declaration and a tentative
definition. If, outside any function block, 'int x = 0;' the definition
is no longer tentative. It's a definition.
 
J

jmcgill

Herbert said:
No, it is at block level, so it is clearly a _definition_ of an
uninitialised variable of type int named x on storage class auto.

You unfairly snipped part of my message. The definition is also a
declaration.
 
C

cman

When you say x is of type int , it is a declaration.

Along with that, you have allocated memory for this variable, hence it
is a definition.

Hence it is both declaration and definition here.

It is better to consider automatic variables as definition. ( whatz the
use of declaration for automatic variables. )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top