473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Is it declaration or defination?

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

Aug 18 '06 #1
25 2768

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

int x; /*is a declaration */

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

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

Aug 19 '06 #16
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
defination
venky wrote:
Hi
main() {
int x; /* it declaration or defination??*/
}
Aug 19 '06 #18
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
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
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
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
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

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

Similar topics

4
by: Guo Congbin | last post by:
·¢¼þÈË: "Guo Congbin" <guocongbin@sei.buaa.edu.cn> Ö÷Ìâ: is size_t a keyword?or a macro? ÈÕÆÚ: 2004Äê6ÔÂ17ÈÕ 23:04 if size_t is a macro, where is the defination? i look it up in the stddef.h,...
8
by: scl | last post by:
two class with same name exist in different dynamic linked library: a.so class REGION() { public: .... ~REGION() {} } b.so
1
by: DIA | last post by:
Can we find the defination of a SQL stored procedure using c#? 1. Using SQL-DMO in c# I was able to locate a SQL server in the network. 2. Hit the target database in the selected server. 3....
18
by: shaanxxx | last post by:
I have following code . #include<stdio.h> int i; int i; int i; int main() { printf("%d",i); return 0;
1
by: Johnny E. Jensen | last post by:
Hello I have a class that inherence from the ToolStrip control called (AWToolbar), and i added my own methods to that class. Then i have a usercontroll (QuickNavigator), and I add the AWToolbar,...
6
by: romeshkumar | last post by:
what is the difference between declaration and defination?
2
by: Gunners | last post by:
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct st_base { char name; int roll_no; int s1,s2,s3,s4,mks; float percentage;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.