Is there a way to know if a variable has been declared in C?

C is strictly static, you can't "lookup" if a variable has already been declared. If you are creating a code generator, why not read lines of code and see what's been declared?

Actually i'm generating several versions of the file and then i'm generating a whole bunch of patches. These patches insert macros, and some macros expand to declarations, so I cannot know everything at generation time, because the patch combination can be quite tricky – LB . Apr 8 '10 at 13:51.

No, there isn't. Doing so is much of what compilers do. A common way to create unique a variable name is to use a very unlikely variable name, if possible combined with the line number.

Something like // beware, brain-compile code ahead! A_rather_unlikely_variable_name_by_sbi_ ## __LINE.

Not really, no. Not unless you count attempting to use it and seeing if your code compiles. You could try to hack something with the preprocessor for specific variables, sort of like the standard #ifdef at the top of every include file.

That wouldn't be scope-aware though, as the preprocessor runs way before the compiler does. C isn't a very dynamic language that way.

I agree, but I cannot find a CPP trick... – LB . Apr 8 '10 at 13:27.

When you generate the variable youself you can emmit a preprocessor token along with the variable and later check if that token exist.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions