A few questions:

1) The first question is the following: is it correct to say that the library is being connected or is the header file correctly connected in C / C ++ languages? A simple example:

#include <stdio.h> int main(void) { ... } #include <iostream> int main() { ... } 

I think that when you connect a library with pragma, you still need to drag its header file, but this is the header file, not the library.

2) The second question is the following, when writing code in C ++, is it worth refusing to use macro substitutions in favor of typed constants? For example, instead of

 #define N 5 

to write

 const int N = 5 

And get rid of macro functions in favor of inline functions?

3) The third question is why some comrades believe that in the SI language it is impossible to use variables - iterators directly in the loop header? Perhaps it was really forbidden in any old standard, but I personally did not encounter such a problem.

  • 9
    I recommend dividing this issue into three separate ones, since they are absolutely unrelated. - D-side
  • @ D-side they are all set by the same participant, for example :) - αλεχολυτ
  • 2
    If each question were arranged separately, I would add all of them. And so - I can not. - αλεχολυτ

3 answers 3

Is it correct to say that the library is being connected or the header file in the C \ C ++ languages ​​is being correctly connected?

In the classic case, the header file and the library itself are two separate entities.

  • In the classic case, the inclusion of the header file is not a library connection. The header file is only the "announcement" of the library contents, and not the library itself. The library itself is connected separately, using some platform-specific tools.

    In your example, the header files of the standard library appear. The standard library itself is usually connected by the compiler automatically, so beginners often have the illusion that the libraries are connected simply by including the header file.

  • Due to the popularity of patterned programming in C ++, libraries that consist only of header files (pure header library, header-only library) have naturally appeared. In this case, the inclusion of the header file is actually the inclusion of the entire library directly into your code. More just nothing to connect. This is another reason why the inclusion of a header file is sometimes called "connecting the library" (in this case, rightly called).

    For example, that part of the standard C ++ library, which is historically called STL, is usually implemented as a pure header library.

  • On the MSVC platform, there is the practice of creating library header files that already contain the #pragma directive within themselves, which also provides the connection of the library itself to the project. Thus, by including the header file, you automatically invoke the connection of the corresponding library. In this case, it is also possible to say that by including such a header file, you thereby connected the library itself ... but still it is worth understanding the essence of the process.

    The ability to connect libraries through a directive in the source ( #pragma ) is a very competent and useful feature of MSVC, but it is peculiar only to MSVC.

when connecting a library with pragma, you still need to drag its header file

Of course. The header file is always needed, regardless of whether you need to connect something with #pragma . But once again: connection of libraries through #pragma is a property only of MSVC. On other platforms, libraries are not connected via #pragma , and analogs of such functionality are not yet visible.

when writing code in C ++, is it worth refusing to use macro substitutions in favor of typed constants

Yes, except for those situations when there is a mixed C / C ++ project. In header files that can potentially be included in C code, you should follow #define .

Also, when it comes to compile time constants, it is constexpr to use constexpr instead of const .

and get rid of macro functions in favor of inline functions?

Where it is possible / justified - yes.

Judging by your third question (as I understood it), you use more or less modern C compilers that support the C99 standard or higher. Then you already have inline-functions in C, and where it is possible, you should use them even in C code.

However, it is worth noting that type-agnostic macros in C are more likely substitutes for template inline functions in C ++. Therefore, the transition from macros to ordinary inline-functions is not always possible / justified.

The third question is why some comrades believe that in the SI language it is impossible to use variables - iterators directly in the loop header?

If we are talking about the declaration of variables in the header of the loop, then this is a property of standard C99 and later. In pre-C99 language this was not allowed.

  • Sorry, but can I contact you by mail?) - QuaternioNoir
  1. The library is connected, the header file is included ... :)
  2. Yes.
  3. It was impossible, in the modern standard - completely.
  4. Brevity - c.t. :)

    A small remark about point 3, but a little bit in a different perspective: why it is not just possible in C ++, but it is recommended to declare variables just before their place of use. Including in cycles.

    For example, because a similar entry:

     Foo bar; 

    in C ++, the Foo constructor is behind it. And he, in turn, may not be so easy, have side effects, etc. But why do we need all this if, for example, in a branch of code, on which execution can go, this variable will not be used at all?

    • 2
      Well, at the same time, we should not forget that declaring a variable with heavy construction / destruction in the body of a cycle can lead to its construction / destruction at each iteration. Sometimes it is worthwhile to sacrifice the locality of the announcement (to make an announcement for a cycle) in order to avoid such unnecessary expenses. - AnT