Many local vs one global variable

Is it better to use many local variables or a global one?
Memory-wise, I think one global should be better, but not sure.
Processing-wise, I think it might be the same, but again I don't know.

Many local example:

switch (c)
{
         case 1:
             int temp1=x;  //local
             .....
         case 2:
             int temp2=y;  //local
             .......
         case 3:
             int temp3=z;  //local
             ......
}

One global example:

int temp;   //global

switch (c)
{
         case 1:
             temp=x;  
             .....
         case 2:
             temp=y;  
             .......
         case 3:
             temp=z;  
             ......
}

Thank you very much for your help.

PS, my code is very tight on memory...

my code is very tight on memory

What code?

From a previous thread I've learned (from the experienced, thank you) that it does not matter, since the compiler will get what you mean and then optimise on variable storage usage.

As a newbie, I quickly learned, that it is much easier to follow the assigned values of my variables, if they are all declared and used as globals troughout your code.
It will also avoid confusions between assigning and storing the values of the variables while the program loops.

As you get experienced, you can use the common best practice programming method of declaring and using local variables within the scope of your functions.
This will make large code structures easier to read.

AWOL:
What code?

There's no point in pasting hundreds of line of code from multiple files. I'm asking about the concept in general.

Generally, local "auto" variables are created on the stack when the function is entered, and discarded when the stack is popped after the function return. Or, they may be put into a register if the compiler thinks that it would be a good idea.

KeithRB:
Generally, local "auto" variables are created on the stack when the function is entered, and discarded when the stack is popped after the function return. Or, they may be put into a register if the compiler thinks that it would be a good idea.

Does that mean that using local variables is more processor intensive, since they have to be created and discarded? While the global ones just have to be written and read, after the allocation?

It depends on the processor. I am not familiar enough with AVR's to say. But let me just say that gcc is very very good, and all this second guessing doesn't buy you very much.

dragospuri:
Is it better to use many local variables or a global one?

...is a very poorly worded question. As worded, the answer is obvious: better is to use what is appropriate based on the scope.

I suspect that you meant to ask about storage duration: static versus automatic. The subsequent answers are based on that assumption.

Memory-wise, I think one global should be better, but not sure.

I am. Static ("global") requires storage. Automatic ("local") may or may not. In terms of reducing SRAM consumption automatics are a better choice. However, following that path amounts to tweaking at the fringes; is very likely a waste of time.

Processing-wise, I think it might be the same, but again I don't know.

It is not. Automatics nearly always use less CPU time for the simple reason they are first placed in registers. But, like swapping many automatics for a few statics, it is a path that amounts to tweaking at the fringes.

dragospuri:
Does that mean that using local variables is more processor intensive, since they have to be created and discarded? While the global ones just have to be written and read, after the allocation?

The overhead on creating and discarding a local variable is insignificant. It's just a matter of incrementing and decrementing the stack pointer on most CPU's, because the calling stack frame is where they are allocated.

aarg:
The overhead on creating and discarding a local variable is insignificant. It's just a matter of incrementing and decrementing the stack pointer on most CPU's, because the calling stack frame is where they are allocated.

And the stack frame is created by doing a single subtract to allocate enough space for ALL automatic variables required by the current functions. The impact of adding another automatic variable is zero, unless it is initialized.

Even so, worrying about optimization on that level is almost guaranteed to be a complete and total waste of time. The overall architecture of the code will have orders of magnitude more impact on performance and resource utilization than that kind if microscopic, really pico-scopic, optimization.

Regards,
Ray L.