• Note that we do not host any files on our servers; users just link to it. If you wish to have content removed, please contact the host of the file.

Explaining Header Files In C/C++ For Babies And Newbies

Jitter


Heaven
Joined
Dec 5, 2020
Messages
128,291
Likes
87,380
Awards
8
Etercoin
0
LV
25
 
Consider giving an upvote since I wrote all of this by myself. Also, I've made the images myself


*images kinda broke in nulled and now you have to click on the link to view the images. sorry
Have you ever wondered what these 2 lines of code in your C project are?



Those are called "Header Files". They help us by making our code a lot cleaner. And when I say a lot, I mean it.

That include statement is a pre-processor function. The code inside that header file is executed before your code is executed.

Code:
#include <stdio.h>
Take this line as an example. That "stdio.h" stands for "standard input output". With that, you can use functions like printf or scanf.

-----------------------------------------------------------------------

There are many pre-processor functions out there. Another one is the "define" one. With this you can make constants (variables that their value cannot change).



When you use "define", you should use capital letters. We also don't use the assignment operator, we just put a white space. Also, at the end, we don't put a semicolon.

Here I'm making 2 constants and we are showing the sum of those on the screen. Pretty easy, right?



-----------------------------------------------------------------------

Now that we understand how everything works, let's dive in these header files.

First, create an empty file, and name it however you want. Make sure that it contains ".h" (so it will be a header file)




Now you should see in CodeBlocks that a new folder popped up in your explorer. Here is the place where all of your header files will be saved.



Inside of this header file, we can write anything you want. For example, I've created 2 more constants



Now it's time to make it work. All we have to do is to include it, like in the following image:



Notice how we used quotation marks instead of those brackets? We do that since our header file is in that project. Otherwise, it will probably throw an error, so pay attention to that.

And here I made a little program that uses those constants in the header file and outputs the sum of those 2 numbers.



-----------------------------------------------------------------------

In header files, you can also write C code inside of it. I recommend that you use functions instead of writing it normally. Below I've made a function that calculates the sum of 2 numbers that the program got from input



To make it work, we'll come back to our C file and call the function



And here's the console:


-----------------------------------------------------------------------

So that's it! Header files in a nutshell. There's more to it, but I can't cover everything by typing it. I recommend that you search some videos on YouTube about these header files. They can help you a lot (if you use them correctly).

I would appreciate if you could upvote this thread, it really helps me out, since I actually wrote all of this, and it kinda took me a while.

If you want me to talk about other stuff in C/C++, tell me in the comments what else I should cover. See ya next time!


Edited by TheWolfChad, 12 February 2021 - 06:33 PM.
 
Debrid Link

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.



User Menu