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

00:26:38
https://www.youtube.com/watch?v=iZydkjOdQYw

Zusammenfassung

TLDRIn this lesson on C programming, Mike explains how to manage C projects using source (.c) files and header (.h) files. He discusses the compilation process and the importance of separating interface (header files) from implementation (source files) to build efficient code. By illustrating the creation of a small math library, the video sheds light on how functions and structures are declared in header files and defined in source files, and how to handle them during compilation. Key concepts like the use of header guards to prevent multiple inclusions are covered, providing insight into the pre-processing phase of compilation. The lesson helps build foundational knowledge for organizing and understanding C code, especially when working on larger projects.

Mitbringsel

  • ๐Ÿ’ป Understand the distinction between .c and .h files.
  • ๐Ÿ”— Learn to use header guards to prevent multiple inclusions.
  • ๐Ÿ“š Familiarize with creating and using libraries in C.
  • โš™๏ธ Comprehend the preprocessing and compilation process.
  • ๐Ÿ” Recognize the significance of separating interface and implementation.
  • ๐Ÿ—‚๏ธ Know how to organize code into different files for clarity.
  • ๐Ÿ› ๏ธ Implement a basic math library with vectors in C.
  • ๐Ÿง  Enhance understanding of general function declarations and definitions.
  • ๐Ÿ”‘ Get insights into the basics of memory handling with pointers.
  • ๐Ÿ“ Write clean and maintainable C code by following conventions.

Zeitleiste

  • 00:00:00 - 00:05:00

    This lesson focuses on understanding the role of C files, particularly source files (.c) and header files (.h), in the compilation process. It explains the importance of separating code into interfaces (header files) and implementations (C files), with header files containing function declarations and types but no logic or data manipulation. This foundational knowledge is aimed at building intuition for working with C, especially when including libraries and handling the compilation process.

  • 00:05:00 - 00:10:00

    The video explains the practical setup of a simple C project using header files and source files. It introduces the concept of an interface through header files that declare structures and functions without implementing them. Using a math library example, general programming advice like sketching out the interface on paper before coding is provided. The aim is to establish a structured approach by detailing how a C program should separate main functionality from library functionality.

  • 00:10:00 - 00:15:00

    The tutorial covers creating and including custom header files, emphasizing on maintaining consistency in function signatures between declarations in header files and definitions in source files. It introduces the use of the C preprocessor to guard against multiple inclusions of the same header file, which could cause compilation issues. The process involves defining unique symbols using #ifndef, #define, and #endif directives to manage conditional compilation effectively.

  • 00:15:00 - 00:20:00

    Implementation details are discussed, such as defining functions matching their declarations and handling pointers correctly in C. The lecture corrects common mistakes, like local scope issues when returning results from functions, and stresses using const for pointer parameters meant for input only. The use of the preprocessor for preventing redefinition of included content is reinforced, helping maintain clean and efficient code management.

  • 00:20:00 - 00:26:38

    The lesson concludes by tying together the overall process from writing and organizing C code to the compilation stage, ensuring understanding of how header files are integrated in the build process. By separating interfaces from implementations, programmers can create modular, maintainable codebases that are easier to manage and collaborate on. This concept is vital for progressing in C programming beyond the basics, especially in larger projects.

Mehr anzeigen

Mind Map

Video-Fragen und Antworten

  • What is the purpose of a header file in C?

    Header files serve as an interface in C programming. They typically contain declarations of functions and data types, such as structures and enumerations, which are to be shared between multiple source files.

  • How do you prevent multiple inclusions of a header file?

    To prevent multiple inclusions of a header file, C developers use "header guards." This involves defining a unique symbol within the file using preprocessor directives like #ifndef, #define, and #endif around the file's contents.

  • What is the role of the preprocessor in C?

    The preprocessor in C handles directives that begin with a hash (#). It processes these directives before actual compilation begins, including copying the contents of included files and conditionally compiling code sections.

  • What is the difference between a .c file and a .h file?

    A .c file contains the actual source code implementation, including function definitions and logic. In contrast, a .h file is used for declarations only, serving as a reference for other source files to understand what functions and types are available.

  • How do you compile multiple C files together?

    To compile multiple C files together, you use the GCC (GNU Compiler Collection) command by listing all .c files as input. The compiler will then compile each file and link them together to create an executable.

Weitere Video-Zusammenfassungen anzeigen

Erhalten Sie sofortigen Zugang zu kostenlosen YouTube-Videozusammenfassungen, die von AI unterstรผtzt werden!
Untertitel
en
Automatisches Blรคttern:
  • 00:00:02
    hey what's going on folks it's mike here
  • 00:00:04
    and welcome to your second lesson in c
  • 00:00:07
    so in this lesson what i'm going to be
  • 00:00:09
    talking about is how to deal with c
  • 00:00:12
    files source files header files and how
  • 00:00:15
    all of these pieces fit together
  • 00:00:17
    so this might shed some light as well on
  • 00:00:19
    what's going on in the compilation
  • 00:00:21
    process as well as what just happens
  • 00:00:23
    when we're doing all of these include
  • 00:00:25
    and bringing in these mysterious
  • 00:00:26
    libraries so i hope this will give you a
  • 00:00:28
    little bit more intuition as to what's
  • 00:00:30
    going on in c and we're going to build a
  • 00:00:32
    little library of code and you can see
  • 00:00:34
    the advantage of why we separate out
  • 00:00:37
    files into the interfaces or the header
  • 00:00:39
    files and then the implementation files
  • 00:00:42
    in the dot c files so with that said
  • 00:00:44
    let's go ahead and dive in
  • 00:00:46
    so with this lesson i'm going to talk
  • 00:00:48
    about two main things here
  • 00:00:51
    and the idea is we have these dot c
  • 00:00:54
    files which have some source code and
  • 00:00:58
    typically we have an associated.h file
  • 00:01:00
    which is known as a header file
  • 00:01:03
    and you can think of a header file sort
  • 00:01:05
    of like in index it's going to tell you
  • 00:01:09
    what the interface is
  • 00:01:12
    and
  • 00:01:13
    sort of give you an idea of what
  • 00:01:15
    contents are available so again this
  • 00:01:18
    idea of interface or index or place to
  • 00:01:21
    look up so typically we do
  • 00:01:24
    not
  • 00:01:26
    have implementations
  • 00:01:29
    in our header files
  • 00:01:32
    entrance in our
  • 00:01:35
    header
  • 00:01:37
    files
  • 00:01:39
    and what this means is you're not going
  • 00:01:41
    to see things like loops and
  • 00:01:44
    conditional statements and all these
  • 00:01:46
    things here your interface rather is
  • 00:01:48
    going to just define what functions are
  • 00:01:50
    available maybe some structures or user
  • 00:01:53
    defined types
  • 00:01:55
    that's it for the header files so i
  • 00:01:58
    think it'll be easy if we look at an
  • 00:02:00
    example here
  • 00:02:01
    so again this is the first thing that
  • 00:02:02
    we're going to talk about this sort of
  • 00:02:04
    difference and then the second insight
  • 00:02:06
    we will get is compilation
  • 00:02:08
    which i'll highlight
  • 00:02:10
    below here as we proceed forward so what
  • 00:02:13
    i want to go ahead and do is just sketch
  • 00:02:15
    out our program here
  • 00:02:17
    and how i'm going to do this is i'm
  • 00:02:19
    going to have a main dot c file here
  • 00:02:24
    and in this main.c file i'm going to
  • 00:02:26
    have a few different functions that i
  • 00:02:28
    want to call and they're going to be
  • 00:02:29
    from a math library that we create
  • 00:02:32
    so i'm going to separate out that code
  • 00:02:35
    and have in
  • 00:02:37
    associated
  • 00:02:38
    mic
  • 00:02:40
    math
  • 00:02:42
    dot c and again this is going to be my
  • 00:02:44
    code here
  • 00:02:46
    now before i even create this file here
  • 00:02:49
    i'm going to sketch out some of the
  • 00:02:50
    details in this header file here so
  • 00:02:53
    again this is our interface
  • 00:02:56
    and by convention it'll usually have the
  • 00:02:58
    same name so mic math
  • 00:03:00
    but a different suffix here not c
  • 00:03:03
    but actually dot h for header file here
  • 00:03:07
    and that's going to define what some of
  • 00:03:09
    the functions that we have for example
  • 00:03:11
    so with that said let's go ahead and
  • 00:03:13
    start sketching this out here
  • 00:03:15
    so i'm going to go ahead and split my
  • 00:03:16
    window here just so that we can see
  • 00:03:19
    things in different views
  • 00:03:21
    and in this bottom window is where i'm
  • 00:03:23
    going to be doing my compilation okay so
  • 00:03:25
    let's go ahead and create a file here
  • 00:03:28
    and all of our programs again remember
  • 00:03:30
    start with a main here so i'll make this
  • 00:03:33
    a little bit bigger while everything can
  • 00:03:35
    fit in the screen here
  • 00:03:36
    and we'll have our main function and we
  • 00:03:39
    return a value here that's an integer
  • 00:03:42
    from our main
  • 00:03:43
    okay and let's go ahead and include the
  • 00:03:46
    standard input output.h file because
  • 00:03:48
    we're going to want to write some values
  • 00:03:50
    out
  • 00:03:51
    now what kind of math library do we want
  • 00:03:53
    to have well something that we might
  • 00:03:54
    want to have is working with vectors for
  • 00:03:58
    instance in math uh just a little crash
  • 00:04:00
    course here because i do a lot of
  • 00:04:02
    graphics work but if you have a
  • 00:04:05
    vector that's something with a magnitude
  • 00:04:08
    and a direction and usually you just
  • 00:04:10
    model that with a dot x and a dot y
  • 00:04:13
    component here okay so we need some sort
  • 00:04:16
    of structure and we're going to be
  • 00:04:18
    passing these vectors around and maybe
  • 00:04:20
    adding them subtracting them and so on
  • 00:04:23
    so this might take you back to your math
  • 00:04:25
    days where maybe you've drawn something
  • 00:04:27
    like this with a little arrow and the
  • 00:04:30
    vector was defined something like this
  • 00:04:32
    okay so let's go ahead and start
  • 00:04:36
    sketching that out then
  • 00:04:37
    and maybe all i'm going to do in this
  • 00:04:40
    main file is sort of the main driver of
  • 00:04:43
    the program where i'm actually doing
  • 00:04:45
    some work maybe adding some vectors and
  • 00:04:47
    returning a result so this program
  • 00:04:49
    itself isn't too interesting quite yet
  • 00:04:51
    it's going to be the entry point into
  • 00:04:54
    our program as always but we're going to
  • 00:04:56
    want to create mic math.c and again a
  • 00:04:59
    better name for this might be vector.c
  • 00:05:02
    or vector.h i'm just naming it mic math
  • 00:05:04
    so it's very clear that this is
  • 00:05:06
    something that we created
  • 00:05:07
    so i'm going to go ahead and open
  • 00:05:09
    another file here
  • 00:05:11
    and let's just go ahead and call it
  • 00:05:13
    mymath.c
  • 00:05:15
    and i'm going to open the associated
  • 00:05:18
    header file here so let me make this
  • 00:05:20
    window a little bit bigger here
  • 00:05:22
    and i'll open mic math dot h here
  • 00:05:25
    so i've got mic math dot h on the left
  • 00:05:27
    side and mic math dot c on the right
  • 00:05:30
    side just so you can keep them clear
  • 00:05:32
    and again i'm going to want to start to
  • 00:05:34
    sketch out what we have here on the
  • 00:05:37
    interface here before i worry about the
  • 00:05:39
    implementation details
  • 00:05:40
    now a key tip that you might think about
  • 00:05:42
    again depending on how far along you are
  • 00:05:44
    in your programming journey is to
  • 00:05:45
    actually sketch this out on paper look
  • 00:05:47
    at other
  • 00:05:49
    application programming interfaces or
  • 00:05:51
    apis to see how they sketch out their me
  • 00:05:53
    libraries and so on or maybe just
  • 00:05:56
    discuss with a colleague the
  • 00:05:57
    functionality that you think you need
  • 00:05:59
    okay so what i'm going to do here in mic
  • 00:06:01
    math is again i'm going to create some
  • 00:06:03
    sort of structure here and i'm going to
  • 00:06:05
    call it vector
  • 00:06:07
    and
  • 00:06:08
    a vector remember for the structure has
  • 00:06:11
    the curly braces and just two fields and
  • 00:06:13
    we can decide on what our sort of
  • 00:06:15
    precision is going to be for now i'm
  • 00:06:17
    just going to work with
  • 00:06:19
    actually floats will be reasonable and i
  • 00:06:21
    can do float x y or so on but typically
  • 00:06:24
    i like to just put these on the same
  • 00:06:27
    or separate these out into different
  • 00:06:28
    lines just for clarity
  • 00:06:30
    so to make things a little bit easier
  • 00:06:32
    i'm going to make this a typedef and
  • 00:06:34
    then we can just use this as vector
  • 00:06:36
    underscore h now various conventions in
  • 00:06:39
    c you'll see the types often lowercase
  • 00:06:42
    so actually let me follow that
  • 00:06:44
    convention here and we have our
  • 00:06:46
    structure here
  • 00:06:47
    so
  • 00:06:48
    with that
  • 00:06:49
    then what we're going to do is well
  • 00:06:51
    think about what functions we want to do
  • 00:06:53
    and let's start with something very
  • 00:06:55
    simple
  • 00:06:56
    let's go ahead and have a function here
  • 00:06:59
    that is going to return our custom type
  • 00:07:02
    which is something new that we've seen
  • 00:07:04
    and then we're just going to add two
  • 00:07:07
    vectors together
  • 00:07:08
    so let's just call this
  • 00:07:10
    add
  • 00:07:11
    and i like to do upper cases here
  • 00:07:14
    our vector
  • 00:07:16
    uh whatever the first vector is so v1
  • 00:07:19
    and our vector
  • 00:07:21
    um
  • 00:07:22
    v2 here
  • 00:07:24
    and again i'm not going to i'm putting
  • 00:07:27
    the curly braces here but in our actual
  • 00:07:29
    header files we don't want to do any
  • 00:07:31
    implementation here so rather than put
  • 00:07:33
    that there
  • 00:07:35
    we're just going to put the semicolon
  • 00:07:37
    and just declare our function so
  • 00:07:40
    function
  • 00:07:41
    declaration
  • 00:07:43
    for
  • 00:07:44
    the ability to add two
  • 00:07:46
    vectors
  • 00:07:47
    and return a new vector as a result okay
  • 00:07:51
    so again there's a few new things if
  • 00:07:52
    you're following this from scratch and
  • 00:07:54
    this is your first introduction to c
  • 00:07:56
    again you can see that we can return
  • 00:07:58
    custom types which is great and we can
  • 00:08:00
    pass in these custom types into our
  • 00:08:02
    functions just like any other data type
  • 00:08:04
    so long as the structure has been
  • 00:08:06
    declared ahead of time now for our
  • 00:08:08
    actual structure we have the actual
  • 00:08:11
    sort of implementation or bones or the
  • 00:08:13
    pieces of what it is it's a
  • 00:08:16
    concept with an x and a y to represent
  • 00:08:19
    position
  • 00:08:20
    okay so with that said what do we do now
  • 00:08:22
    in our mic math.c file well i'm going to
  • 00:08:26
    go ahead here
  • 00:08:27
    and i'm going to include
  • 00:08:30
    mike
  • 00:08:31
    math.h here
  • 00:08:34
    and essentially when i do include that's
  • 00:08:35
    going to copy and paste all of these
  • 00:08:38
    contents into this file here
  • 00:08:40
    and that's really all that happens when
  • 00:08:43
    i do an include there's nothing magic
  • 00:08:45
    some anything that begins with the hash
  • 00:08:47
    mark is taken care of during what's
  • 00:08:49
    called a pre-processing step that means
  • 00:08:52
    before your code is compiled it'll be
  • 00:08:54
    copy and pasted into your code wherever
  • 00:08:57
    you have these pound include or include
  • 00:09:00
    declarations here
  • 00:09:01
    now you might be wondering could this
  • 00:09:03
    cause a problem here what if i include
  • 00:09:05
    the library multiple times or perhaps
  • 00:09:08
    amongst different files and yes that can
  • 00:09:11
    cause a problem because we'll have added
  • 00:09:13
    or had many duplications of this
  • 00:09:15
    function
  • 00:09:16
    and in fact let me go ahead and try to
  • 00:09:19
    compile this now just so you can see the
  • 00:09:21
    different errors that you might get
  • 00:09:23
    so what i'm going to do here on our
  • 00:09:24
    command line
  • 00:09:26
    is using gcc
  • 00:09:28
    is compile our main file
  • 00:09:30
    mic math dot c
  • 00:09:33
    and
  • 00:09:34
    then i'll set the output to some program
  • 00:09:37
    here
  • 00:09:38
    now notice that i'm not compiling the
  • 00:09:40
    dot h file i don't need to the dot h
  • 00:09:42
    file is being copied in here so all the
  • 00:09:44
    source code is in my dot c file that's
  • 00:09:47
    something important to keep in mind here
  • 00:09:49
    so let me go ahead and hit enter here
  • 00:09:51
    and again you're going to see
  • 00:09:53
    conflicting types for add
  • 00:09:56
    note there's a previous declaration of
  • 00:09:58
    add here because i've included it
  • 00:09:59
    multiple times so let me go ahead and
  • 00:10:01
    remove
  • 00:10:02
    all of those just include once and then
  • 00:10:05
    now if i compile the files one at a time
  • 00:10:08
    then we are in good shape here so that's
  • 00:10:10
    how this works now there is a way to
  • 00:10:12
    protect yourself against this and again
  • 00:10:15
    it's going to be using this thing called
  • 00:10:16
    the c preprocessor
  • 00:10:18
    where you'll define some symbol here and
  • 00:10:21
    typically it's the file name mic math
  • 00:10:24
    underscore h it could be any unique name
  • 00:10:26
    that you think of just make sure it's
  • 00:10:28
    something you're not going to use more
  • 00:10:29
    than once
  • 00:10:32
    and we say if not defined
  • 00:10:35
    like
  • 00:10:36
    math h
  • 00:10:38
    then we define this unique symbol
  • 00:10:41
    and
  • 00:10:42
    then we
  • 00:10:44
    put in
  • 00:10:45
    and if here with the preprocessor so
  • 00:10:48
    there you can see all of the file here
  • 00:10:50
    so basically when i include mic math
  • 00:10:52
    here it's going to read this first line
  • 00:10:55
    and say hey if i haven't defined this
  • 00:10:57
    symbol during this preprocessing step go
  • 00:10:59
    ahead and define it
  • 00:11:01
    and then
  • 00:11:02
    add in or copy and paste off this code
  • 00:11:04
    so that will happen the first time
  • 00:11:06
    and then if i try to include this again
  • 00:11:09
    like let's try a couple of times
  • 00:11:11
    it'll simply say hey this mic math
  • 00:11:14
    underscore h has been defined before so
  • 00:11:17
    we don't need to
  • 00:11:19
    proceed further here
  • 00:11:21
    and that's what's known as a sort of
  • 00:11:22
    conditional compilation and again it's
  • 00:11:24
    using the preprocessor okay so that's
  • 00:11:26
    the idea and it lets us include the same
  • 00:11:29
    file multiple times without worrying
  • 00:11:31
    about duplicating these symbols okay so
  • 00:11:34
    let's go ahead and look at our
  • 00:11:35
    implementation here
  • 00:11:36
    so in order to do our implementation
  • 00:11:39
    well we've got the function declaration
  • 00:11:40
    here but i'm going to need to define
  • 00:11:43
    this particular function so we'll need a
  • 00:11:45
    function definition
  • 00:11:48
    and it needs to match the same signature
  • 00:11:50
    that is the same return type the same
  • 00:11:52
    name of the function and the same
  • 00:11:54
    parameters in order for us to implement
  • 00:11:56
    this so i've just called this add that
  • 00:11:59
    takes in a vector
  • 00:12:00
    v1 and vector 2 here
  • 00:12:04
    and all this is going to do
  • 00:12:06
    is
  • 00:12:08
    return
  • 00:12:09
    a new vector
  • 00:12:11
    that has added v1s components here so i
  • 00:12:14
    can create the vector here i'll call
  • 00:12:16
    result and the result dot x so we're
  • 00:12:19
    accessing the field here
  • 00:12:22
    equals v ones dot x plus v twos dot x
  • 00:12:26
    and i'll repeat this code for the y
  • 00:12:29
    components here
  • 00:12:32
    and making sure to replace all these
  • 00:12:35
    okay and then we need to return
  • 00:12:36
    something and we are returning the
  • 00:12:39
    result here
  • 00:12:41
    now before i run this code though we
  • 00:12:43
    have to ask ourselves a little bit of a
  • 00:12:44
    question here
  • 00:12:46
    is there going to be a problem because
  • 00:12:48
    we're going to be creating this vector h
  • 00:12:50
    here result and we've talked previously
  • 00:12:53
    about scope if you've watched the learn
  • 00:12:55
    c in one day the previous lesson here
  • 00:12:57
    where i have talked about the scope of
  • 00:13:00
    this variable being declared here only
  • 00:13:02
    within these
  • 00:13:04
    curly braces is where it will actually
  • 00:13:06
    live or be accessible here
  • 00:13:08
    so if i return this and this is being
  • 00:13:11
    deleted this is actually going to cause
  • 00:13:12
    a problem so we need to think carefully
  • 00:13:15
    about our libraries and how we're
  • 00:13:16
    working with things
  • 00:13:18
    so instead what i'm going to do
  • 00:13:20
    is
  • 00:13:21
    instead of doing the
  • 00:13:23
    return here this is just going to be a
  • 00:13:26
    void function
  • 00:13:28
    so i have to match my function
  • 00:13:30
    definition here
  • 00:13:32
    and what i'm going to need to do is
  • 00:13:34
    decide how i'm going to get the output
  • 00:13:36
    here so what i'm going to do because i
  • 00:13:38
    want to modify this actual vector here
  • 00:13:42
    this is going to be the output vector
  • 00:13:44
    here
  • 00:13:47
    and v2 is going to be the input
  • 00:13:54
    so how this will work is we'll take in
  • 00:13:56
    the input
  • 00:13:58
    and the output
  • 00:14:00
    add them together and save the result in
  • 00:14:02
    the output mutating this value only
  • 00:14:06
    okay so that means i can get rid of the
  • 00:14:08
    result there's no return type and i just
  • 00:14:10
    have to be a little bit careful with how
  • 00:14:13
    i do my um
  • 00:14:14
    addition here
  • 00:14:16
    now again because these are pointers
  • 00:14:18
    coming in this means we're actually
  • 00:14:19
    going to change the values here so out
  • 00:14:23
    and then the arrow
  • 00:14:25
    and the out y so these will look the
  • 00:14:27
    same here so let me just do one at a
  • 00:14:29
    time actually
  • 00:14:31
    do this one
  • 00:14:33
    and this will be the out
  • 00:14:35
    x
  • 00:14:37
    and the n x here
  • 00:14:41
    and then i will
  • 00:14:42
    repeat this here except for using the y
  • 00:14:45
    values
  • 00:14:50
    all right
  • 00:14:52
    and because we don't want to change this
  • 00:14:53
    actual value there's a way you can
  • 00:14:55
    enforce in the c language that this
  • 00:14:58
    value here will not change
  • 00:15:00
    and that is to send it in as const here
  • 00:15:03
    so i'm going to make this just a little
  • 00:15:04
    bit smaller so it fits nicely on the
  • 00:15:06
    screen and you can see everything
  • 00:15:08
    and because we've changed our function
  • 00:15:10
    definition here we should make sure that
  • 00:15:13
    we match
  • 00:15:14
    our signature here now i actually don't
  • 00:15:16
    need to have the different
  • 00:15:20
    names of the parameters here this would
  • 00:15:22
    be enough to satisfy c compiler but it's
  • 00:15:24
    a best practice to make sure that you
  • 00:15:26
    also put the
  • 00:15:27
    uh same names here because i communicate
  • 00:15:29
    some information and i do need to have
  • 00:15:32
    the content here however
  • 00:15:34
    okay so there we are let's go ahead and
  • 00:15:37
    try to compile this and see if we've
  • 00:15:38
    made any mistakes no mistakes so let's
  • 00:15:41
    go ahead and try to use this now in our
  • 00:15:43
    actual main
  • 00:15:45
    so let me go ahead and split this window
  • 00:15:47
    here
  • 00:15:48
    and i'll have our main here
  • 00:15:50
    now if i go ahead and just try to use
  • 00:15:53
    our vector so let's go ahead and just
  • 00:15:55
    create two of these types here
  • 00:15:57
    i'll call this
  • 00:15:58
    the first one
  • 00:16:00
    and the second one
  • 00:16:03
    let's go ahead and try to compile this
  • 00:16:05
    and run this code and we're going to get
  • 00:16:07
    an error here it's going to say unnamed
  • 00:16:09
    type vector underscore h first and
  • 00:16:12
    another one for second tier
  • 00:16:14
    and the reason for this again in our
  • 00:16:17
    main program here looking at lines 1
  • 00:16:19
    through
  • 00:16:20
    9 here on this top file
  • 00:16:23
    here
  • 00:16:24
    it doesn't know about vector so again we
  • 00:16:26
    have to include that library
  • 00:16:29
    and we don't use the
  • 00:16:32
    brackets or the angled
  • 00:16:34
    braces because we have this file here
  • 00:16:38
    if i list the files in our current
  • 00:16:40
    directory
  • 00:16:41
    mic math.h well that's right within our
  • 00:16:44
    directory here so instead we refer to
  • 00:16:46
    that by its
  • 00:16:48
    relative path to where we're compiling
  • 00:16:50
    here so vector
  • 00:16:52
    or excuse me this is called mic math.h
  • 00:16:55
    and that's why we use the quotation
  • 00:16:57
    marks so there is a difference when you
  • 00:16:59
    see the quotation versus the angled the
  • 00:17:01
    angled brackets are looking in a special
  • 00:17:03
    place in a system path or from your c
  • 00:17:07
    compiler that's already defined for
  • 00:17:09
    these standard libraries
  • 00:17:11
    so for your second day and c that's
  • 00:17:13
    enough to know that there are just a
  • 00:17:15
    difference there
  • 00:17:16
    all right so now let me go ahead and try
  • 00:17:18
    to compile here and we'll see that we
  • 00:17:20
    compile and if i try to actually run
  • 00:17:22
    this program
  • 00:17:24
    well it's not really doing anything here
  • 00:17:26
    because well we should do something
  • 00:17:28
    interesting with first and second here
  • 00:17:31
    so let's go ahead and
  • 00:17:33
    see what we can do here and we want to
  • 00:17:35
    use our add function here so i'm going
  • 00:17:37
    to do add
  • 00:17:38
    and our input is going to be first
  • 00:17:42
    and our output
  • 00:17:44
    uh or excuse me the in value will be
  • 00:17:47
    second
  • 00:17:48
    and
  • 00:17:49
    first will be our output so eventually
  • 00:17:52
    we're going to store the value into out
  • 00:17:54
    that's what's being modified second is
  • 00:17:56
    not being modified
  • 00:17:58
    okay
  • 00:17:59
    and we should assign these some values
  • 00:18:02
    sometimes it's nice to
  • 00:18:04
    indent here so let's use something
  • 00:18:06
    reasonable 7.0 and you can put things on
  • 00:18:09
    the same line here like this
  • 00:18:12
    i might do it just so that everything
  • 00:18:14
    fits nicely in our window here
  • 00:18:18
    in fact i'll uh rearrange these windows
  • 00:18:20
    just so everything
  • 00:18:22
    fits nicely but
  • 00:18:24
    generally spacing does matter and it is
  • 00:18:26
    important in c programs so let me go
  • 00:18:29
    ahead and just
  • 00:18:30
    move our
  • 00:18:32
    programmers
  • 00:18:34
    or remove these windows so you can see
  • 00:18:35
    everything so i do like to use good
  • 00:18:38
    indentation and kind of
  • 00:18:40
    keep things in line
  • 00:18:42
    although i will just keep these in line
  • 00:18:44
    here
  • 00:18:45
    okay and then we should do the same
  • 00:18:46
    thing for second
  • 00:18:48
    second
  • 00:18:50
    dot x
  • 00:18:51
    equals oh i don't know one point zero
  • 00:18:54
    and second dot y equals
  • 00:18:58
    1.0
  • 00:19:00
    and you'll often see if you use floats
  • 00:19:02
    and f at the end that's just to indicate
  • 00:19:04
    that there that this is a floating
  • 00:19:07
    point type and not a double type here so
  • 00:19:09
    you'll occasionally see me
  • 00:19:11
    write a number and then a letter after
  • 00:19:13
    to be very explicit let's see what type
  • 00:19:15
    we're using
  • 00:19:16
    okay now if we do the addition here well
  • 00:19:19
    let's see if this is even going to
  • 00:19:21
    compile first
  • 00:19:22
    and foremost so go ahead and make your
  • 00:19:24
    predictions if it will or it won't
  • 00:19:26
    and well if you made the correct
  • 00:19:28
    prediction you'll also notice that
  • 00:19:30
    remember we're expecting a pointer in
  • 00:19:32
    here so we need to pass in an address
  • 00:19:34
    here
  • 00:19:36
    so of the actual
  • 00:19:38
    data type that we want to modify here
  • 00:19:40
    vector h
  • 00:19:42
    these two here
  • 00:19:43
    okay so now we'll in fact compile and we
  • 00:19:46
    should print off the result here
  • 00:19:49
    so the result here which is stored in
  • 00:19:52
    well first is our output so let's go
  • 00:19:55
    ahead and print that out
  • 00:19:59
    and we'll just write first dot x equals
  • 00:20:02
    and this is a floating point value so
  • 00:20:04
    percent f
  • 00:20:05
    first dot y
  • 00:20:07
    equals percent f
  • 00:20:09
    and then we can go ahead and uh put
  • 00:20:12
    these here the values so this might be
  • 00:20:14
    an example of um
  • 00:20:16
    putting things on different spaces to
  • 00:20:18
    just make things a little bit more
  • 00:20:19
    bigger here
  • 00:20:20
    okay so let's go ahead and compile
  • 00:20:22
    and let's run our program here
  • 00:20:26
    and we'll see first x is eight first y
  • 00:20:29
    is six
  • 00:20:30
    uh that looks good to me seven plus one
  • 00:20:32
    is 8 1 plus 5 is 6 and we have our
  • 00:20:36
    function added here all right so again
  • 00:20:38
    if this is truly your second day in c
  • 00:20:40
    there's a lot going on here so let's
  • 00:20:43
    slow down a little bit recap then we're
  • 00:20:45
    going to take it back to the drawing
  • 00:20:46
    board and then we'll look at the source
  • 00:20:47
    code one more time just so you
  • 00:20:49
    understand what's going on
  • 00:20:50
    okay so again just to recap we always
  • 00:20:53
    have a main function in our c programs
  • 00:20:55
    that's where our entry point is that's
  • 00:20:56
    where we know where to start
  • 00:20:58
    typically before our main though we
  • 00:21:00
    include
  • 00:21:01
    code that's been written by other
  • 00:21:02
    programmers these are libraries and they
  • 00:21:04
    end in h files the dot h files give us a
  • 00:21:08
    list of functions or structures like
  • 00:21:11
    what we've created
  • 00:21:13
    in our mic math file here
  • 00:21:17
    which i'll display our structure here
  • 00:21:20
    just so you can see it and those are the
  • 00:21:22
    types of things that go in header files
  • 00:21:24
    and typically we include these again at
  • 00:21:26
    the top of our files and these files
  • 00:21:29
    have something called header guards at
  • 00:21:31
    the top here to ensure that they're only
  • 00:21:33
    included once regardless of how many
  • 00:21:35
    times that you write pound include
  • 00:21:37
    pound include remember is just a copy
  • 00:21:39
    and paste so it's literally taking all
  • 00:21:41
    the text in this file here and copying
  • 00:21:44
    and pasting it into this file again
  • 00:21:46
    that's why we need these header guards
  • 00:21:48
    that define some symbol or rather check
  • 00:21:51
    if this symbol is defined
  • 00:21:53
    if it is not then we define it
  • 00:21:56
    and that's all the work that we have to
  • 00:21:57
    do in that file
  • 00:21:59
    all right and then the difference
  • 00:22:01
    between header files that end in h
  • 00:22:04
    that's my convention and dot c is
  • 00:22:06
    there's no actual implementation or work
  • 00:22:10
    so we can see here in our function
  • 00:22:13
    definition here and i'll make the window
  • 00:22:15
    a little bit bigger
  • 00:22:16
    and if you're using vim and want to see
  • 00:22:18
    how to do this resize plus say 3 or
  • 00:22:21
    something like that we'll do the trick
  • 00:22:23
    here so now we can see all of our code
  • 00:22:25
    at once
  • 00:22:26
    okay so again
  • 00:22:27
    here's the declarations and then the
  • 00:22:30
    actual implementation this is where you
  • 00:22:31
    would have expressions that are being
  • 00:22:33
    evaluated loops conditional statements
  • 00:22:36
    etc and we match the function definition
  • 00:22:39
    with our declaration here meaning the
  • 00:22:42
    return type the name and all the
  • 00:22:44
    parameters have to be the same and
  • 00:22:46
    remember in c we can only name a
  • 00:22:47
    function
  • 00:22:48
    add for example one time with an
  • 00:22:50
    uppercase a d d and so on all right and
  • 00:22:53
    then finally if we want to actually use
  • 00:22:55
    the functions or the data types that we
  • 00:22:57
    define here like vector underscore h
  • 00:23:00
    then we include mic math in the main.c
  • 00:23:03
    file and then we have access to that
  • 00:23:05
    type and then all the functions here
  • 00:23:07
    so again it's kind of nice because
  • 00:23:10
    what we can do is just quickly skim over
  • 00:23:13
    mic math.h see that there's an add
  • 00:23:15
    function this new vector data type and
  • 00:23:18
    know that those are available we say hey
  • 00:23:20
    i need those things so i'll include it
  • 00:23:22
    and then you can start using those data
  • 00:23:23
    types as needed
  • 00:23:25
    now this lesson also provided a little
  • 00:23:27
    bit of a refresher to be careful about
  • 00:23:28
    the data types that you're passing in
  • 00:23:31
    headers and we also have to think about
  • 00:23:34
    when we return from a function for
  • 00:23:36
    instance are we returning something
  • 00:23:38
    that's local in scope and gonna
  • 00:23:40
    disappear
  • 00:23:41
    because in fact that will just sort of
  • 00:23:43
    leave us with no results so we fix that
  • 00:23:45
    by just passing in a pointer
  • 00:23:48
    and then using that as a output
  • 00:23:52
    parameter so this is the actual thing
  • 00:23:54
    that we are modifying this side we are
  • 00:23:56
    not modifying and that sort of goes a
  • 00:23:58
    little bit to different api designs and
  • 00:24:01
    there's pros and cons to that which will
  • 00:24:03
    be perhaps for your third fourth or
  • 00:24:04
    fifth etc day of c all right so let's go
  • 00:24:07
    ahead and take this to the drawing board
  • 00:24:09
    again just to understand what we did
  • 00:24:11
    with this source code here
  • 00:24:13
    so i'm going to make the source code a
  • 00:24:14
    little bit smaller so that it again
  • 00:24:17
    fits reasonably well here
  • 00:24:19
    we took our main.c file which has our
  • 00:24:22
    entry point in the program we have mic
  • 00:24:24
    math.c
  • 00:24:26
    and these two files here
  • 00:24:28
    these are the only ones that get
  • 00:24:29
    compiled so we have g
  • 00:24:32
    cc here
  • 00:24:34
    we are only inputting these two files
  • 00:24:36
    into gcc
  • 00:24:38
    because again this file was essentially
  • 00:24:41
    copy and pasted here
  • 00:24:43
    or it was copy and pasted here one of
  • 00:24:45
    the two we included it both places but
  • 00:24:47
    again because of the header guards it
  • 00:24:49
    only is applied one time
  • 00:24:51
    all right and then gc will take these
  • 00:24:53
    two files
  • 00:24:54
    here
  • 00:24:55
    and it will glue them together do the
  • 00:24:58
    compilation and we get a bunch of ones
  • 00:25:00
    and zeros
  • 00:25:02
    there's actually another step in here
  • 00:25:04
    that we won't talk about quite yet which
  • 00:25:07
    is sort of linking everything in
  • 00:25:08
    together including the standard library
  • 00:25:11
    that we've included but eventually we
  • 00:25:13
    get our
  • 00:25:14
    program here dot slash prog which we can
  • 00:25:17
    then run here
  • 00:25:20
    right so hopefully that gives you some
  • 00:25:21
    intuition about these dot h files that
  • 00:25:23
    we've included
  • 00:25:25
    so somewhere else someone has written
  • 00:25:27
    the standard io.h which has functions
  • 00:25:29
    like printf puts and different functions
  • 00:25:32
    that we've been previously looking at in
  • 00:25:34
    this series and then now you'll know how
  • 00:25:36
    to properly create your own.h files and
  • 00:25:39
    your.c files and how and why some of
  • 00:25:43
    these things are structured and they're
  • 00:25:46
    really structured because it allows you
  • 00:25:48
    to split up your source code so you
  • 00:25:50
    don't have to put all of your code in
  • 00:25:51
    one file which would get
  • 00:25:53
    pretty unreasonable to maintain or hard
  • 00:25:55
    to work with in a team here
  • 00:25:58
    all right folks so i hope you enjoyed
  • 00:26:00
    that lesson and this is your second day
  • 00:26:02
    in c again we got to refresh some things
  • 00:26:04
    like functions building our own data
  • 00:26:06
    types and now splitting up our projects
  • 00:26:08
    into separate files so that you
  • 00:26:10
    understand again what's going on here if
  • 00:26:12
    you found some value in this video and
  • 00:26:14
    it was useful please go ahead and like
  • 00:26:16
    and subscribe so you don't miss the next
  • 00:26:18
    one and be sure to go and check the
  • 00:26:20
    first in the series which i'll link in
  • 00:26:22
    the description below so that you can
  • 00:26:24
    review anything that was maybe a little
  • 00:26:26
    bit uh not as
  • 00:26:28
    fresh in your memory so please use these
  • 00:26:31
    as a resource comment below if you have
  • 00:26:32
    questions and i'll look forward to
  • 00:26:34
    seeing you in the next lesson
Tags
  • C programming
  • header files
  • source files
  • compilation
  • libraries
  • functions
  • structures
  • vectors
  • pointer usage
  • header guards