Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Im getting the error: register name not specified for ‘i’

Tags:

c

#include <stdio.h>
register int i = 10;
int main(void)
{
    printf( " i = %d \n ", i );
    return 0;
}

Here i declared the variable i as register. But, while compiling its showing the following error

" error: register name not specified for ‘i’ "

Why we should not use register keyword in global ?

like image 917
user559208 Avatar asked Dec 22 '22 19:12

user559208


2 Answers

A register variable is a type of local variable.
It is a hint to store the value in a register for faster access.
A register variable can not be global or static.
It can be defined only in a block.
Also please format the code you post

like image 136
Cratylus Avatar answered Dec 24 '22 08:12

Cratylus


A register variable cannot be used as a "global" variable, because file scope variables have static storage, thus by definition they have an address. register variables are exactly the contrary, that are variables for which you, the programmer, promise to never take their address. So combining the two makes not much sense.

BTW, the error message that you get is not very helpful. It seems that your compiler is referring to an extension that allows to fix a register variable to a particular hardware register. If you post such an error message please also give an indication which compiler and/or platform you are using.

like image 45
Jens Gustedt Avatar answered Dec 24 '22 10:12

Jens Gustedt