How to use global variables in C to use the same variable in different functions -


I'm new to C, and I'm trying to explain how to use global variables. If I mainly define one then the variables come in undefined form in other functions, but if I define it outside of all the functions, all variables that I want to global are undefined. Any tips about how to use them correctly?

Stdio.h & gt; Int foo; four times; Zero changeIN (int neww); Int main (int argc, char ** argv) {foo = 0; ChangeInt (5); Printf ("foo is now% d \ n", foo); Return 0; } Zero changeInt (int newValue) {foo = newValue; }

On the one hand, it is not the best practice to use globals, especially in multi-threaded accessories. This is absolutely fine in some applications, but there is always a right way. To be more appropriate, you can declare your variables that you require in the main, then give the function, in which a pointer is modified.

Meaning

  zero changeInt (int * ToChange, int newValue); Int main (int argc, char ** argv) {int foo = 0; ChangeInt (& amp; foo, 5); Printf ("foo is now% d \ n", foo); Return 0; } Zero changeInt (int * toChange, int newValue) {* toChange = newValue; // Notify the pointer to modify the value}  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -