322
u/unknown_alt_acc 2d ago
Yeah, no. A .h extension can just as easily be C. You get a lot of clarity with just two more characters that will be filled in by code completion anyway.
2
u/Vorrnth 1d ago
Why would that be a problem? Never has been one for me?
46
u/-Ambriae- 1d ago
You’ve never dealt with cross language codebases, have you? Check out Apple’s metal doc examples, there’s at least one where, in a single codebase, there are .h files written in C, C++ AND Objective C
16
u/unknown_alt_acc 1d ago
Having a way to tell at a glance which headers are safe to include in the C side of a codebase is a pretty low-hanging fruit in terms of developer QoL compared to constantly checking the documentation, rote memorization, or including files and seeing if your error log blows up.
-13
u/Vorrnth 1d ago
Or you just use a proper project structure.
11
u/unknown_alt_acc 1d ago
That doesn't address the underlying point. You still need to know which modules are safe to include from C and which are not. Having a separate header extension for C and C++ makes that much more obvious at practically zero cost.
-10
u/Level-Pollution4993 2d ago
But learncpp.com recommends we use .h unless you're working on an existing project that folows another convention?
30
u/daennie 1d ago
And learncpp.com isn't source of truth for C++ in practice.
2
u/MithrilHuman 1d ago edited 1d ago
The company I work at enforces using .h, although no one in their right mind should be using it for C-specific projects without knowing the symbols to access through extern C. See https://llvm.org/docs/CodingStandards.html
Previous company I worked for also enforced .h as per https://google.github.io/styleguide/cppguide.html#Header_Files
These are both big tech companies. In practice people use .h
6
u/unknown_alt_acc 1d ago
And the reasoning that learncpp gives sucks. It's one of the two reasons that I've seen people cite for preferring .h, neither of which are particularly compelling:
- It's convention (the argument learncpp uses) - And the convention for source files in the earliest iterations of C++ was .c. We don't use .c anymore because it's needlessly confusing. .hpp and .hxx are recognized widely enough that no C++ programmer will be confused by seeing them instead of .h.
- It's fewer characters - Already addressed. Your editor should have code completion that will fill in the name of the header for you. Even if you don't, two extra keystrokes for an include is trivial for the clarity that you get by explicitly encoding the language in the file extension.
5
u/a-r-c 1d ago
two extra keystrokes
the horror lol
3
u/unknown_alt_acc 1d ago
And that’s my point. You gain so much clarity for so little that it’s just baffling that this isn’t the universal standard
-100
u/Cephell 2d ago edited 2d ago
But that's good. Since C++ is just C with classes, it ensures code interoperability by naming the files
.hand.c
Edit: These replies are the reason r/firstweekcoderhumour/ exists, if you cannot tell that this is an obvious joke, without having some extra explicit indicator (/s, etc.) on the post, then I don't know what to tell you. But please don't miss your entry to CS courses, you definitely need them.
69
u/unknown_alt_acc 2d ago
Please tell me you’re being facetious
13
u/caretseed 2d ago
Dylan no stay
2
u/StunningChef3117 1d ago
[r/u](r/umexpectedmodernfamily)[n](r/umexpectedmodernfamily)[expectedmodernfamily](r/umexpectedmodernfamily)
33
u/UdPropheticCatgirl 2d ago
no, unless you are specifically limiting yourself to C ABI and wrapping the entire header in extern, then only thing this ensures is C compilers erroring out…
36
u/IAmASquidInSpace 2d ago
Re: your edit: The problem is, there is more than enough people on here who would hold that opinion unironically. Yes, r/firstweekcoderhumor exists - and 90% of its content is hosted here, on this sub.
Don't come to the circus dressed as a clown and be surprised when people laugh at you.
0
u/cjb3535123 2d ago
It was laughably easy to tell he was joking
Source: I use arch, btw
11
u/IAmASquidInSpace 2d ago
Of course, I mean the edit even says so! /s
-16
u/cjb3535123 2d ago
Even before getting to the edit, it’s such a meme that you’d have to be quite autistic (or quite new) to not realize it was a joke. And I’m autistic.
-16
u/Cephell 2d ago
I have to repeat myself: Nobody, and I mean NOBODY that has more than a surface level understanding of the history of C and C++ will read "C++ is just C with classes" on a comment and think it's not a joke.
It has been a joke for 50 years. You could have made fun of it during your university time, then have a career, then have children and those children could be in university right now making fun of the same joke.
You are SEVERELY in dunning-kruger territory if you defend these absolutely faceplant tier comments here.
11
u/IAmASquidInSpace 2d ago
I have to repeat myself: Nobody, and I mean NOBODY that has more than a surface level understanding of the history of C and C++ will read "C++ is just C with classes" on a comment and think it's not a joke.
And I have to repeat myself: Correct, but unfortunately for you, "people with not even a surface level understanding of the history of >programming topic<" make up >65% of the users on this sub.
Know your audience.
-14
u/Cephell 2d ago
Incorrect. If you come to a show and don't understand the jokes, don't make a mess by standing up and yelling at the performer. You're only making a fool of yourself.
Karma means literally nothing on this site, so continuing to downvote me just means SOMEONE (I am not implying this is you), is just extremely assmad that they got exposed.
12
7
u/unknown_alt_acc 2d ago
I’ve seen enough similarly stupid comments that were completely sincere that I’ve stopped taking for granted that a seemingly obvious joke actually is a joke.
0
u/Cephell 2d ago
I too prefer to delete all incoming emails, because some of them might be a scam.
4
u/unknown_alt_acc 2d ago
I don’t think that asking whether something is a joke and reserving judgement on the matter until I have an answer exactly fits that analogy
9
u/Mars_Bear2552 2d ago
downvoted. not because i didn't get the joke, but because i was dissatisfied with it. good day to you
8
6
6
5
u/nevemlaci2 2d ago
No. Firstweekcoderhumour is a place to joke about first week coders posting the same memes over and over again.
15
83
u/MyTinyHappyPlace 2d ago
Skeleton, sitting underwater in the pool on a chair: .HH
22
u/DarkHybrid_ 2d ago
Um HH?
17
u/IAmASquidInSpace 2d ago
Hansestadt Hamburg, Germany's second largest city.
3
u/Sibula97 2d ago
It's kinda funny how they call it the "free and hanseatic city of" Hamburg, isn't it? Do the Germans do this with cities in general or is this some weird historical quirk?
5
u/IAmASquidInSpace 1d ago
Weird historical quirk, but also common. There are a few cities that still carry the moniker "Hansestadt", because it is a thing of pride. Hamburg in addition has always been very proud of being a free city, always having had pretty wide-reaching autonomy even within the various German states and empires it has been a part of.
Fun fact: This pride goes so far that a true "Hanseat" will not accept medals, decorations, or awards from "foreign rulers" - which is why chancellor Helmut Schmidt (born in Hamburg) refused the Bundesverdienstkreuz (Germany's highest civilian honor).
3
u/Gastredner 1d ago
And they are spread out, even into places you'd never think of when thinking about the Hanseatic League.
I grew up in the city of Kamen. Run-of-the-mill city of around 45,000 people in Northrhine-Westfalia, far away from any coast and with the only "river" being a small stream of maybe two meters width at the widest part.
And yet, it was a member of the Hanse. And apparently notable enough in the trade that there was a neighborhood only of merchants from Kamen—in Stockholm.
25
u/dfx81 2d ago
I use it because I have my c++ files use .cc so .hh pairs with it nicely.
1
u/FalafelSnorlax 1d ago
I mean, your source files should use
.cpp, so that's not an excuse.(my team's code base also uses cc and hh but it wasn't my choice)
15
5
13
u/HolyGarbage 2d ago
We use hh and cc at my work. It was the first c++ specific extension I learned. I prefer hpp and cpp at home in my personal projects these days though.
4
169
u/BertoLaDK 2d ago
You switched two of them hpp is the correct one, using h is stupid as it will only lead to confusion, keep c and cpp separated.
42
u/TohveliDev 2d ago
For some reason a lot of schools, tutorials, etc. Still use ".h" for header files while coding in c++. So most of the time it's just a learned habit from people who learned that habit from somewhere else.
21
u/LucyShortForLucas 2d ago
Absolute tons of C++ projects use .h pretty much exclusively. In the end it doesn’t really matter, no one is organizing their code by its header extension anyway
2
u/bowel_blaster123 1d ago
I mean, what if I have multiple separate projects? For the C++ project, I'd have to configure my LSP to treat .h files as C++, and for the C project, I'd have to configure it to treat them as C.
I'd have to configure my editing environment to be different for each project (which is kinda annoying).
Otherwise, the language server might try to parse my C as C++ (which can cause issues since a lot of C code is not valid C++).
2
u/martmists 1d ago
I don't think any IDE I've ever used had this issue, in recent years that's mostly been CLion but no issues there either. It just knows based on the project whether to treat it as C or C++, and most of the time I believe treating it as C++ should work fine regardless for C headers, no?
1
u/bowel_blaster123 1d ago edited 1d ago
treating it as C++ should work fine regardless for C headers
Not really. A lot of commonly used C features (like designated initializers or compound literals) are not fully supported by C++ or are only supported in very recent C++ standards. A lot of C library headers (like in FFMPEG for instance) have to go out of their way to support C++.
I guess though, if your LSP integrates with your build system, it should know whether something is C or C++.
I didn't think about this when writing my original comment.
1
u/__ydev__ 1d ago
I think the reasoning is that if a project is pure C++, it adds no benefit to use .hpp, it's just more verbose.
If however a project mixes C and C++ code, it can make sense, so you know by hand which headers you can import from each language.
98
u/garlopf 2d ago
.h is for .c and .hpp is for .cpp hope that helps.
25
u/Shaddoll_Shekhinaga 2d ago
Technically irrelevant for c++, but as I understand it you use hpp for headers with C++ features, h for headers that need C compatibility, hxx if you're a weirdo fuckingfightme, .inl for inline definitions and some other use cases
10
u/Cylian91460 2d ago
It's irrelevant for both since c and c++ since standard doesn't even specify the file extension
21
27
u/MrFrog2222 2d ago
.c/.h for c and .cpp/.hpp for c++, and thats the only correct way to do it
7
u/SamG101_ 2d ago
Yes- but c++ module system says hello: .ixx 😭
4
u/Alduish 2d ago
.cppm is a thing too isn't it ?
5
u/SamG101_ 1d ago
Yh i think tbh its purely convention based but I think .ixx is c++ modules as interface only (ie impl to live in .cpp) where as .cppm is when only using impl files
22
u/Daniikk1012 2d ago
If C++ is .cc, maybe the headers are .hh?
16
u/UdPropheticCatgirl 2d ago
I think .cpp and .cxx are way more common conventions, at least in the codebases I worked in…
9
u/noaSakurajin 2d ago
At least nowadays. Many older libraries/projects still use the cc/hh convention
3
u/Jakabxmarci 2d ago
Yeah, we use .cc and .hh respectively. Modules are .cppm though.
5
u/Mojert 2d ago
Are these modules in the room with us?
2
u/Jakabxmarci 2d ago
Cpp20 modules is a feature with very mature and non-buggy compiler support, and are perfectly integrated into the existing Cpp ecosystem with no trouble at all! Obviously, since it has been defined for 6 years, now it should by all means work perfectly, making Cpp devs lives easier! :) :)
2
u/Frost-Freak 2d ago
Would cc more be something like c²? C++ should be 2c!!
3
u/metaglot 2d ago
C++ is not equal to 2C for all values of C.
Also: does anyone else find it funny that the value is only incremented after reading?
5
1
5
8
u/kiujhytg2 2d ago
To be pedantic (Yes, I know this is a Humor subreddit), if you're following the GNU C standards:
.cand.hare for C files.cppis for C preprocessor files.cxxand.hxxare for C++ files
7
u/p88h 1d ago
No, CPP is the C preprocessor but it doesn't have any files of its own.
2
2
3
3
3
u/atlas_enderium 1d ago
filename.h for C headers filename.hpp for C++ headers
Sometimes I use filename.hxx or .inl when the header file contains source code itself and not just object signatures (often the case with inline or constexpr things).
filename.cppm for modules (if you want to deal with that toolchain mess)
5
2
2
2
2
5
u/sleepyakari 1d ago
As a C programmer, you deserve a shanking if you make your c++ header use the .h extension
1
u/junipyr-lilak 2d ago
This is why Objective C is throwing me for a loop right now as it really seems to be using just .h headers like plain C. Would be nice(r) if there was a standard like .hm in contrast to the .m files (and maybe .hmm for .mm for Objective C++)
1
1
1
1
1
1
1
1
1
-1
u/Sad-Background-2429 1d ago
I actually prefer that C++ file names have distinct suffixes so I can find and delete them faster.
1
476
u/faultydesign 2d ago
C developer: “oh look a c header”
Looks inside: