# My interview question:

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If you have a global variable and put the name "static" in front of it, what does this change? I said nothing. I guess I missed the point of this question?

##### Share on other sites
You did indeed. clicky

##### Share on other sites
So it makes it local to the file. So this was such a big question to ask because if you don't know that, you obviously don't know how to program. God damn.

##### Share on other sites
Quote:
 Original post by dpadam450So it makes it local to the file.
More exactly, it makes it local to the translation unit. (A static global declared in a header file is most certainly not just local to the header file.)
Quote:
 So this was such a big question to ask because if you don't know that, you obviously don't know how to program. God damn.
If you don't know that, you obviously haven't spent much time getting to know your language of choice, or interacting with others' code in that language.

##### Share on other sites
It may or may not be a bogus question depending on various factors. If you stated on your resume that you were highly proficient with C or C++ then yeah, getting that answer wrong would look a little bad because it would make it seem like you weren't that proficient. If it was just one small component of the interview out of many similar language construct questions it probably wasn't that important. Other concerns are how many people are applying for the position. If there are a LOT of people trying to get this job then, all other things being equal, why not drop the candidate that can't answer the question correctly? This isn't exactly highly obscure trivia. A lot of programs, particuarly some C programs I've worked with, use global static variables. I don't like 'em, but they're there.

It could just be a crap interview though.

##### Share on other sites
"or interacting with others' code in that language."

Well I've only worked on games at school and I've never had anyone do this or have any teacher mention it. I've never professionally done anything so that would be the reasoning. It still kinda sounds sloppy, being that it is still global. I dont know.

Either way, I looked it up after the guy above sent me a link. 5 seconds I know what it means. Really this is a big deal?

##### Share on other sites
Quote:
 Original post by dpadam450Either way, I looked it up after the guy above sent me a link. 5 seconds I know what it means. Really this is a big deal?

If you plan on using C or C++ for anything non-trivial, you better know your statics. They are both languages where something can compile, but then fail miserably during linking.

Personally, I avoid static context like plague, but that is not a luxury one can afford in any or at least most real world projects.

The basics of C++ are here. It would be prudent to understand each and every point listed there.

##### Share on other sites
Quote:
 Original post by dpadam450Either way, I looked it up after the guy above sent me a link. 5 seconds I know what it means. Really this is a big deal?
They don't care about static in particular. They asked you the question to gauge whether you really knew a lot about the language, or whether you only had a limited, working knowledge. Apparently they judged correctly. But perhaps if you whine about it, it'll make things better!

##### Share on other sites
Quote:
 Original post by dpadam450Either way, I looked it up after the guy above sent me a link. 5 seconds I know what it means. Really this is a big deal?

As an interviewer I don't care about you looking it up. I care that you GUESSED the answer. You should have said: "I don't know" if you had been talking to me.

I don't want people guessing, I want people that know what they know and what they don't know. Never guess, be honest about knowledge. And when you are afraid of answering "I don't know" state it differently: "I am not entirely sure, I would have to look it up. I never used it as I never use global or static variables." (if that is true)

##### Share on other sites
Well my answer actually was that it would be the same. It is all just compiler time and that a static variable is really just a global variable at run-time, it stays in memory at the exact same location and not on the stack, so declaring a global variable to be "static" (not on the stack) -> that is what a global variable is already.

##### Share on other sites
I didn't want to whine about it, it just HIGHLY conflicted with my knowledge of static. Yes it does make sense that static local to functions could be applied to files.

Thanks. And I'll go through the C++ FAQ when I get some more time.

##### Share on other sites
I still don't think you understand what static means. Say you have two files:

// 1.cppint foo;void myfunc(){  foo = 1;}// 2.cppextern int foo;void myfunc();int main(){  myfunc();  cout << foo;  return 0;}

In this case, the program will print out "1" because the foo in 2.cpp is the same as the foo in 1.cpp. If, instead, you have:

// 1.cppstatic int foo;void myfunc(){  foo = 1;}// 2.cppextern int foo;void myfunc();int main(){  myfunc();  cout << foo;  return 0;}

If you tried to compile this, you'll get a link error because 2.cpp cannot "see" the foo that has been declared in 1.cpp, since it's been marked as static. In fact, you could even do this:

// 1.cppstatic int foo;void myfunc(){  foo = 1;}// 2.cppstaticint foo;void myfunc();int main(){  myfunc();  cout << foo;  return 0;}

In this case, the problem will compile and link, but it will print out "0" instead of "1". The reason is that the foo that 1.cpp "sees" is a different foo to the one that 1.cpp "sees".

Edit: Ah, I see where the confusion is coming from. When you declare a variable in a method body as static, that's something totally different to what happens when you declare a global variable as static. The static keyword has a number of different meanings, depending on the context it's used in (for example, a static member variable of a class is something totally different again - although somewhat related to declaring variables as static in method bodies, I guess).

##### Share on other sites
Yea that was what I understood it as. The link sent up top though, said that what you did would on example 2 would be undefined.

##### Share on other sites
Quote:
 Original post by dpadam450Yea that was what I understood it as. The link sent up top though, said that what you did would on example 2 would be undefined.

I only see one instance of "undefined" on that page:
Quote:
 If two or more declarations of the same name have external linkage but incompatible types, then you've done something very silly and have undefined behaviour.

Both foos in Codeka's 2nd example have internal linkage (as specified by the static keyword) and not external linkage, so that doesn't apply here.