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
Using a static variable or function in various files. Posted by shantanuk on 27 Nov 2006 at 7:35 PM
Hi,
Suppose we have declared a static variable or function in one of the source files and now we wish to use same variable or function in other source file, how do we use this?
_shantanuk.
Report
Re: Using a static variable or function in various files. Posted by Lundin on 28 Nov 2006 at 12:20 AM
: Hi,
: Suppose we have declared a static variable or function in one of the source files and now we wish to use same variable or function in other source file, how do we use this?
: _shantanuk.
:


You can't. The whole purpose of static variables is to block other files from using them.

You must do like this, which is also good programming practice:

/* myfile.h */

int getMyVar (void);


/* myfile.c */
#include "myfile.h"

static int myVar;

int getMyVar (void)
{
  return myVar;
}


/* main.c */

#include "myfile.h"

int main()
{
  printf("%d\n", getMyVar());
  return 0;
}




 

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.