Scope of variable ?

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-04-15
I am trying to port a C program with 4 functions (including) main() into an MFC app.

The 4 functions communicate between themselves using GLOBAL VARIABLES.

In the MFC app it looks like....

void CMFCappDoc::OnRleEmbed()

However many of the variable names are identical to ones already used in the MFC. How do I declare the variables such that they are visible (ie.identical to global) inside the OnRleEmbed() function and accessible to func1(), func2() and func3(), but not outside OnRleEmbed() so that they do not interfere with the rest of the MFC app.

Question by:vikky999
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8222925
there really isn't a way in C to my knowledge outside of putting all your globals inside a global struct and accesing them from inside the struct


typedef struct globals{
    int global1;
    int global2;

globals global;

however, since you are using MFC you must be using C++ and you can use namespaces to prevent name collisions

#include <windows.h>
#include <iostream>

namespace mine{
int cout; //notice this global has the same name as the cout in iostream

int main()
    mine::cout = 5;
    std::cout<<mine::cout; //woo hoo used at the same time
    return 0;

Author Comment

ID: 8223036
hmm..okay i think i get it.

But i have one more doubt in that case, I have created two files which i added to this project

Rle.h contains all the function declarations
Rle.cpp contains all the function definitions

In the MFCAppdoc.cpp file I include "Rle.h"

If ive defined some variables in Rle.h eg
int apple;
im guessing the variable apple is global in MFCAppdoc.cpp, am i right ?

but what if i define
int apple ;
in Rle.cpp ?? what is the scope of apple then ?

coz when i tried it in Rle.h i got error LNK 2005 , multiply defined symbols or something like that and it disspeared when i declared them in Rle.cpp. However now im not sure that the variable are in scope in MFCAppdoc.h.

I believe when i put a statement
#include "Rle.h"
the statement is simply replaced by the Rle.h file. so that would make sense and apple would obviously be global.
But I dont understand what happens if I declare it in Rle.cpp

Expert Comment

ID: 8223106
yeah pretty much,  

#include is replaced by the contents of the file by the preprocessor.

if you put it in the cpp file (and its not externed) then MFCAppdoc.h will not know about "apple".

"externed" means that in MFCAppdoc.h you put a statement like so

extern int apple; //this tells the linker that you have a global apple defined elsewhere in your application

so if you need it in both files I would follow this example

so for an example

Rle.h looks like this then

//this #xxx stuff makes sure your .h is only included once in a file you should always do this regardless for your .h files of the example
#ifndef RLE_H_
#define RLE_H_

extern int apple;

in mfcappdoc.cpp do this

#include "Rle.h"

and in Rle.cpp do this

#include "Rle.h"

int apple;  //same rule applies as above

you can even include Rle.h in MFCAppDoc.h if necessary safely.

Anyway you should not put a variable definition in a .h file.  You should declare them (with extern) and then define them in a .cpp file.  That way you can avoid the multiply defined symbols stuff that you see.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8224548
It isn't clear to me whether you have access to the source code for func1(), etc. and if OnRleEmbed is the only top level function that needs them.

If that is the case, there's a simpler way.  Declare them as statics within OnRleEmbed, and pass them as parameters to func1(), etc.

The basic rule is to keep scope as small as possible.


Accepted Solution

honey_hamster earned 500 total points
ID: 8224976
If the variables have to be visible to func1(), func2(), and func3(), then you can put func1(), func2(), func3() all in the same .c source file, and declare the variables as static at the top of the .c source file.  That way, all code within the .c file can see the variables, but the variables remain hidden and separate from all software outside your .c file.

Author Comment

ID: 8228334
Thanks everybody. I kind of took some suggestions from all of you , i dont know how to distribute points.

THANKS !!!!!!


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question