Include needed headers in both .c and the corresponding .h or only in the .h?

Assume I have a module filter.c/filter.h.
That module exposes a function in the header:
uint32_t filterStuff(uint32_t input);

This means I need to include stdint.h, and I need it in both the filter.c AND the filter.h
In the past I simply included stdint.h in filter.h. Since filter.h is also included inside filter.c the code has the int32 typedef available as well and everything works.

But when I check that code with clang-tidy I get a

no header providing “uint32_t” is directly included

warning from misc-include-cleaner.

Does this suggest I should include stdint.h in both filter.c AND filter.h separately? Is that considered good style?

NOTE: I’m asking only about the case where something is included in the header directly belonging to some c-file: foo.c and foo.h. I’m not asking about trusting correct indirect includes from headers NOT directly belonging to said module.

3

The header filter.h should include <stdint.h> since one of the functions it declares needs one of the types from <stdint.h>, and a header should be self-contained, minimal and idempotent. (For more discussion, see Should I use #include in headers? and other questions.)

The code in filter.c necessarily uses filter.h, so it does not really need to include <stdint.h> directly. However, other source files might use filter.h for some parts of the code but also use types from <stdint.h> separately from the use mandated by filter.h. It becomes contentious whether those other files should include <stdint.h> directly. The argument in favour of doing so is that if the code ceases to need filter.h, then the <stdint.h> header will need to be added to the source to provide the now-missing declarations. The argument against that is “the code will have to be changed when it stops using filter.h, and adding the <stdint.h> header would simply be one of the other necessary changes”.

I tend towards the “against” view — I would not include <stdint.h>. But I’m not 100% consistent about it (is anyone 100% consistent?). If the policy is to use tools that complain if you don’t include the file directly, then follow the rules imposed by that policy and the tools you use.

I still regard self-containment as important. If I include a header x.h, I should not need to include other headers y.h and z.h (that I may not even be fully cognizant of) in order to use the facilities of x.h.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật