Consider the following program:

Many people when they want to use a struct, they create a new variable as:

struct structureName variableName

While it works when you just define it as:

 structureName variableName

My teacher always uses the first method. My question is how do they differ? Do I ever need to specify struct before defining my variableName. Here is an example to explain my question:

struct example {
    int n;
    char c;

int main() {
    example o;
    o.c = 'c';
    o.n = 5;
    printf("%c", o.c);
    printf("%d\n", o.n); //this works

    struct example ex; // this versus "example o" without using struct keyword
    ex.c = 'e';
    ex.n = 7;
    printf("%c", ex.c);
    printf("%d", ex.n); //this works

    return 0;
  • 5
    Because you compile it as C++. Try to compile it as plain C and it will never compile. Dec 21, 2014 at 14:28
  • "My teacher always uses the first method" - maybe your teacher is a C guy, and not a C++ guy?
    – Doc Brown
    Dec 21, 2014 at 14:32
  • Is this a class in C? or C++? Given the rest of the code (printf), I would assume C. And while some code may be able to be compiled by a C++ compiler, that does not mean that it is valid C code even though it looks like it would be.
    – user40980
    Dec 22, 2014 at 4:33

1 Answer 1


This is one of the differences between C and C++.

In C, structure names are completely separate from other names and you must use the struct keyword to tell the compiler to look for the name of a structure.
Another way to put it is that the struct keyword is actually part of the name of the structure.

When designing C++, this was changed and the use of the struct (or class) keyword was made optional when referring to a structure or class.

  • As a side note, I always prefer typedefing my types before including headers.
    – Clearer
    Dec 21, 2014 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.