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.
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?
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
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
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.
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
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.
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 .
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
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
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.
Awesome job describing header file usage and importance.
Additionally, good to see you're back and making more content for 2022 - good stuff.
Your simple way of explanation is unique. Your videos make me love coding. Thanks, man
thank you man for explaining global and external in simple way with actual demo.
Thanks for this explanation. Simple and straightforward. Exactly what I needed!
Very clearly explained! Many, many thanks for that!
Hello from Georgia
Thank you so much for this video. I was looking for more clarity on this topic.
It's just simple and important things that I've learned from this video
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
Life saver. I was cracking my brains on that
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?
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
@@CodeVault Thanks for the reply! :)
What if don't create the file "global.c" and declare global variables directly in the header file "global.h"?
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
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.
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
thanks you!
Nice explaination
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?
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;
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?
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.
You were back and I missed it. I have failed you...
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 .
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
Why is c so simple and python I just struggle to understand
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