8

What are features from the actual C Standard, which originally weren´t part of C, but were invented in/for C++ and because of its benefits, were later adopted to C?

One prominent example is the single-line comment //, which originally came from C++ and later was adopted by C.

Do you know more features of the actual C Standard, which explicitly or implicitly came from the development of C++?

Info: Of course, I know that C++ is derived from C but i was just thinking about which features was adopted from the development of its derivative, C++.

  • 4
    C's `_Bool` type was inspired by C++'s `bool` I believe. – Jesper Juhl Dec 18 '19 at 16:28
  • 2
    Being able to declare variables in other locations than at the very top of a function was also a C++ first, later adopted by C I think. – Jesper Juhl Dec 18 '19 at 16:32
  • @JesperJuhl Sure? This has been a feature of C much longer than most people think, and predates the first C++ standard. – Konrad Rudolph Dec 18 '19 at 16:34
  • 2
    I think function prototypes came from C++, IIRC. – Ian Abbott Dec 18 '19 at 16:34
  • 2
    Not allowing "implicit int" as a function return type (except for `main()`) is (I think) also something that C++ pioneered and C subsequently adopted. – Jesper Juhl Dec 18 '19 at 16:35
  • @Konrad Am I sure? No. I explicitly said "I think". Which implies that I'm *not* certain. I'm going by my *memory* of reading the standards over the years, but I have not double checked my memory. – Jesper Juhl Dec 18 '19 at 16:36
  • 5
    This doesn't seem like a question with one clear answer, and there's no obvious problem here. – Caleb Dec 18 '19 at 16:37
  • You can see the C99 directives – TZof Dec 18 '19 at 16:39
  • 2
    @JesperJuhl I think declaring variables inside an inner block of a function might predate C++, but mixing declarations and statements within a block probably came from C++ since it has only been a standard feature since C99. – Ian Abbott Dec 18 '19 at 16:45
  • https://chat.stackoverflow.com/ – Lightness Races in Orbit Dec 18 '19 at 17:05
  • 1
    [Here](http://www.stroustrup.com/sibling_rivalry.pdf) B. Stroustrup relates the early history of C and C++. – AProgrammer Dec 18 '19 at 17:28
  • 1
    Advancements to C were purposely put on hold during C++ nascent years. Features that appeared first in C++ then C are then not necessarily due to "C++ has it so let's put it in C" as much as the C update was postponed and the feature came up in both. I'm thinking of `//`, inline, and maybe others. – chux - Reinstate Monica Dec 18 '19 at 17:37

2 Answers2

4

I cannot confirm that they were definitely influenced by C++ directly1, but here is a list of features in standard C++ that were added in C99:

  • single line comments (as mentioned)
  • inline functions
  • variables allowed after beginning of block
  • boolean datatype
  • complex math library

C11:

  • Anonymous unions (C11 allows anonymous structs too). Anonymous unions were already in standard C++. (Anonymous structs are still not allowed in standard C++).

1 For example BCPL, predecessor of B which in turn is the predecessor of C already had same syntax for single line comments. Some of these may have been supported as language extensions in some C implementation prior to their incorporation to standard C++. In these cases both standard C and standard C++ may have been influenced by the same source, rather than influencing one another.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

Attributes were added in C++11 and will be added in the next C standard revision C2x. The proposal (and here) for this feature specifically references C++.

Attributes can be useful for providing information that, for example, helps the compiler to issue better diagnostics or optimize the generated code. Source

Example:

int [[attr1]] i [[attr2, attr3]];

[[attr4(arg1, arg2)]] if (cond)

{
    [[vendor::attr5]] return i;
}

In this example, "attribute attr1 applies to the type of variable i, attr2 and attr3 apply to the variable itself, attr4 applies to the if statement and vendor::attr5 applies to the return statement." Source