Getting confident with header files in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
engineer man here today we're going to make programming cool again by teaching you about header files ready good okay to use levels beginner what is a header file for header files for declaring functions and macros specifically those that should be shared between source files our header files required well know but you'd be silly not to use them if you would like you can always take the contents of the entire header file and copy and paste it into all of your source files that is what the C compiler is gonna do anyways but that's a lot of maintenance so it's just easier to use header files example time yeah I think so are we looking at three files the first file is just your main source file and we're gonna come back to this you could see the include statement we'll come back and describe how that works later but in this main function we're really just calling a function that's included from the header file we're using a macro that's included in the header file and then a type def that's included in the header file the second file we're looking at is going to be the actual functions source file and in here there's just one function it's just a it's an add function returns manager and it returns the sum of two numbers that's it that's all it does the final one we're looking at is the actual header file this is the most important one so in the header file there is the first we're going to look at is these two lines appear this is called the header guard and the whole purpose of the header guard is to ensure that in a given program one header file is not expanded multiple times so in this case what would happen is the first time this header files include it's gonna say if not defined functions underscore H then do all this so the first thing it's going to do is define functions H and that'll ensure that if it's occluded again functions H is going to be defined and therefore it's not going to run this so in our header file we have three things we have a function declaration we have a macro declaration and then we have a type def declaration these are going to be the three things that were wanting to share with our main source file in this case so now we'll go back to our main source file so what happens here is you're going to include the header file now when you include the header file and see goes and our GCC goes and compiles your program really what it's doing is it's saying alright I'm gonna look for functions each in the current directory ok it's there I'm gonna come I'm going to grab all the contents and I'm gonna just dump it right into there this is what GCC will do in the end now because of that let's look at our make file in the compilation here in our compilation we have main dot C functions dot C notice that we do not have functions dot H and that's because you're not really compiling functions H the GCC is gonna include it in each source file and it's gonna compile about whole file so there's no actual need to include it in your actual GCC code and just like that you're that much smarter and header files pair yourself on the back you earned it in the comments below let me know if I missed anything or if you have any questions I read usually reply to every comment and I love reading your feedback and make sure to punch that subscribe and like button too if you feel like I deserved it see you next time
Info
Channel: Engineer Man
Views: 35,028
Rating: 4.931818 out of 5
Keywords: engineer man, header files, c programming
Id: mR8P0NU-ues
Channel Id: undefined
Length: 3min 20sec (200 seconds)
Published: Fri Nov 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.