Suppose we have two files.
functions.c:
#include <stdio.h>
void greet(void) {
printf("Hi!");
}
main.c:
int main() {
// Empty
}
And we need to call greet() from main.c. How do we do that? ๐ค
If you are coming from other languages, you might expect to import functions.c in main.c and call greet().
But C programmers usually create a file named functions.h first, containing the top portion of the function (called declaration):
functions.h:
void greet(void);
Then #include it in main.c and call the function:
main.c:
#include "functions.h"
int main() {
greet();
}
Why not #include functions.c directly? Why use a header file (functions.h)?
How #include works
The working mechanism of #include "filename" is extremely simple--It just copies that file's contents into that spot before actual compilation begins (literally). main.c would look like this just before compilation:
main.c:
void greet(void); // <-- Contents of functions.h
int main() {
greet();
}
So functions.h is not strictly needed here, as long as you put void greet(void); (the declaration for the function greet()) in main.c. Like this:
main.c:
void greet(void); // <-- Declaration for greet()
int main() {
greet();
}
So the question becomes, why we need to declare functions.
Why do we need to declare functions? ๐ค
When we compile our program, we already specify all source files.
Example build command (using the C compiler gcc):
gcc main.c functions.c -o final_program
Since we already specify all source files here, manual import inside the files (like Python) isn't needed. We do not import source files inside source files, we simply build (compile) them together.
The key part
But each one of those source files compile separately, often in parallel to utilise the multiple cores in your CPU. In our case, main.c and functions.c get compiled in parallel. This is the main reason why we need to declare functions.
Lets assume the compiler is compiling this part of main.c:
greet();
Here, we are calling a function which is not defined in the current file. The compiler doesn't know how to call greet(), like which arguments to pass, the types of the arguments, the return type, etc. Without that info, the compiler wouldn't be able to translate it to machine code
When we add a declaration void greet(void); in main.c, we are promising the compiler that a function named greet that takes no arguments and return nothing exists in one of the source files (note: void means "nothing"). Now, the compiler has our promise so it doens't throw an error saying that this function doesn't exist, as well as the necessary information to compile the calling part. The address to the actual function is left empty.
After compilation, each source file becomes an object file, with the file extension .o (main.o, functions.o).
Then, something called a linker, combines these object files together, giving us the final executable. It will fill in the empty function addresses, and throws an error if the function we promised to exit doesn't exist in any of the source files.
So, that's solved now. But we didn't answer the original question, did we?
Why Can't We Just #include "functions.c in main.c?
We said:
"Each one of those source files compile separately, often in parallel to utilise the multiple cores in your CPU."
But if we #include the contents of all files in main.c, main.c would be a single large file before compilation, and we would loose that benefit.
There is also a performance advantage as well. Assume we compiled our project first, then made a few changes to functions.c. We don't need to compile main.c again! We can reuse the main.o file we created earlier. Build systems like CMake do this automatically under the hood. If we had #includeed functions.c in main.c, this wouldn't have been possible, since the compiler sees a single, concatenated main.c file.
Other than that, there is one problem that happens if you #include "functions.c" in more than one file. Because #include just copies the code in, greet() gets defined more than once across your compiled files, and the linker throws a multiple definition error when combining them.
Putting #pragma once in functions.c wouldn't fix this, because it only stops a file from being included more than once inside the same single file during one compilation. It does not stop if separate .c files #include functions.c and create duplicate definitions for the linker.
Thanks for reading ๐
This is my first post for Necoz and was inspired by @csm18!
I wrote this entire article from my phone during a bus commute. Feel free to report if there are any typos/issues! ๐
Top comments (102)
@akhourianmolkumar @star123 It has been a while since I took my last typing speed test. And guess what, it increased!!! ๐คฏ I never knew...
I got this on the first try ๐ฅ๐ฅ๐ฅ
My Peak can reach 92 max to max and my father Peak maybe 110 but you outperformed a 16year experience it head lol ๐ฅ ๐ โ ๏ธ
Thanks bro ๐ซก
Bro today I'll post nothing, bro todays plans?
Why nothing? ๐ค
Bro per day 1 post?
Oh yeah... That isn't required...
bro what's plan I asked, also if you could come online in discord then it will be better.
I do not like Discord bro... ๐ช
why?
??
Because I cannot leave whenever I want...
Bro it was soo hard to find this GIF lol ๐คฆโโ๏ธ From where did you get it?
hmm, bro what's todays plan ?
You don't have any plans? ๐ค
bro I was asking your plans CTOs lol my plans is to update APIC V2 UI for today that's my work also I'm thinking to make akhouri systems website url: akhouri-systems.github.io not sure but yeah that's it now yours?
Yeah yeah yeah!!! akhouri-systems.github.io! Or maybe akhouri-systems.vercel.app in case you want a Next.js website or something with a backend!
bruh what's your plan?
Maybe add a select feature to APIC STUDIO or something? ๐ค
oh PR?
PR? ๐ค
you said maybe a select feature in apic studio so you're saying me to add or you're adding? and if it was for me then I'm Asking what's your plans/.
also; bro does apic studio really need one more feature/?./
and if yes then why.?/
Yeah. If I want to select a region and erase it, APIC STUDIO can't do it yet so I will have to go to a competitor's product.
Okay then I will add, still waiting for your plans lol, me begging 10 times leave it then if you don't have any, I'm online in discord if you want you can come.
Also, if I accidentally select a huge model but I meant to select a smaller one, I cannot stop removing the background so I can switch to a smaller one (it happened right now again; I lost count).
okay I'll fix.
I'm Fixing, /\
Another bug I found. Here is how to reproduce:
Expected: background removed
What happened: background wasn't removed, but APIC STUDIO said it did
Workaround: Clicking Undo then Redo removes the background.
Maybe the background was removed internally, but the preview wasn't updated? ๐ค
I opened an image, then I added a speech bubble image layer. I tried to remove the background of that speech bubble layer, but I can't, I think. It removes the background of the original layer instead. All layers should support all features.
Also, the AI background removal is very bad in certain situations. A manual select + erase is better when the background is white (like my speech bubble image). That's why I said we need a select feature (including sub-features like expand, contract, feather, invert, etc.). Also, there is no eraser tool; there is no way to draw anything. I am unable to add a box or circle or oval.
The zoom doesn't work when I have more than 1 layer. Also, I can only access these controls in Remove Object and Advanced:
Bro is there a feature to temporarily disable snapping? Like in other editors I can hold Shift to disable snapping while dragging.
Bro there is only 5 fonts we need more ๐ฅ
Bro everything should apply to the currently selected layer, not the original layer. If I do anything (like remove bg, advanced, whatever) it happens to the original layer not the selected one.
bro currently does it really need more fonts just think yourself lol also all bugs fixed
okay will be added soon.
Bro dragging selects an area. But to remove that selected area, we have to scroll down, find the Clear button, and click it. It can be really annoying for new users. Either you should add an "X" button on the top right of the selection or something, or make selection only start by clicking a "Select" button. A better way is to make clicking "Crop" or "Blur" or "Pixelate" show the box.
Bro adding more fonts is easy... It really needs more fonts. ๐
okay then I'm adding it,
๐
Bro someone vibe coded a whole OS lol have you heard that viral AI slop OS made from claude? in youtube it is now a viral roast topic.
No... ๐ค
But bro aren't you supposed to do your job?
Go and do your job... ๐ซต
bro check apic studio now, also now my job over yeah I know people work 9-5 but I work 9-5(minutes hehehe hehehe hehehe hehehe) just joking but all bugs fixed right as of now? because I'm now scrolling shorts (uh I lied) actually I'm listening this masterpiece:
youtu.be/r-fBULbTtdM in fact while working I was listening this hehe
Ok... You did all of that this quickly?!
yup, and now I'm thinking I do my homework(option A), or I play roblox (option B) or I talk with you in discord if you're ready which you're not ๐uh (option C) or just stay randomly online in dev.to, while in multitasking watch 50% GODZILLA : THE KING OF MONSTERS movie 19th time guess I'll watch if I choosed this option(OPTION D)
now you tell me?
for me I WILL choose option C but I know you ๐ซต
I am going to study for the exam... So you do option A! ๐
you're sly guy.
just like me ๐๐ฅ
okay Founder mode off, School mode on going to do homework but while keep this tab and discord tab open.
Ok, I'll keep this tab open! ๐
Bro what did you do to APIC STUDIO? ๐ง๐ง๐ง That is not what I meant lol ๐คฆโโ๏ธ
what I did? bro tell is that too wrong?
peak brave
Really?? ๐คฃ๐คฃ๐คฃ๐
yeah average brave user experiences this daily lol they are savage. I love brave browser a lot ๐โ ๏ธ
I also use Brave on mobile. On PC still Chrome...
bro what I did wrong? in apic studio
lol you're legend hehe brave in mobile and pc chrome
ME PURE BRAVE USER BOTH LAPTOPS WITH BRAVE, AND BOTH MOBILES TOO
Bro you repeated all of that in the Layers section again. What I meant is that if we select a layer by clicking it (which it already does), we can go to ANY section in the whole editor (bg remove, obj remove, advanced, whatever) and make changes, and they apply to the currently selected layer. That's what all photo editors do; do not repeat all features in the Layers section ๐คฆ
bro it is urgent fix?
Not urgent but still...
Wow๐ฎ๐ญ 134WPM
Which website?
monkeytype.com
Okay bro, I'll write the post , and then share the result there ๐
What? ๐ค
I will ask people about their typing speed. Is this a good idea?
Yeah I think... ๐ค
Bro, this is good!๐
I added it just now!!! How did you see that? ๐ค
Bro, I learn by reading your code.
Really? ๐๐๐ I am honored!!!
yeah huge respect!
Huge respect to you as well ๐ซก
I've mostly worked with languages that use imports, like TypeScript, Java, Python, and PHP, so learning how C handles separate compilation and linking was really refreshing! ๐ธ
And congratulations on your first Necoz article! I love seeing members inspire each other to write new articles! ๐
๐ Glad you learned something from me :)
Absolutely awesome! You nailed a topic that many beginners struggle with. ๐ฅ
Also, I'm really thankful to you for considering my post an inspiration! ๐
Hehe thanks! ๐
Absolutely!
Also, your post wasn't the inspiration. You were the inspiration (yeah, the post was also inspiration, but you were the first inspiration because the post came after I got inspired :))
trailing return types in cpp
while loop and for loop are not the same in C/C++
That means a lot to me man! Thank you ๐งก
Thanks! ๐
@csm18 both of our posts got the exact number of reactions! ๐ซจ
Cool!
Really enjoyed this explanation! It's refreshing to see someone explain why C works this way rather than simply telling beginners to use header files and move on.
The distinction between compilation and linking is one of those fundamentals that becomes much more valuable as projects grow.
It also makes me wonder what you think about unity builds, where developers intentionally combine multiple source files to improve compilation performance. At what point do the benefits outweigh the drawbacks of separate compilation?
And writing this entire article on your phone during a bus commute? That's impressive. I can barely write a decent commit message on mine. ๐
Looking forward to more articles like this!
Thank you so much for the feedback! ๐ When I build an ESP-IDF project, the first compilation is very slow. But subsequent compilations are much faster. I think separate compilation is better during development, since we often update a few files and rebuild? ๐ค
I'm quite noob in mobile too.
Everybody's noob on mobile. Particularly on my mobile. Because it only has 4GB RAM. Apps automatically get cleared so there is no multitasking :( If I am writing an article and want to Google something, and I come back to write the article, the entire browser reloads ๐
I tried everything I know to fix this but no wayyy :(
I personally hate compile errors and that fact that direct source inclusion does not work from .c to .c file. It would save me the headache of trying to manage variables in headers and functions in c and then updating all the headers when the C files change. This is why I like the NASM assembly compiler or python or go or even JavaScript because you can include the source files directly without headers.
Yeah same here. Updating header files are really annoying at times ๐ It feels like unnecessary effort to manage a copy of all function declarations in the header.
Bro, this is really well written.๐๐ป
เชฌเชนเซ เชธเชพเชฐเซเช เชเซ :D
Thanks Star โบ๏ธ
I was waiting for this!
Thanks! I remember you telling me :)
I think the parallel compilation point finally made headers click for me.
Thank you so much! Glad my explanation did what it's supposed to do! ๐
The explanation of why pragma once cannot prevent duplicate definitions across separate compilation units is especially useful. One detail I would clarify: reusing unchanged object files is normally the build systemโs job. Running the shown gcc command again compiles both source files again; a Make or Ninja example would make that distinction visible.
Thanks for the comment. That is correct! ๐ I updated that section to this:
Thank you so much for pointing that out!
Thanks for updating it! That makes the object file benefit much clearer. I would keep one small qualification beside it: main.o can be reused when its own source and included headers are unchanged. A change to a shared header can still require recompiling both translation units.
Yes, that's true as well! ๐
Clear, beginner-friendly walkthrough โ I liked how you showed the preprocessor literally copying the file in. The parallel-compilation and duplicate-definition reasons for using headers make a lot more sense now.
Thanks a lot! ๐