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

Is it declaration or defination?

P: n/a
Hi
main() {
int x; /* it declaration or defination??*/
}

Aug 18 '06 #1
Share this Question
Share on Google+
25 Replies


P: n/a

venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.

Aug 18 '06 #2

P: n/a
venky schrieb:
Hi
main() {
int main(void) {
int x; /* it declaration or defination??*/
}
http://en.wikipedia.org/wiki/Declara...ter_science%29

Decide yourself.

--
Thomas
Aug 18 '06 #3

P: n/a

int x; /*is a declaration */

Aug 18 '06 #4

P: n/a
declaration

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

int x; is always a definition;
extern int x; may be a declaration.
Aug 18 '06 #5

P: n/a
venky wrote:
int x; /* it declaration or defination??*/
It is both.

x is declared, and storage is assigned, which makes it a definition.

Aug 18 '06 #6

P: n/a
eahaa wrote:
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.
Aug 18 '06 #7

P: n/a
cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
>venky wrote:
>> main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a 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**);
Aug 18 '06 #8

P: n/a
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
Both.

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

Aug 18 '06 #9

P: n/a
On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
<ve****************@gmail.comwrote:
>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
Aug 18 '06 #10

P: n/a
Mark McIntyre wrote:
On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
<ve****************@gmail.comwrote:
>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.
Hi Mark,

Now I'm wondering:

How would another definition in the same scope be syntactically valid?
Aug 18 '06 #11

P: n/a
Mark McIntyre <ma**********@spamcop.netwrites:
On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
<ve****************@gmail.comwrote:
>>
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.
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.

--
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.
Aug 18 '06 #12

P: n/a
On Fri, 18 Aug 2006 13:46:15 -0700, in comp.lang.c , jmcgill
<jm*****@email.arizona.eduwrote:
>Mark McIntyre wrote:
>On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
<ve****************@gmail.comwrote:
>>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.

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
Aug 18 '06 #13

P: n/a
On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:
cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.

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!
Aug 19 '06 #14

P: n/a
Herbert Rosenau wrote:
On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:
>cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.

The top-poster says it's a declaration, which it is,
[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!
Aug 19 '06 #15

P: n/a

the line given "int x;" is declaration of variable "x".

Aug 19 '06 #16

P: n/a
venky wrote:
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 19 '06 #17

P: n/a
defination
venky wrote:
Hi
main() {
int x; /* it declaration or defination??*/
}
Aug 19 '06 #18

P: n/a
Herbert Rosenau wrote:
>>>int x; is always a definition;
extern int x; may be a declaration.
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.
You unfairly snipped part of my message. The definition is also a
declaration.
Aug 19 '06 #19

P: n/a
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. )

rain.man wrote:
defination
venky wrote:
Hi
main() {
int x; /* it declaration or defination??*/
}
Aug 19 '06 #20

P: n/a
On Sat, 19 Aug 2006 06:17:32 UTC, Harald van Dk <tr*****@gmail.com>
wrote:
Herbert Rosenau wrote:
On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:
cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.


The top-poster says it's a declaration, which it is,
[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!
Learn the difference between a declaration and a definition.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 20 '06 #21

P: n/a
On Sat, 19 Aug 2006 16:19:13 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:
Herbert Rosenau wrote:
>>int x; is always a definition;
extern int x; may be a declaration.
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.

You unfairly snipped part of my message. The definition is also a
declaration.
No, a declaration does not assign memory, a definition does.
Definitions and declarations are clearly different.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 20 '06 #22

P: n/a
Herbert Rosenau wrote:
On Sat, 19 Aug 2006 06:17:32 UTC, Harald van Dk <tr*****@gmail.com>
wrote:
>Herbert Rosenau wrote:
On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:

cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.
The top-poster says it's a declaration, which it is,
[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!

Learn the difference between a declaration and a definition.
I suggest you do. "Definition" in this context is defined in n1124 6.7p5,
and it is very clear: it does not allow for /any/ definitions (of
identifiers) that are not also declarations.
Aug 20 '06 #23

P: n/a

"Herbert Rosenau" <os****@pc-rosenau.deha scritto nel messaggio
news:wm***************************@JUPITER1.PC-ROSENAU.DE...
On Sat, 19 Aug 2006 16:19:13 UTC, jmcgill <jm*****@email.arizona.edu>
>You unfairly snipped part of my message. The definition is also a
declaration.

No, a declaration does not assign memory, a definition does.
Definitions and declarations are clearly different.

--
Tschau/Bye
Herbert
He meant that when you define something, you declare it too, so they are
related
Aug 20 '06 #24

P: n/a
"Herbert Rosenau" <os****@pc-rosenau.dewrites:
On Sat, 19 Aug 2006 06:17:32 UTC, Harald van Dk <tr*****@gmail.com>
wrote:
>Herbert Rosenau wrote:
On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:
cs*****@gmail.com wrote:
declaration

lovecreatesbeauty wrote:
venky wrote:
main() {
int x; /* it declaration or defination??*/
}
int x; is always a definition;
extern int x; may be a declaration.
The top-poster says it's a declaration, which it is,
[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!

Learn the difference between a declaration and a definition.
I suggest you do so yourself.

Here's what jmcgill wrote:

| 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**);

You misleadingly quoted only part of that: "The top-poster says it's a
declaration, which it is,".

C99 6.7p5:

A declaration specifies the interpretation and attributes of a set
of identifiers. A definition of an identifier is a declaration for
that identifier that:
-- for an object, causes storage to be reserved for that object;
-- for a function, includes the function body;99)
-- for an enumeration constant or typedef name, is the (only)
declaration of the identifier.

All definitions are declarations. jmcgill was right. You were wrong.

--
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.
Aug 20 '06 #25

P: n/a
Herbert Rosenau wrote:
On Sat, 19 Aug 2006 06:17:32 UTC, Harald van D�k <tr*****@gmail.com>
wrote:
>Herbert Rosenau wrote:
>>On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <jm*****@email.arizona.edu>
wrote:

cs*****@gmail.com wrote:
declaration
>
lovecreatesbeauty wrote:
>venky wrote:
>> main() {
>> int x; /* it declaration or defination??*/
>>}
>int x; is always a definition;
>extern int x; may be a declaration.
The top-poster says it's a declaration, which it is,
[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!

Learn the difference between a declaration and a definition.
Here is the thing:
Definitions are also declarations.
(the converse is not true, ofcourse)
Aug 21 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.