Should struct definitions go in .h or .c file?

Aaron Yodaiken picture Aaron Yodaiken · Jun 11, 2011 · Viewed 95.7k times · Source

I've seen both full definitions of structs in headers and just declarations—is there any advantage to one method over the other?

If it makes a difference, I usually typedef a struct like so in the .h

typedef struct s s_t;

Edit

To be clear, the options are declaration in the header file and definition in the class, or both declaration and definition in the header file. Both should result in the same usability, even if one is by linkage, shouldn't they?


I see many almost duplicates, e.g. here but no exact matches. Please correct me if I'm wrong in this regard.

Answer

τεκ picture τεκ · Jun 11, 2011

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

Public structures should go in the .h file.