Global variables in a multi-file project in C

  Рет қаралды 17,148

CodeVault

CodeVault

Күн бұрын

Пікірлер: 29
@ed.peguillan.3
@ed.peguillan.3 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.
@leythecg
@leythecg 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.
@boryswwa
@boryswwa Жыл бұрын
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
@OmarIghirouaiour-c2p
@OmarIghirouaiour-c2p 7 ай бұрын
It's just simple and important things that I've learned from this video
@romakekua8249
@romakekua8249 2 жыл бұрын
Hello from Georgia
@reshmaimmaculater.4868
@reshmaimmaculater.4868 2 жыл бұрын
Thank you so much for this video. I was looking for more clarity on this topic.
@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
@shinsban2374
@shinsban2374 2 жыл бұрын
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! :)
@chenmoney1920
@chenmoney1920 2 жыл бұрын
Nice explaination
@vladimir_khlghatyan
@vladimir_khlghatyan 2 жыл бұрын
What if don't create the file "global.c" and declare global variables directly in the header file "global.h"?
@CodeVault
@CodeVault 2 жыл бұрын
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
@KFlorent13
@KFlorent13 2 жыл бұрын
You were back and I missed it. I have failed you...
@alvarogaliana3271
@alvarogaliana3271 Жыл бұрын
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 Жыл бұрын
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
@raghavimadhwa2336
@raghavimadhwa2336 2 жыл бұрын
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 2 жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
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
Sharing functions between files in C
9:00
CodeVault
Рет қаралды 30 М.
Global functions in multi-file projects in C
10:39
CodeVault
Рет қаралды 8 М.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 2,2 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 5 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
why do header files even exist?
10:53
Low Level
Рет қаралды 431 М.
Declaration vs. Definition of a variable in C
10:37
CodeVault
Рет қаралды 8 М.
Global Variables in C++... not as easy as it seems
18:25
The Cherno
Рет қаралды 65 М.
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 76 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
Difference between arrays and pointers in C
11:23
CodeVault
Рет қаралды 32 М.
Header files and libraries (Kevin Lynch)
9:32
Northwestern Robotics
Рет қаралды 97 М.