DEV Community

Why Can't We Just #include "functions.c" in "main.c"? 🤔

EffessDev on November 26, 2026

Suppose we have two files. functions.c: #include <stdio.h> void greet(void) { printf("Hi!"); } main.c: int main(...
Collapse
 
effessdev profile image
EffessDev Necoz • • Edited

@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 🔥🔥🔥

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

My Peak can reach 92 max to max and my father Peak maybe 110 but you outperformed a 16year experience it head lol 🔥 💀 ☠️

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks bro 🫡

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Bro today I'll post nothing, bro todays plans?

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Why nothing? 🤔

Thread Thread
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Bro per day 1 post?

Collapse
 
nyaomaru profile image
nyaomaru Necoz •

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! 💚

Collapse
 
effessdev profile image
EffessDev Necoz •

🙀 Glad you learned something from me :)

Collapse
 
csm18 profile image
csm Necoz •

Absolutely awesome! You nailed a topic that many beginners struggle with. 🔥

Also, I'm really thankful to you for considering my post an inspiration! 😊

Collapse
 
effessdev profile image
EffessDev Necoz • • Edited

Hehe thanks! 🙂

Collapse
 
csm18 profile image
csm Necoz •

Absolutely!

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

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 :))

Thread Thread
 
csm18 profile image
csm Necoz •

That means a lot to me man! Thank you 🧡

Thread Thread
 
effessdev profile image
EffessDev Necoz •

Thanks! 🙂

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

Collapse
 
johnclentoira profile image
john Clent Oira •

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!

Collapse
 
effessdev profile image
EffessDev Necoz •

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? 🤔

Collapse
 
akhourianmolkumar profile image
Akhouri Anmol Kumar Necoz •

I'm quite noob in mobile too.

Thread Thread
 
effessdev profile image
EffessDev Necoz •

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 :(

Collapse
 
gtref profile image
G-type Necoz •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

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.

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ Necoz • • Edited

Bro, this is really well written.🙌🏻
બહુ સારું છે :D

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks Star ☺️

Collapse
 
technogamerz profile image
𝐓𝐡𝐞 𝐋𝐚𝐳𝐲 𝐆𝐢𝐫𝐥 Necoz •

I was waiting for this!

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks! I remember you telling me :)

Collapse
 
the_daily_flare profile image
The daily flare •

I think the parallel compilation point finally made headers click for me.

Collapse
 
effessdev profile image
EffessDev Necoz •

Thank you so much! Glad my explanation did what it's supposed to do! 🙂

Collapse
 
emilywoodsnyc profile image
Emily Woods •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks for the comment. That is correct! 😊 I updated that section to this:

Also, 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.

Thank you so much for pointing that out!

Collapse
 
emilywoodsnyc profile image
Emily Woods •

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.

Thread Thread
 
effessdev profile image
EffessDev Necoz • • Edited

Yes, that's true as well! 😊

Collapse
 
enoch_hou profile image
Enoch Hou •

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.

Collapse
 
effessdev profile image
EffessDev Necoz •

Thanks a lot! 😊

Collapse
 
nour_dude_314 profile image
nourdude Necoz •

honestly idc i just include everything (#include functions.c) because why tire myself with .h files?

Collapse
 
effessdev profile image
EffessDev Necoz •

It's also completely valid 🤣 Thanks a lot for the comment!

Collapse
 
effessdev profile image
EffessDev Necoz •

Why @xulingfeng why? 😢

I'll write the next article for The Third Cup...

Collapse
 
xulingfeng profile image
xulingfeng Necoz •

😅

Collapse
 
effessdev profile image
EffessDev Necoz •

Hehe thanks 🙏🙂

Collapse
 
syedfahads profile image
Syed Fahad •

writing all this on a bus from your phone is impressive lol. fun twist for a follow up: some projects do include .c files on purpose, like unity builds and SQLite shipping as one big sqlite3.c

Collapse
 
effessdev profile image
EffessDev Necoz •

Yes you are right! @johnclentoira also mentioned it in the comments. Thanks for the comment! 🙂