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

Discussion in 'C Programming' started by sushant, Jan 20, 2005.

  1. sushant

    sushant Guest

    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....
    sushant, Jan 20, 2005
    #1
    1. Advertising

  2. sushant

    Richard Bos Guest

    "sushant" <> 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
    Richard Bos, Jan 20, 2005
    #2
    1. Advertising

  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
    Lawrence Kirby, Jan 20, 2005
    #3
    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. Jianli Shen
    Replies:
    1
    Views:
    581
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    413
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    442
    Jon Slaughter
    Oct 26, 2005
  4. Replies:
    1
    Views:
    373
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 10, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    481
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page