views:

107

answers:

3

In C if I declare a struct/union/enum:

struct Foo { int i ... }

when I want to use my structure I need to specify the tag:

struct Foo foo;

To loose this requirement, I have to alias my structure using typedef:

typedef struct Foo Foo;

Why not have all types/structs/whatever in the same "namespace" by default? What is the rationale behind the decision of requiring the declaration tag at each variable declaration (unless typdefe'd) ???

Many other languages do not make this distinction, and it seems that it's only bringing an extra level of complexity IMHO.

A: 

Well, other languages also usually support namespaces. C doesn't.

It probably isn't the reason, but it makes sense to have at least this natural namespace.

Let_Me_Be
A: 

Interesting question! Here are my thoughts.

When C was created, little abstraction existed over assembly language. There was FORTRAN, B, and others, but when C came to be it was arguably the highest level language in existence. It's goal was to provide functionality and syntax powerful enough to create and maintain an operating system, and it succeed remarkably.

Think that, at the time, porting a system to a new platform meant rewriting and adapting components to the platform's assembly language. With the release of C, it eventually came down to porting the C compiler, and recompiling existent code.

It was probably an asset back then that the very syntax of the language forced you to differentiate between types that could fit in a register, and types that couldn't.

Language syntax has evolved a lot since then, and most of the things we're used to see in modern languages are missing in C. User-defined namespaces is only one of them, and I don't think the concept of "syntax sugar" even existed back then. Or rather, C was the peak of syntax sugar.

We're surrounded with things like this. I mean, take a look at your keyboard: why do we ave a PAUSE/BREAK key? I don't think I've pressed that key for years.

It's inheritance from a time in which it made sense.

Santiago Lezica
I hit my PAUSE/BREAK key regularly. The most common use, for me, is to interrupt compiling, or breaking in with the debugger.
mrduclaw
+1  A: 

Structures/records were a very early pre-C addition to B, just after Dennis Ritchie added a the basic 'typed' structure. I believe that the original struct syntax did not have a tag at all, for every variable you made an anonymous struct:

struct {
    int  i;
    char a[5];
} s;

Later, the tag was added to enable reuse of structure layout, but it wasn't really regarded as real 'type'. Also, removing the struct/union would make parsing impossible:

/* is Foo a union or a struct? */
Foo { int i; double x; };
Foo s;

or break the 'declaration syntax mimics expression syntax' paradigm that is so fundamental to C.

I suspect that typedef was added much later, possible a few years after the 'birth' of C.

The argument "C was the highest level language at the time." does not seem true. Algol-68 predates it and has records as proper types. The same holds for Pascal.

If you like to know more about the history of C you might find Ritchie's "The Development of the C Language" an interesting read.

schot
I don't really see what this has to do with the question. The OP asks why they need to say `struct Foo foo` instead of `Foo foo` when declaring a variable, not defining the type.
detly
@detly: I wanted to provide some context, but I could have been more to the point: *Because tags didn't start out as real types and to make sure 'declaration syntax mimics expression syntax' remains possible.*
schot
@schot - okay, that makes more sense. I never really think of the 'declaration syntax mimics expression syntax' rule beyond pointer types.
detly