Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Typedef struct vs struct definition

I'm a beginner in C programming, however, I was thinking about what's the distinction between utilizing typedef when defining a design as a structure to not utilizing typedef. It appears to me like there's actually no distinction, they achieve a similar objective.
struct myStruct{
int one;
int two;
};

vs
typedef struct{
int one;
int two;
}myStruct;
by

2 Answers

RoliMishra
In C (not C++), you have to declare struct variables like:

struct myStruct myVariable;

In order to be able to use myStruct myVariable; instead, you can typedef the struct:

typedef struct myStruct someStruct;
someStruct myVariable;


You can combine struct definition and typedefs it in a single statement which declares an anonymous struct and typedefs it.

typedef struct { ... } myStruct;
sandhya6gczb
In C, the type specifier keywords of structures, unions, and enumerations are mandatory, ie you always have to prefix the type's name (its tag) with struct, union, or enum when referring to the type.

You can get rid of prefixing keywords by using a typedef, which is a form of information hiding as the actual type of an object will no longer be visible when declaring it.

Login / Signup to Answer the Question.