preprocessor

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is there away to check if a variable has been initialised? Say I have a global variable called gM...

can is say something like:

#ifndef gM
{
int gM;
}
#endif

Thanks for the help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Initialized to what? If you don't specify a value, global variables are initialized to 0.

But from the psuedo-code you gave, I don't think you mean "initialized", but "defined". For that, the answer is no, there is no pre-processor that can determine this.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Initialized to what? If you don't specify a value, global variables are initialized to 0.

But from the psuedo-code you gave, I don't think you mean "initialized", but "defined". For that, the answer is no, there is no pre-processor that can determine this.

Your correct, sorry, I ment defined... So what if I declare a preprocessor called _gM and then say:

#ifndef _gM
int gM;
#define _gM
#endif

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. You seem to be talking about a variable being defined, rather than initialise, right? (The latter would be to give the variable a well defined value).

2. No, you can't do it as above. The pre-processor runs before the compiler proper, and a variable definition is handled by the latter so the former does not know anything about it. The pre-processor actually knows nothing at all about the language C. It more or less a simple device doing textual replacements.

3. The closest I can think of is to introduce a preprocessor symbol (in the extreme case, one for each variable), like so:

#ifndef THE_VARIABLE
int theVariable
#define THE_VARIABLE
#endif

You can use this technique to avoid compiler errors for things being defined twice. In fact many use this to "guard" complete header (.h) files from being included several times, eg in wazabazoo.h:

#ifndef WAZABAZOO
#define WAZABAZOO



#endif

This technique is often called "include guards".

Still, if you have trouble with variables being defined more than once I musr raise the sspicion that there is something structurally wrong with your project. Are you defining variables in header files? Don't. Move them to the .c file where they are to be used.

HTH!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that would work, but the only reason I can see for doing something like that is poor design.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

FYI, although this is probable not a solution to Billy 2's problem, another technique for checking during runtime(with GCC) is to use the weak attribute.
If the variable or function isn't defined somewhere the symbol for it will be zero.

extern int gM __attribute__ ((weak));
uint8_t bar;

uint8_t func(void)
{
    static uint8_t foo;

    if (!gM)
    {

        foo += bar;
    }
    return 1;
}       
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

atomicdog wrote:
If the variable or function isn't defined somewhere the symbol for it will be zero.
if (!gM)
->
if (!&gM)

Stefan Ernst