What is the actual difference between a header (.h) file and a source (.c) file?

What is the actual difference between a header (.h) file and a source (.c) file?

Attached: Royal Guards_09.jpg (851x1280, 377K)

Other urls found in this thread:

en.wikipedia.org/wiki/Include_directive
twitter.com/NSFWRedditGif

One contains prototypes and one contains implementations.

You managed to be wrong on both aspects

Nothing. File extensions are just an arbitrary part of the filename and don't contain any special magic that sets them apart from other files.

external functions in c code need to be forward declared before using. "header" files are just reusable function declarations and stuff that automatically inserted into every c file with #include

your mom gives me header and I shoot my source inside her mouth lmao

how is this wrong?

You can write implementations in h files and you can write prototypes in c files.

There's a reason why modern languages didn't continue this naming schema: it has no meaning.

Nothing except for organizational purposes and efficiency using prototypes in multiple source files.

Literally just convention, you could write your declarations in .c files and include those, or implement everything in .h files
You might need to pass some extra options to the compiler if you really abuse this because by default they will assume normal conventions are used

Holy fuck, 6 answer yet NONE of them explain what both are exactly.

en.wikipedia.org/wiki/Include_directive
According to wikipedia, they are just to differentiate between your actual code and shit you'd just "import std.stdio" in higher level languages. I still don't get it though.

Attached: winni.jpg (700x460, 265K)

Okay and your header guards will cause linker error if you write implementations in .h and include the .h in more than one place.

it isn't convention. enjoy duplicating the same structs and defines in a bunch of separate c files and when you make a change updating all the separate duplicate implementations instead of just using one header and making one change.

correct, you can -- however, that isn't necessarily the convention. .h and .cpp distinctions are meaningless if you want to just shove everything into a single source file. you are correct about that.
but a lot of C++ dev shops prefer to keep prototypes separate from their implementation code. it's just for organization but that doesn't make it intrinsically meaningless

additionally, it saves you from have to recreate the same prototypes across multiple files

h you declare and define shit, c you extrapolate shit you declared

Saying it's for organization is a little bit of a half-truth. As long as you use it, you'll organize around it; but if you didn't have to use it then many won't. It's really about compiler optimization and preventing scope leaking. Which is why other languages don't have these concepts: because those features have been baked into the language. (In particular they bake in compiler optimization by inventing abstractions that make it impossible so they don't have to do it)

What the fuck are you talking about? Writing your declarations in a c file and including that everywhere is functionally equivalent to writing it in an h file and including that everywhere. And you only need to change one file in both cases.

>including .c file in other files
haha no

Bongs are so shit at military and intimidation holy fuck, they're like awkward schoolboys compared to Germans or Russians

Have you ever tried it? It will literally work.

Is it good practice? Fuck no, conventions exist for a reason, and this is a pretty fucking strong convention. Can the convention be broken, will it work? Absolutely.

wrong. you can have a static function declared and implemented in a header and it will cause no issues when linking.

for what benefit? Sake of argument only?

it is important to understand that each c file is a self-contained program that compiles into machine code in obj file. so all function and type declarations must be written in that c file.

And a none static function?

The only difference is in the intent of the programmer. .h files should be for function declararions and macro definitions. .c files should be for variables and function implementations.

However, you can put all of these things in either filetype, but not following the convention will confuse other programmers. Also, duplicated function implementations will cause a compiler error.

The compiler comes with a set of header files that describe the functions in the standard library (ie those built into the compiler)

.h are not meant to be compiled, they feauture some functions for later reuse in .c file, .h itself not have main function. It can improve readability, but watch out for including the same header file multiple times in various header files, its common error. In short: if you #include "something2.h" in something1.h, and then you #include "something1.h" in something3.h, you cant include something2.h in something3.h, it will give you an error

Yeah - fucking prototypes and implementation.

You put your prototypes in your header files and then write your implementation in the .c file.
When people want to consume your library, they your header.

this is a retarded post. what are header guards even?

they will cause a linking error because the symbol is defined twice, if the header is included twice in separate .c files.

C was designed so that it could be implemented as a single pass compiler. Therefore functions and variables need to be declared before they can be refered to.