Saturday, 28 October 2023

Chapter 27 // Exercise 10, 11 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2022.

Chapter 27 // Exercise 10

Make a list of C language features adopted from C++ or C with Classes (section 27.1).

Github: N/A

  1. Inline Functions: Initially a C++ thing, it lets you suggest to the compiler that a function should be inlined for performance.
  2. Single-line Comments: I believe // was first used in B, then brought back in C++ because /**/ is tedious.
  3. Variable Declaration Anywhere: Instead of only at the beginning of blocks, you can now declare variables anywhere in C, much like in C++.
  4. Mixed Declarations and Code: Kinda related to the previous one. You can intermix variable declarations with executable code.
  5. Boolean Type (_Bool or bool with stdbool.h): I was shocked the first time I tried to use a bool in C and found out it had to be added to the language in C89.
  6. Compound Literals: Lets you create temporary structures or arrays in the middle of an expression, similar to how you'd do in C++.
  7. Designated Initializers: A more expressive way to initialize struct or array members.
  8. Static Assertions (_Static_assert): Helps catch errors at compile-time based on constant expressions.

Chapter 27 // Exercise 11

Make a list of C language features not adopted by C++.

Github: N/A

Had to look these ones up. It was actually very interesting.
  1. Complex Number Support (_Complex and _Imaginary): C has built-in support for complex numbers, while C++ relies on the std::complex template class.
  2. Different Treatment of inline Functions: In C, an inline function's definition can be present in multiple translation units. In C++, inline functions have external linkage by default, but in C they have internal linkage.
  3. Allowing Implicit Function Declarations: Older versions of C allowed functions to be called without a previous declaration (not recommended, though!). C++ never allowed this.
  4. Named Address Spaces: Some C compilers for specific platforms (like embedded systems) support this feature, but it's not a part of C++.
  5. Non-constant Aggregate Initializers: In C, global and static aggregates can be initialized with non-constant values. C++ doesn't allow this.

No comments:

Post a Comment