My question, Consider CPU has 4 register and in my C code i have declared 10 register variable
How it handled by compiler ? and how it handled during process execution ?
My question, Consider CPU has 4 register and in my C code i have declared 10 register variable
How it handled by compiler ? and how it handled during process execution ?
You can use as many register variables as you want. The only guaranteed effect is that it stops you from taking the address.
This makes it possible for a compiler to keep it in a register even with optimization disabled, but doesn't require it.
With optimization enabled, the register keyword is fully useless. That's why C++ deprecated and then removed it. Ancient compilers needed the help; compilers for the past 2 decades at least have been able to figure out when they can keep variables in registers on their own. (Doing escape analysis and alias analysis on a whole function before emitting any asm for any of its statements.)
Except for TCC, the Tiny C Compiler, which compiles on the fly in one pass. Perhaps that would benefit from the register keyword, since it doesn't look ahead in the function or do any analysis. Or for simplicity it might just ignore register, other than erroring if you try to take the address.
There's a GNU C extension that also uses the register keyword, like register int foo asm("r2");. For local vars, that forces the compiler's choice of register when you use it as an operand in asm("..." : "=r"(foo)). And with GCC at least (but not clang), the compiler does tend to keep that variable in that register even outside of asm statements. But that's no longer a documented (supported) guarantee; GCC removed that part of the docs a few years ago.
For global vars, an explicit-register global var will tie up that register for the whole program. This would be a super bad idea on a machine with only 4 registers, using up 1/4 of the total on one variable, even in code that doesn't touch it.
This GNU C extension makes you specify which register to use, so obviously you are limited to the number of registers in the target you're compiling for. Unless you use the same register for multiple variables, which I was surprised to find isn't an error for local vars, unless you actually try to use them both with asm statements. (https://godbolt.org/z/K463bcf8c). It is an error for global vars.