C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
global Static variables goofed Posted by hvjoshi on 10 Jul 2007 at 10:15 AM
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 <stdio.h>
#include <conio.h>
#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;
}


Report
Re: global Static variables goofed Posted by Lundin on 10 Jul 2007 at 11:08 AM
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:

main file:
----------
#include <stdio.h>
#include "my_file.h"

int main()
{
  int i;
  i = double_me();
  printf("%d\n", 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;
}




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.
Report
Re: global Static variables goofed Posted by hvjoshi on 12 Jul 2007 at 9:55 AM
: 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:
:
:
: main file:
: ----------
: #include <stdio.h>
: #include "my_file.h"
: 
: int main()
: {
:   int i;
:   i = double_me();
:   printf("%d\n", 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;
: }
: 
: 
:
:
:
: 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...

: main file:
 ----------
 #include <stdio.h>
 #include "my_file.h"

 static int i; 

 int main()
{
/*   int i; */
   i=5; 
   i = double_me();
   printf("%d\n", 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;
 }
 
 
:

Report
Re: global Static variables goofed Posted by vijayrathod on 12 Jul 2007 at 10:22 AM
: : 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:
: :
: :
: : main file:
: : ----------
: : #include <stdio.h>
: : #include "my_file.h"
: : 
: : int main()
: : {
: :   int i;
: :   i = double_me();
: :   printf("%d\n", 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;
: : }
: : 
: : 
: :
: :
: :
: : 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...
:
:
: : main file:
:  ----------
:  #include <stdio.h>
:  #include "my_file.h"
: 
:  static int i; 
: 
:  int main()
: {
: /*   int i; */
:    i=5; 
:    i = double_me();
:    printf("%d\n", 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;
:  }
:  
:  
: :
:
:


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

Report
Re: global Static variables goofed Posted by Lundin on 12 Jul 2007 at 11:14 PM
: 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.
Report
Re: global Static variables goofed Posted by hvjoshi on 13 Jul 2007 at 4:16 AM
: : 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...!!!

Report
Goofed? No, not really... Posted by blueskyy on 9 Aug 2010 at 8:43 AM
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...
Report
Re: Goofed? No, not really... Posted by bilderbikkel on 12 Aug 2010 at 6:37 AM
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.