Global variables in a multi-file project in C

  Рет қаралды 15,991

CodeVault

CodeVault

2 жыл бұрын

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 жыл бұрын
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 жыл бұрын
Awesome job describing header file usage and importance. Additionally, good to see you're back and making more content for 2022 - good stuff.
@hossamnasser9717
@hossamnasser9717 Жыл бұрын
Your simple way of explanation is unique. Your videos make me love coding. Thanks, man
@boryswwa
@boryswwa 11 ай бұрын
Thanks for this explanation. Simple and straightforward. Exactly what I needed!
@leythecg
@leythecg 2 жыл бұрын
Very clearly explained! Many, many thanks for that!
@reshmaimmaculater.4868
@reshmaimmaculater.4868 2 жыл бұрын
Thank you so much for this video. I was looking for more clarity on this topic.
@roopibhai1556
@roopibhai1556 Жыл бұрын
thank you man for explaining global and external in simple way with actual demo.
@romakekua8249
@romakekua8249 2 жыл бұрын
Hello from Georgia
@user-sf2lr4vn5z
@user-sf2lr4vn5z 3 ай бұрын
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
@neetorstar450
@neetorstar450 2 жыл бұрын
Life saver. I was cracking my brains on that
@chenmoney1920
@chenmoney1920 2 жыл бұрын
Nice explaination
@shinsban2374
@shinsban2374 Жыл бұрын
thanks you!
@fusca14tube
@fusca14tube 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@CodeVault Thanks for the reply! :)
@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.
@video0607
@video0607 Жыл бұрын
What if don't create the file "global.c" and declare global variables directly in the header file "global.h"?
@CodeVault
@CodeVault Жыл бұрын
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
@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 Жыл бұрын
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;
@alvarogaliana3271
@alvarogaliana3271 8 ай бұрын
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 8 ай бұрын
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
@KFlorent13
@KFlorent13 2 жыл бұрын
You were back and I missed it. I have failed you...
@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
@tekshanmadhawa4632
@tekshanmadhawa4632 10 ай бұрын
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 10 ай бұрын
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
Sharing functions between files in C
9:00
CodeVault
Рет қаралды 28 М.
Global Variables in C++... not as easy as it seems
18:25
The Cherno
Рет қаралды 60 М.
УГАДАЙ ГДЕ ПРАВИЛЬНЫЙ ЦВЕТ?😱
00:14
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
Зачем он туда залез?
00:25
Vlad Samokatchik
Рет қаралды 3 МЛН
Does size matter? BEACH EDITION
00:32
Mini Katana
Рет қаралды 20 МЛН
Cat Corn?! 🙀 #cat #cute #catlover
00:54
Stocat
Рет қаралды 15 МЛН
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 379 М.
Condition variables in C
18:57
CodeVault
Рет қаралды 92 М.
Declaration vs. Definition of a variable in C
10:37
CodeVault
Рет қаралды 7 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 338 М.
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 74 М.
Header files and libraries (Kevin Lynch)
9:32
Northwestern Robotics
Рет қаралды 94 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 388 М.
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 76 М.
Producer - Consumer Problem in Multi-Threading
25:18
CodeVault
Рет қаралды 110 М.
УГАДАЙ ГДЕ ПРАВИЛЬНЫЙ ЦВЕТ?😱
00:14
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН