Why does C need "struct" keyword and not C++?

C++CCompiler Construction

C++ Problem Overview


I've always been a little confused about what's going on here:

#include <stdio.h>

int main() {  
    timeval tv;
    tv.tv_sec = 1;

    for (;;) {
        select(0, 0, 0, 0, &tv);
        printf("%s\n", "Hello World!");
    }
}

Sorry if that doesn't compile, just wrote it as a quick example.

Code like this won't compile under gcc unless I add the keyword struct prior to the use of the struct timeval. g++ on the other hand handles it fine as is.

Is this a difference between how C and C++ handle structures or is it just a difference in the compilers? (I'm very C++ oriented, and the use of struct in C on lines like this has always somewhat baffled me).

C++ Solutions


Solution 1 - C++

Syntactically both treat struct almost the same. Only C++ has added an extra rule that allows to omit the struct (and class) keyword if there is no ambiguity.

If there is ambiguity, also C++ requires the struct keyword in some places. A notorious example is stat on POSIX systems where there is a struct stat and a function stat.

Solution 2 - C++

Consider the original idea of C++ (or, when it was just an idea, "C with classes"), that of an OO-oriented language that was compatible with C to the point where most valid C programs were also valid C++ programs.

C++ built its class model by starting with C's struct and adding some further functionality:

  1. Inheritance (though you can come close in C with having the first member of a struct the struct you want to "inherit" from).
  2. Information hiding (through public, private etc)
  3. Member methods (which were originally turned by macros into C code outside the struct with an added this parameter - many implementations are still similar in practice).

At this point there were two problems. The first is that the default access had to be public, since C has no information hiding and therefore from a C++ perspective has everything public. For good OO one should default to private. This was solved by adding class which is pretty much identical to struct except for the default is private rather than public.

The other is that this OO perspective should have timeval or any other class/struct on the same "footing" as int or char, rather than constantly annotated in the code as special. This was solved by relaxing the rule that one must place struct (or class) before the name of the type in declaring a variable of that type. Hence struct timeval tv can become timeval tv.

This then influenced later C-syntax OO languages, like Java and C# to the point where, for example, only the shorter form (timeval tv) would be valid syntax in C#.

Solution 3 - C++

I would say it's a design decision of both languages.

Structs in C are just structured records and have different usage then built-in type.

C++ has ctors and operator overloads and so they act as types.

struct foo x;         // create a structure of pattern foo
typedef foo foo_type; // "define" a type
foo_type x;           // create an instance of type foo_type

C++:

foo x; // create an instance of type foo

As a side-note, struct foo is still allowed in C++. struct foo is easier to parse then typedef'dfoo as the name-lookup is simpler.

Solution 4 - C++

It's just how C looks like. Therefore, the following patterns is pretty common in C:

typedef struct YourStructure
{
   int x;
   // more fields
} YourStructure;

Then you can reference it the same way like in C++.

Solution 5 - C++

It's simply a difference in the languages. C++ is more permissive in its struct syntax.

Solution 6 - C++

The way C does it came first, of course. Structs and classes in C++ are nearly identical, and it would have been very inconvenient to require class with every class variable so it was simplified for both.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn HumphreysView Question on Stackoverflow
Solution 1 - C++Jens GustedtView Answer on Stackoverflow
Solution 2 - C++Jon HannaView Answer on Stackoverflow
Solution 3 - C++PubbyView Answer on Stackoverflow
Solution 4 - C++KrizzView Answer on Stackoverflow
Solution 5 - C++David HeffernanView Answer on Stackoverflow
Solution 6 - C++Mark RansomView Answer on Stackoverflow