Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the best way to declare and define global variables and functions?

0
Posted

Whats the best way to declare and define global variables and functions?

0

First, though there can be many declarations (and in many translation units) of a single global variable or function, there must be exactly one definition. [footnote] For global variables, the definition is the declaration that actually allocates space, and provides an initialization value, if any. For functions, the definition is the “declaration” that provides the function body. For example, these are declarations: extern int i; extern int f(); and these are definitions: int i = 0; int f() { return 1; } (Actually, the keyword extern is optional in function declarations; see question 1.11.) When you need to share variables or functions across several source files, you will of course want to ensure that all definitions and declarations are consistent. The best arrangement is to place each definition in some relevant .c file. Then, put an external declaration in a header (“.h”) file, and #include it wherever the declaration is needed. The .c file containing the definition should als

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123