Akay
Akay

Reputation: 1132

Best place to put constants

  1. In a multi-source file C application, which is the best place to put constant values, source file itself or in its header file? Provided that constant will be used in that source file only.

  2. Is it a good way to keep static const in header files?

Upvotes: 5

Views: 1485

Answers (1)

user694733
user694733

Reputation: 16039

  1. Don't expose information you don't need to. If constant is an implementation detail of single compilation unit, there is no need to pollute global namespace. You can always add it to header later if needed.

  2. Depends on the nature of constant. static creates new constant for each compilation unit header is included in. If constant is huge, it's better to define it once in source file, and declare it with extern in the header.

Upvotes: 6

Related Questions