Is it declaration or defination?

Discussion in 'C Programming' started by venky, Aug 18, 2006.

  1. venky

    venky Guest

    Hi


    main() {
    int x; /* it declaration or defination??*/
    }
     
    venky, Aug 18, 2006
    #1
    1. Advertising

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


    int x; is always a definition;
    extern int x; may be a declaration.
     
    lovecreatesbeauty, Aug 18, 2006
    #2
    1. Advertising

  3. Thomas J. Gritzan, Aug 18, 2006
    #3
  4. venky

    eahaa Guest

    int x; /*is a declaration */
     
    eahaa, Aug 18, 2006
    #4
  5. venky

    Guest

    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, 2006
    #5
  6. venky

    jmcgill Guest

    venky wrote:
    > int x; /* it declaration or defination??*/


    It is both.

    x is declared, and storage is assigned, which makes it a definition.
     
    jmcgill, Aug 18, 2006
    #6
  7. venky

    jmcgill Guest

    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.
     
    jmcgill, Aug 18, 2006
    #7
  8. venky

    jmcgill Guest

    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**);
     
    jmcgill, Aug 18, 2006
    #8
  9. venky

    whyglinux Guest

    venky wrote:

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


    Both.

    Definition is a special case of declaration, that is, all definitions
    are also declarations.
     
    whyglinux, Aug 18, 2006
    #9
  10. On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
    <> wrote:

    >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
     
    Mark McIntyre, Aug 18, 2006
    #10
  11. venky

    jmcgill Guest

    Mark McIntyre wrote:
    > On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
    > <> wrote:
    >
    >> 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?
     
    jmcgill, Aug 18, 2006
    #11
  12. Mark McIntyre <> writes:
    > On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
    > <> wrote:
    >>
    >> 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) <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.
     
    Keith Thompson, Aug 18, 2006
    #12
  13. On Fri, 18 Aug 2006 13:46:15 -0700, in comp.lang.c , jmcgill
    <> wrote:

    >Mark McIntyre wrote:
    >> On 18 Aug 2006 00:49:34 -0700, in comp.lang.c , "venky"
    >> <> wrote:
    >>
    >>> 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
     
    Mark McIntyre, Aug 18, 2006
    #13
  14. On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <>
    wrote:

    > 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!
     
    Herbert Rosenau, Aug 19, 2006
    #14
  15. Herbert Rosenau wrote:
    > On Fri, 18 Aug 2006 14:52:40 UTC, jmcgill <>
    > wrote:
    >
    >> 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!
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 19, 2006
    #15
  16. venky

    rani Guest

    the line given "int x;" is declaration of variable "x".
     
    rani, Aug 19, 2006
    #16
  17. venky

    Joe Wright Guest

    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 ---
     
    Joe Wright, Aug 19, 2006
    #17
  18. venky

    rain.man Guest

    defination


    venky wrote:
    > Hi
    >
    >
    > main() {
    > int x; /* it declaration or defination??*/
    > }
     
    rain.man, Aug 19, 2006
    #18
  19. venky

    jmcgill Guest

    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.
     
    jmcgill, Aug 19, 2006
    #19
  20. venky

    cman Guest

    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??*/
    > > }
     
    cman, Aug 19, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cvsta
    Replies:
    0
    Views:
    2,311
    cvsta
    Jul 12, 2004
  2. Guo Congbin
    Replies:
    4
    Views:
    3,947
    Ioannis Vranos
    Jun 17, 2004
  3. scl

    two defination ?

    scl, Mar 19, 2005, in forum: C++
    Replies:
    8
    Views:
    2,820
    modemer
    Mar 21, 2005
  4. sunnylele

    A question on variable defination

    sunnylele, Mar 31, 2006, in forum: C Programming
    Replies:
    3
    Views:
    341
    Eric Sosman
    Mar 31, 2006
  5. somenath

    extern function defination

    somenath, Feb 20, 2007, in forum: C Programming
    Replies:
    8
    Views:
    447
    Default User
    Feb 21, 2007
Loading...

Share This Page