Global variables in a multi-file project in C

Поделиться
HTML-код
  • Опубликовано: 17 янв 2022
  • Source code can be found here:
    code-vault.net/lesson/fjri9hc...
    ===== Support us through our store =====
    code-vault.net/shop
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-vault.net

Комментарии • 29

  • @Yankee14ed
    @Yankee14ed 2 года назад +7

    It is funny how I feel like I have taken seemingly simple things like this for granted. Even after using C off and on for many years, I still learn some new subtle things about the language, especially from watching your videos. Thank you for this.

  • @NOPerative
    @NOPerative 2 года назад +4

    Awesome job describing header file usage and importance.
    Additionally, good to see you're back and making more content for 2022 - good stuff.

  • @boryswwa
    @boryswwa 9 месяцев назад

    Thanks for this explanation. Simple and straightforward. Exactly what I needed!

  • @hossamnasser9717
    @hossamnasser9717 Год назад

    Your simple way of explanation is unique. Your videos make me love coding. Thanks, man

  • @reshmaimmaculater.4868
    @reshmaimmaculater.4868 2 года назад

    Thank you so much for this video. I was looking for more clarity on this topic.

  • @leythecg
    @leythecg 2 года назад +2

    Very clearly explained! Many, many thanks for that!

  • @roopibhai1556
    @roopibhai1556 Год назад

    thank you man for explaining global and external in simple way with actual demo.

  • @romakekua8249
    @romakekua8249 2 года назад

    Hello from Georgia

  • @neetorstar450
    @neetorstar450 2 года назад

    Life saver. I was cracking my brains on that

  • @user-sf2lr4vn5z
    @user-sf2lr4vn5z 29 дней назад

    It's just simple and important things that I've learned from this video

  • @nishant1877
    @nishant1877 2 года назад

    Thank you for this video..This video is a necessity for anyone who works in a real team environment and do real coding not interview coding

  • @chenmoney1920
    @chenmoney1920 2 года назад

    Nice explaination

  • @shinsban2374
    @shinsban2374 Год назад

    thanks you!

  • @fusca14tube
    @fusca14tube 2 года назад +4

    Hi... Thanks for this video. I've one question: if you are including the "global.h" file in multiple .c files, is there a need to protect the extern declaration? I mean, do you need to use #ifndef in this "global.h" file?

    • @CodeVault
      @CodeVault  2 года назад +7

      No. extern int x; is just a declaration of the variable x and you can declare the same variables as many times as you want. It might improve compile time if you use that "#ifndef" or "#pragma once". I'll research some more on this topic and might make a video on it

    • @fusca14tube
      @fusca14tube 2 года назад +3

      @@CodeVault Thanks for the reply! :)

  • @KFlorent13
    @KFlorent13 2 года назад

    You were back and I missed it. I have failed you...

  • @alvarogaliana3271
    @alvarogaliana3271 6 месяцев назад

    Rather than modifying the x, I'm trying to retrieve its value in the main2.c function. But apparently this doesn't do it, when I print x in main2.c is set back to 0. Is there any way to do this? I thought global variables would do the trick. Edit: I must use two distinct executable files for this, that's kind of required for my project.

    • @CodeVault
      @CodeVault  6 месяцев назад +1

      With two distinct executables you will have to have some mechanism of communication between the two. Global variables are per process (they never get shared between processes). Either use a fifo or a simple file to communicate between processes

  • @video0607
    @video0607 Год назад +1

    What if don't create the file "global.c" and declare global variables directly in the header file "global.h"?

    • @CodeVault
      @CodeVault  Год назад +3

      Basically the definition of all of those symbols might appear multiple times in your project. For variables I'm pretty sure it's fine (but not recommended). The compiler sort of, cleans up the duplicated global variables. But if you do this with the functions you'll have a problem if you include that global.h file in multiple .c files from your project. This video talks about this topic: code-vault.net/lesson/ys1aqurr3x:1642707892012

  • @raghavimadhwa2336
    @raghavimadhwa2336 Год назад

    Hey, I have one doubt. If I want to share a global variable across two C files which are in different directories, then how to do that?

    • @CodeVault
      @CodeVault  Год назад

      Same as you would do if they were in the same folder. In the gcc compile line add the full path to them and in the include you can specify a relative path to the current folder.

  • @saikishorekemburu3454
    @saikishorekemburu3454 Год назад

    Hey I have a doubt, If I want to access a global variable and change the value of that variable in another file.how can I?

    • @CodeVault
      @CodeVault  Год назад +1

      You can do that just like with any other variable. Simply assign a different value to it in a function in that other file like so:
      variable = 15;

  • @tekshanmadhawa4632
    @tekshanmadhawa4632 8 месяцев назад

    Hey,
    good explanation but I have one doubt ? when you rrun both files in same terminal in VS code which file's integrated terminal was used ? I am confused actually .

    • @CodeVault
      @CodeVault  8 месяцев назад

      There's a big misconception here. I'm not "running files" as in, I am not running a specific source code. The gcc command compiles all the source codes specified (main.c, main2.c, main3.c) into a single executable called "main". I am executing this "main" file that includes the code of all the other .c files

  • @markm4603
    @markm4603 2 года назад

    Why is c so simple and python I just struggle to understand

    • @CodeVault
      @CodeVault  2 года назад

      I'm sure you can get to understand Python as well! It just has some nifty tricks that you can do in one line which would be translated to many many lines of code in C. Once you understand those it's not too difficult I think