Your Second Day in C (Understand .h header and .c source files) - Crash Course in C Programming

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 มิ.ย. 2024
  • ►C Programming playlist: • C Programming Series
    ►Find full courses on: courses.mshah.io/
    ►Join as member to get perks: / @mikeshah
    ►Lesson Description: In this series of lessons I provide you with a crash course in the C programming language. In this lesson I will show you the difference between a .h file, and a .c file. This will lead us into a small discussion and demonstration of how to structure a C project. We'll also be reviewing several of the topics from the first video in the playlist linked above.
    00:00 Introduction
    00:40 Discussion of header versus implementation files
    3:40 Creating a main.c file.
    4:00 The vector struct that we are creating.
    5:20 The header and implementation for our math library
    7:00 Sketching a vector 'Add' function.
    8:45 Including files is a copy and paste by the preprocessor
    10:20 Header Guards discussion and usage
    11:40 Implementing the functions in our header file in our .c file.
    12:40 Refactoring our 'Add' Function
    15:00 const parameters
    15:57 Using our Math Library
    17:00 Different ways to include files
    20:25 Successfully running our program.
    20:53 Review of what we have done.
    24:14 Review of compiling multiple files in C
    26:00 Conclusion and wrap up
    ►Please like and subscribe to help the channel!
    ►TH-cam Channel: / mikeshah
    ►Join our free community: courses.mshah.io/communities/...
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 35

  • @werhanihoussemeddine7309
    @werhanihoussemeddine7309 3 วันที่ผ่านมา

    That is a very good video. I enjoy watching it, but I have a couple of questions. Firstly, I do not understand why he refactored the Add function. Instead of getting two values and returning a value, he changed it to return nothing and passed two references. The result will be the value of the first variable (out). It seems like not a good practice. Lastly, why doesn't he simply pass a value for the variable (in) instead of passing a reference if he is not changing its values?

    • @MikeShah
      @MikeShah  3 วันที่ผ่านมา

      Indeed, there's a few ways to do add. Here's yet another way we could have done things.
      // The 'int' return value can be used for an error code which is common in C. We could use it to detect overflow, underflow, or otherwise return some useful information.
      // The cleanest way to use this function is probably to create a new vector for the out_result -- that's a 'functional' style so we can track the side effects.
      int AddVector(Vector_h* out_result, const Vector_h a, const Vector_h b){
      out_result.x = a.x + b.x;
      out_result.y = a.y + b.y;
      return SUCCESS;
      }
      Part of this example is to focus on header files and organization, but you're right there's many decisions to be made in regards to how we pass in and out data.
      Now we can probably provide another example where we as you suggest do what is most natural. This allows us to do what is most obvious, and probably what I should have shown. In many cases, the obvious case is also something the compiler can optimize/inline as well (and most humans will also understand exactly the intent).
      Vector_h AddVector(const Vector_h a, const Vector_h b){
      Vector_h out_result;
      out_result.x = a.x + b.x;
      out_result.y = a.y + b.y;
      return out_result;
      }
      So in short, agreed I probably should have just showed this example in the video :)

  • @joaocesarlima7339
    @joaocesarlima7339 ปีที่แล้ว +9

    You're now my number one source for learning C/C++ on TH-cam! Please keep it up!

    • @MikeShah
      @MikeShah  ปีที่แล้ว +2

      Cheers, thank you for the kind words!

  • @viktorkomarov8041
    @viktorkomarov8041 9 หลายเดือนก่อน +2

    This is some sort of perfection: the quality of video, the theme you uncover, screen management, sound and explanation. You're criminally underrated teacher and video maker.

    • @MikeShah
      @MikeShah  9 หลายเดือนก่อน

      Cheers, thank you for the kind words!

  • @dhanushs1802
    @dhanushs1802 ปีที่แล้ว +3

    Very Happy I found you on youtube. You are a wonderful teacher. Thank you.

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      Thank you for the kind words!

  • @chriskiesel5360
    @chriskiesel5360 ปีที่แล้ว +1

    The world needs more people like Mike. That’s all I can say, you made it look so easy and smooth…. Thank you so much ❤

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers, thank you for the kind words!

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 2 ปีที่แล้ว +5

    as always brilliant explanation thank you Mike

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      You are welcome!

  • @abdodaood3778
    @abdodaood3778 ปีที่แล้ว +1

    You are not the only programmer whose lessons I follow but you are the best at all 💯, go ahead.

  • @blaisofotso3439
    @blaisofotso3439 2 ปีที่แล้ว +3

    Excellent!
    I love the video.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      Thank you for the kind words!

  • @jimstorch4110
    @jimstorch4110 2 ปีที่แล้ว +3

    Really enjoying the series Mike. Just curious, did you mean to use 'vector_h' or 'vector_t' as the typedef?

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      Yes I did, good catch! _t is the proper convention in C for user defined types :)

  • @aghiadalzein3069
    @aghiadalzein3069 6 หลายเดือนก่อน +1

    This course is the best way to learn C ,thanks for the great effort .Will you add more videos in the future??

    • @MikeShah
      @MikeShah  6 หลายเดือนก่อน

      Cheers! Yes, over time I'll keep adding 🙂

  • @slavapavlov9473
    @slavapavlov9473 2 ปีที่แล้ว

    Hey Mike, thank you for the video!
    To recap.
    Let's say, we have our custom functions declared in custom.h
    We *#include** "custom.h"* both
    in custom.c (our functions are actually implemented here) and
    in main.c (our functions are used here).
    We don't compile .h files. Only .c files.
    For instance, *gcc main.c custom.c*
    The compiler glues them together and gives us one executable.
    Also, remember about so-called header guards in .h files.
    And be careful about data types we're passing into our functions.
    To be frank, I got the gist of it but to feel more comfortable with this topic I'll be looking for more examples in my C programming journey.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      I think you got it!

  • @deepblackoutlaw9640
    @deepblackoutlaw9640 4 หลายเดือนก่อน +1

    thank you for the tutorial

    • @MikeShah
      @MikeShah  4 หลายเดือนก่อน

      Cheers!

  • @hounangogogo
    @hounangogogo 2 ปีที่แล้ว +2

    Hey Mike, thank you for providing this amazing courses. Do you plan to publish this C course, and Computer System course to Udyme?
    Thanks

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      Eventually I'll rerecord the lessons in shorter videos and add exercises, and then post to thinkific or Udemy. There are a few systems videos in the pipeline a few weeks from now I am thinking qbout for posting here.

  • @akash-tj8ru
    @akash-tj8ru ปีที่แล้ว

    Pls make a video on
    "multiple line Macros" in C. Thanks for such nice explanation...very informative and point to point explanation.

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers! Will note that down under the wishlist!

  • @tristan6535
    @tristan6535 ปีที่แล้ว +1

    Hello, great video. I didnt really understand whats the problem with the function having a return type. Why did you use the pointers instead?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Can you provide a timestamp?

    • @tristan6535
      @tristan6535 ปีที่แล้ว +1

      You are starting to talk about this at 12:40 and you say it will cause a problem if „result“ is being deleted and I don’t really understand why it should be deleted so what the problem could be

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      @@tristan6535 For this example it actually shouldn't cause a problem since we are using primitive types. I think this is me foreshadowing heap and stack allocated memory in future lessons :)

    • @tristan6535
      @tristan6535 ปีที่แล้ว +1

      Oki, thank you very much for the quick response. Then I will go on watching the next vides :)

  • @rudolfbouzek6944
    @rudolfbouzek6944 3 หลายเดือนก่อน +1

    hi, its a good series. no offend, but i expect indian accent... nice suprise :D

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      😅