Skip to content
Bilge Akpulat edited this page Feb 4, 2025 · 1 revision

Library Elements

  1. #include <stdio.h> → inside here lies printf function
  2. “” → using a non standart library that is manually created
  3. <> → used in standart libraries (unistd.h or stdio.h)
  4. .h file → header file
  5. #include “library.h” → include the header file
  6. we can have a library inside a library.
  7. gcc -o app main.c library.c → this line will give us an executable file called app.
  8. ./app → runs app
  9. gcc -o app library.c main.c → works the same way.

Practice Code

main.c

#include <stdio.h>
#include "library.h"
int main(void)
{
    printf("%d + %d = %d\n", 4, 5, add(4,5));
    printf("%d - %d = %d\n", 10, 7, sub(10,7));
    return  (0);
}

library.h

int add(int x, int  y);
int sub(int x, int  y);

library.c

{
    return  (x + y);
}

int sub(int a, int  b)
{
    return  (a - b);
}

Source: https://www.youtube.com/watch?v=x8gsHFBW7zY

Header Files

1. Include guard
An include guard prevents a header file from being included multiple times in a single compilation unit, which can lead to errors.
Example:

#ifndef MYLIBRARY_H
#define MYLIBRARY_H
// Header file content
#endif // MYLIBRARY_H

Clone this wiki locally