3
static uint32_t a[20] = {0};

and

uint32_t a[20] = {0};

I use both of them in the code

a[0] = 1;

and so on....

When I make the variable static and use it I get an error

variable "a" was declared but never referenced

but when I remove static things work fine.

In both the cases the array a is a global one.

The error is with the MACROS .

Array declaration is done and it is used by some platform and I don't see an error on that. Same code provides an error because this declaration/array is not used on other platform.

My bad !!!!

static uint32_t a[20] = {0};
void func()
{
 ...............
   #ifdef ABC

   a[0] = 1;

   #endif
 ................
}

Now compile on platform ABC no error compile on some non ABC platform there is an error.

Solution: Wrap global also under the respective macro

#ifdef ABC
static uint32_t a[20] = {0};
#endif
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • `static` means "this variable is not _directly_ accessible outside the unit/file", but I don't understand why you are receiving such error – David Ranieri May 04 '16 at 09:21
  • If you get an error that a variable was declared but never references, odds are you declared it but never referenced it. (Perhaps you referenced a different variable with the same name.) – David Schwartz May 04 '16 at 09:22
  • @DavidSchwartz Yeah I am wondering what is wrong. I am using it the same array in my code. – Gopi May 04 '16 at 09:23
  • I have compiled the code with `static uint32_t a[20] = {0};` using either gcc or llvm, got neither errors nor warnings. – user3078414 May 04 '16 at 09:23
  • @Gopi It told you what is wrong, you are declaring a variable but not referencing it. As I said, you might be referencing a different variable with the same name. Try removing all the declarations that it tells you are not referenced -- if your code still compiles and links, it was right. – David Schwartz May 04 '16 at 09:24
  • "When I make the variable static and use it I get an error" How and where did you try to use the variable? – Lundin May 04 '16 at 09:26
  • @Lundin In the same `.c` file . In one of the functions. – Gopi May 04 '16 at 09:27
  • So you tried to use a static variable in the same .c file in which it was declared and you got an error? Can you post a minimal, complete, compilable piece of code that reproduces the error or warning so we can see it for ourselves? – David Schwartz May 04 '16 at 09:28
  • Then there is some other error in your code not related to the use of `static`. – Lundin May 04 '16 at 09:28
  • _things work fine._ it's apparent. For internal linkage, compiler can issue the diagnostic... – Sourav Ghosh May 04 '16 at 09:30
  • @Lundin Has it got anything to do with the compiler ? I mean is there any obvious bug. I am sure that I am using the variable in my function – Gopi May 04 '16 at 09:34
  • Are you using this declaration inside an header file? – K.H.A.J.A.S May 04 '16 at 09:35
  • @Gopi There's no way we can know if we can't see the code. Sometimes the error message can be misleading. As an extremely silly example, imagine if somewhere in your code you had something like `#define static static X`, you'd get very strange error messages when you declared something `static`. Give us enough code to reproduce the error and we'll tell you what the problem is. – David Schwartz May 04 '16 at 09:35
  • @Gopi It has almost certainly something to do with a bug in the code which you have not shown us. – Lundin May 04 '16 at 09:37
  • @Lundin Uff!!!! I found out what was the error. The code which was using this was under some macro which the declaration was not... Thanks all. – Gopi May 04 '16 at 09:46
  • @DavidSchwartz Uff!!!! I found out what was the error. The code which was using this was under some macro which the declaration was not... Thanks all. – Gopi May 04 '16 at 09:47
  • Ok I'll vote to close as "cannot be reproduced" then. It would perhaps be interesting for future readers if you could post the problematic code that caused the issue. – Lundin May 04 '16 at 09:50

2 Answers2

5

The major difference is, when defined as static, the scope of the array is limited to the translation unit, while , without static, the scope in not limited to the translation unit.

Quoting C11, chapter §6.2.2

If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.

So, in case of a static global, you cannot use that variable outside of the translation unit.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The keyword static

Case 1 : When used in file scope

Example :

static int x=0; // declared outside main() and all functions

means the the variable can be used only within the translation unit, ie the file which contains it.

So you cannot do

extern int x; // from another file

Case 2 : When used in block scope

Example

somefunction()
{
static int x=0;
x++; // x acting as a counter here
}

The variable x stays put(or it is not reinitialized) during different invocations of the function. You can use it as a function variable, for example, as a counter to find how many times a function is called. The scope is limited to the function block.


Regarding the warning :

variable "a" was declared but never referenced

The memory allocated for an automatic variable is freed when it goes out of context. But this is not the case with static variables. They stay put till the end of the execution. If you don't use a static variable, the compiler may warn you regarding this - I guess - so that you could avoid such a declaration.

sjsam
  • 21,411
  • 5
  • 55
  • 102