9
YADU
30d

"What are all the meanings of static in C and C++?"

Not a particularly good question but I think I'll always remember it because of how weirdly specific it is.

(And, anyone who wants to have a go at this WITHOUT Googling, feel free to.)

Comments
  • 7
    Thunder and lightning, very very frightening!
  • 6
    No googling. It has three applications, and all of them have the point of scope limitation.

    1) static functions. Limits visibility of a function to file scope, helping in maintainability and compiler optimisation (inlining), but the latter point is not that important these days because of link time optimisation (LTO). Also allows including them in header files with inline attribute so that you don't have to use function-like macros and thus don't run into ++/-- side effect problems with the arguments.

    2) file static variables. Like global variables, but limited to file scope. Makes clear that the variable isn't accessed throughout the rest of the code base.

    3) function static variables. Like the previous point, but with function limited scope. Can be used for making a function remember something across calls, or to save stack space at the expense of the BSS segment. Is dangerous with multi-threading if the function might be used from different threads.
  • 5
    Also, it's a relevant question in low level programming. If you can't answer that, it's as bad as not being clear about what "const" means exactly (it doesn't mean "constant"), or the merits and limitations of "volatile".
  • 2
    @Fast-Nop

    For either C or C++ you missed some
  • 2
    @Fast-Nop maybe the interview was for a front end job
  • 3
    @Fast-Nop I was able to answer it and got the job, but was just taken aback by the question.

    Up to then I mainly got whiteboarding questions (this was for an internship).
  • 1
    @YADU I don't think I missed some for C.
  • 3
    @Fast-Nop Hints for sizes of array parameters (starting from C99 anyways)
  • 2
    @ars1 C++ networking job
  • 8
    @YADU My compliment - TIL something new about C that I havn't used in decades, and that's pretty rare.

    I mean, I have never used VLAs, either, because I think they're a bad idea, but at least I know that they exist.
  • 1
    @Fast-Nop

    It is not file scope, C does not have a file scope, static function and static variables outside of functions are limited to a single translation unit.

    A translation unit can have, and often has, multiple files, every #include adds an additional file to the current translation unit.

    In theory you can use a single file for multiple translation units, with preprocessor tricks and different command line options, but i newer saw this and it would be a horrible way to program.
  • 2
    @happygimp0 That's what I meant, the file that the compiler actually sees (hence static functions in header files). Translation unit would have been precise.

    I did see that approach in the old days, i.e. including all .c files in a master .c file to enable more compiler optimisations, in particular with global inlining - LTO was not yet a thing back then. One example would be the famous chess engine "Crafty".
  • 0
    @happygimp0 hey come on no need to be pedantic here
Add Comment