global or local variables in project organization [SOLVED]

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
If a variable is needed throughout the program then only I use the global variable otherwise I use the local variable inside function. I have shown two programs in which I am using local and global variables. When both programs run they give the same output.

global variable
C:
#include<stdio.h>

int count; // global variable

void foo (void)
{
  count = 0;  
  count++;
  printf("%d \n", count);
}

int main ()
{
    foo ();
  return 0;

}
local variable
C:
#include<stdio.h>

void foo (void)
{
  int count; // local variable  
  count = 0;  
  count++;
  printf("%d \n", count);
}

int main ()
{
    foo ();
  return 0;

}
I think it depends on memory resources to use global and local variables. MCU has limited memory as compare to PC. So if we are writing a complex program on PC and we declare all the variables as global variables then maybe it doesn't matter much. But if we are writing a complex program for MCU and we have declared all the variables as global variables so maybe we can have problem because MCU has limited memory.

I'm curious to know when you are writing a very large program with lots of functions, variables. How do you decide whether you will use a local variable or whether you will use a global variable to use limited memory.
It will not be right to make all the variables as global variables and it will not be right to make all the variables as local variables. There should be a good reason to do it
 

MrChips

Joined Oct 2, 2009
24,642
The good reason is not about memory capacity. It is about organization.

Do you put all your files in one folder?
Do you put all your clothes in one big basket?
 

click_here

Joined Sep 22, 2020
446
Using global variables is usually considered a bad practise and a sign of an inexperienced programmer.

You would usually minimise the amount of code that can modify a variable for very good reasons.

In your trivial examples it doesn't really matter, but as your code gets larger it becomes very important to have as much encapsulation as possible.

One day you might find that there is a bug and a global variable has the wrong value in it - This is a big problem because it could have come from anywhere.

You might have written multiple header files and add two you've created from a previous projects... only to find that the libraries you just added has a naming clash. All of the sudden you have a lot of work to do! You might even be stuck here, because changing either library might break the other projects.

You might start using multiple threads down the track and have two functions changing the global variable at the same time. This is a really hard bug to track down!

Well written code is modular - i.e. Changing one section has no effect on another section. When you use lots of global variables, you create dependency between different sections of your code. This is a lesson that I had learnt after years of pain: you will usually have to add a feature later on. Eliminating dependency is the trick that experienced programmers use to make their program robust.

There are some places where it is considered acceptable - Two that I can think of are register names on a microcontroller and a variable that contains error flags. I'm sure that other people can think of more...


Hope this helps :)
 

Yaakov

Joined Jan 27, 2019
3,760
As a trivial example but one that makes a point:

Let’s say you are using for loops in several different functions and the variable i to store the iterator.

Completely without regard to memory usage, why wouldn’t you make i a global?
 

click_here

Joined Sep 22, 2020
446
As a trivial example but one that makes a point:

Let’s say you are using for loops in several different functions and the variable i to store the iterator.

Completely without regard to memory usage, why wouldn’t you make i a global?
To me that would seem risky because 'i' is used everywhere - You are likely to get a name clash in an imported library.
 

Yaakov

Joined Jan 27, 2019
3,760
To me that would seem risky because 'i' is used everywhere - You are likely to get a name clash in an imported library.
Yes, as well as not knowing the state of the variable if your function is reentrant. The idea is, making it global means everyone can change something that is really only valid in the context of your function.

It's just a way to think about scope, which is the subject here. In what context–scope–can you expect the variable to be respected? For things like loop iterators, it's pretty clear you don't want something outside your function changing that value.
 

click_here

Joined Sep 22, 2020
446
Yes, as well as not knowing the state of the variable if your function is reentrant. The idea is, making it global means everyone can change something that is really only valid in the context of your function.

It's just a way to think about scope, which is the subject here. In what context–scope–can you expect the variable to be respected? For things like loop iterators, it's pretty clear you don't want something outside your function changing that value.
Sorry, I didn't read your tone - I thought that you were actually suggesting that as a good idea!
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
May be I was overthinking about local & global variable in context of memory. I must stick to what I have shown in two program,
 

MrChips

Joined Oct 2, 2009
24,642
In terms of project organization, all variables should be local.
Make the variable global only if you have a strong reason for doing so.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
In terms of project organization, all variables should be local.
Make the variable global only if you have a strong reason for doing so.
Actually the reason for my confusion is that why all variables should be local. Can you @MrChips explain reason behind it. why should it be done and what are its benefits?
 

Yaakov

Joined Jan 27, 2019
3,760
Actually the reason for my confusion is that why all variables should be local. Can you @MrChips explain reason behind it. why should it be done and what are its benefits?
Global variables are available to all functions. Sometimes you need to have a place to put things all of your code can see.
 

MrChips

Joined Oct 2, 2009
24,642
Here are some reasons:

1) Portability - You can move functions from one project to another without needing to reference a variable outside of the function.

2) Security and consistency - A local variable is accessed from within a function. External functions cannot alter the variable.
 
Top