Find the answer to your Linux question:
Results 1 to 5 of 5
#include<stdio.h> int a; int a=2; int main() { printf("A %d\n",a); return 0; } no error here #include<stdio.h> int a=3; int a=2; int main() { printf("A %d\n",a); return 0; } Error ...
  1. #1
    Just Joined! vinall's Avatar
    Join Date
    Jun 2006
    Location
    bangalore,INDIA
    Posts
    42

    Redeclaration can any body explain

    #include<stdio.h>
    int a;
    int a=2;
    int main()
    {
    printf("A %d\n",a);
    return 0;
    }

    no error here

    #include<stdio.h>
    int a=3;
    int a=2;
    int main()
    {
    printf("A %d\n",a);
    return 0;
    }

    Error : Redeclaration of a

    why it is not giving in first program why it is giving in second one

    if include a file twice its including the file twice how to avoid such situations

    thanks in adv

  2. #2
    Just Joined! vinall's Avatar
    Join Date
    Jun 2006
    Location
    bangalore,INDIA
    Posts
    42
    ///////////////////////tt.h


    int a=10


    /////////////////////tt.c

    #include<stdio.h>
    #include"tt.h"
    #include"tt.h"

    int main()
    {
    return 0;
    }


    //////////////////////

    $ gcc tt.c
    error redeclarion of a
    previous declaration of a was here


    /////////////////////


    how to avoid such kind of situations bcoz if am having hundreds of files and to take care such that its not included more than once is difficults is there any other technique to say compiler not to include one file more than once.....

    some body help me regarding these two posts


    thanks in advance

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    23
    int a=3;
    int a=2;
    surely this will give an error coz they are both initialized with same name and assigned
    int a;
    int a=2;
    here u are just assigning a value to the first one, and it s globally defined , u can try to make it within ur main and u ll c the defference, redefinition error should occur if am not mistaken.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    In your first program, "int a;" is a prototype: it is saying that in this program there exists some global int called a. It is just like a function prototype.

    In the second, you are declaring two ints called a, which is illegal.

    I hope this makes sense.

    As far as avoiding the problem with including headers, you can do this by using a DEFINE protection.

    Code:
    #ifndef TT_H_INCLUDED
    #define TT_H_INCLUDED
    
    int a = 10;
    
    #endif
    This way, the preprocessor will only actually include the file once.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined! vinall's Avatar
    Join Date
    Jun 2006
    Location
    bangalore,INDIA
    Posts
    42
    Thank U i got it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...