global Static variables goofed - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

global Static variables goofed

hvjoshihvjoshi Posts: 4Member
Hi,
I have a problem....I am defining a static varialbe in the main c file as global. Now the same variable is defined as extern static in the another file.I use that variable to process some data.Now I check the variable value in the main file after returning from the exiternal file.

As far as i know this should give error while complie....BUT it gets compiled and even runs in a normal fashion as if the variable is a global variable.

here is my code

file 1:
----------
#include
#include
#include "file2.c"

static int i;

void main()
{
double_me();
printf("%d/n",i);
getchar();
}


file 2:
-------
extern int i;

void double_me()
{
i=i+i;
}


Comments

  • LundinLundin Posts: 3,711Member
    There is no such thing as "static extern". The whole purpose with static is that the variable can't be accessed outside its own namespace. Also, never ever include .c files or you will get linker errors.

    Good programming practice would be to write like this:

    [code]main file:
    ----------
    #include
    #include "my_file.h"

    int main()
    {
    int i;
    i = double_me();
    printf("%d
    ", i);

    getchar();
    return 0;
    }


    myfile.h:
    ---------
    #ifndef _MY_FILE_H
    #define _MY_FILE_H

    int double_me(void);

    #endif /* _MY_FILE_H */


    myfile.c:
    -------

    static int i=0;

    int double_me(void)
    {
    i++;
    return i;
    }

    [/code]


    This is how all C program should look, more or less. The .h and its corresponding .c file forms an independant code module which may be used in other projects etc.
  • hvjoshihvjoshi Posts: 4Member
    : There is no such thing as "static extern". The whole purpose with
    : static is that the variable can't be accessed outside its own
    : namespace. Also, never ever include .c files or you will get linker
    : errors.
    :
    : Good programming practice would be to write like this:
    :
    : [code]: main file:
    : ----------
    : #include
    : #include "my_file.h"
    :
    : int main()
    : {
    : int i;
    : i = double_me();
    : printf("%d
    ", i);
    :
    : getchar();
    : return 0;
    : }
    :
    :
    : myfile.h:
    : ---------
    : #ifndef _MY_FILE_H
    : #define _MY_FILE_H
    :
    : int double_me(void);
    :
    : #endif /* _MY_FILE_H */
    :
    :
    : myfile.c:
    : -------
    :
    : static int i=0;
    :
    : int double_me(void)
    : {
    : i++;
    : return i;
    : }
    :
    : [/code]:
    :
    :
    : This is how all C program should look, more or less. The .h and its
    : corresponding .c file forms an independent code module which may be
    : used in other projects etc.

    I 100% agree with you...Actually I am in a impression that if I declare a variable as static in global section of my main.c file then compiler should not allow it to be defined as extern in any other file.

    am i right or is there any mistake in this concept?Please clarify.
    my code would be like this...

    [code]: main file:
    ----------
    #include
    #include "my_file.h"

    static int i;

    int main()
    {
    /* int i; */
    i=5;
    i = double_me();
    printf("%d
    ", i);

    getchar();
    return 0;
    }


    myfile.h:
    ---------
    #ifndef _MY_FILE_H
    #define _MY_FILE_H

    int double_me(void);

    #endif /* _MY_FILE_H */


    myfile.c:
    -------

    //static int i =0;
    extern int i;

    int double_me(void)
    {
    i++;
    return i;
    }

    [/code]:

  • vijayrathodvijayrathod Posts: 3Member
    : : There is no such thing as "static extern". The whole purpose with
    : : static is that the variable can't be accessed outside its own
    : : namespace. Also, never ever include .c files or you will get linker
    : : errors.
    : :
    : : Good programming practice would be to write like this:
    : :
    : : [code]: : main file:
    : : ----------
    : : #include
    : : #include "my_file.h"
    : :
    : : int main()
    : : {
    : : int i;
    : : i = double_me();
    : : printf("%d
    ", i);
    : :
    : : getchar();
    : : return 0;
    : : }
    : :
    : :
    : : myfile.h:
    : : ---------
    : : #ifndef _MY_FILE_H
    : : #define _MY_FILE_H
    : :
    : : int double_me(void);
    : :
    : : #endif /* _MY_FILE_H */
    : :
    : :
    : : myfile.c:
    : : -------
    : :
    : : static int i=0;
    : :
    : : int double_me(void)
    : : {
    : : i++;
    : : return i;
    : : }
    : :
    : : [/code]: :
    : :
    : :
    : : This is how all C program should look, more or less. The .h and its
    : : corresponding .c file forms an independent code module which may be
    : : used in other projects etc.
    :
    : I 100% agree with you...Actually I am in a impression that if I
    : declare a variable as static in global section of my main.c file
    : then compiler should not allow it to be defined as extern in any
    : other file.
    :
    : am i right or is there any mistake in this concept?Please clarify.
    : my code would be like this...
    :
    : [code]: : main file:
    : ----------
    : #include
    : #include "my_file.h"
    :
    : static int i;
    :
    : int main()
    : {
    : /* int i; */
    : i=5;
    : i = double_me();
    : printf("%d
    ", i);
    :
    : getchar();
    : return 0;
    : }
    :
    :
    : myfile.h:
    : ---------
    : #ifndef _MY_FILE_H
    : #define _MY_FILE_H
    :
    : int double_me(void);
    :
    : #endif /* _MY_FILE_H */
    :
    :
    : myfile.c:
    : -------
    :
    : //static int i =0;
    : extern int i;
    :
    : int double_me(void)
    : {
    : i++;
    : return i;
    : }
    :
    : [/code]: :
    :
    :


    it's not possible to access ur static variable in other file

  • LundinLundin Posts: 3,711Member
    : I 100% agree with you...Actually I am in a impression that if I
    : declare a variable as static in global section of my main.c file
    : then compiler should not allow it to be defined as extern in any
    : other file.

    That is somewhat correct. The variable can't be accessed in another file, but the tricky thing is - the compiler won't complain about an extern variable with the same name as the static. So the code will compile and then the linker will go and look for a variable 'i' in other files, but it won't find any that it is allowed to use since the 'i' in the other file is static. So your code would likely result in some sort of linker error "undefined external" etc.
  • hvjoshihvjoshi Posts: 4Member
    : : I 100% agree with you...Actually I am in a impression that if I
    : : declare a variable as static in global section of my main.c file
    : : then compiler should not allow it to be defined as extern in any
    : : other file.
    :
    : That is somewhat correct. The variable can't be accessed in another
    : file, but the tricky thing is - the compiler won't complain about an
    : extern variable with the same name as the static. So the code will
    : compile and then the linker will go and look for a variable 'i' in
    : other files, but it won't find any that it is allowed to use since
    : the 'i' in the other file is static. So your code would likely
    : result in some sort of linker error "undefined external" etc.

    I have tried compiling these files with turboc 3.0, VC++ 6.0, some modified versions of these files with embedded compilers like keil as well....but i am always getting the properly compiled binaries...SURPRIZINGLY...!!!

  • blueskyyblueskyy Posts: 1Member
    The answer is simple. If you remove this line:

    #include "file2.c"

    it should probably work.

    Why? I will give you the explanation using the gcc/g++ compiler first, then relate it to Visual C++, it's easier that way. You compile your files by running:

    gcc "file 1" "file 2" -o file.exe

    In Visual C++, if you create a console project, it will compile the two files automatically for you. This is what it meant by "an external" file. The linker will have to link the objects in "file 1" with the objects in "file 2".

    Using the include directive is more like putting all the code together into a single file. Here the linker has nothing to link (well... not entirely true, I confess... but this is a simplified explanation), it's only one file.

    PS: I don't get why people pretend to answer when they don't feel like answering, or cite textbook definition without understanding what it meant, or just repeat oneself over and over... Oh, you see this everywhere...
  • bilderbikkelbilderbikkel Posts: 754Member
    : PS: I don't get why people pretend to answer when they don't feel
    : like answering, or cite textbook definition without understanding
    : what it meant, or just repeat oneself over and over... Oh, you see
    : this everywhere...
    Dear Blueskyy, I do not understand your tone and I do not see who you are talking about. What is your problem? And do you suggest a better solution?

    Please take care not to offend our helpful and experienced fellow programmers.

    Bilderbikkel
Sign In or Register to comment.