mrigendra
mrigendra

Reputation: 1576

Global variable does not take new value

I was making a simple C program

#include<stdio.h>
static int a;
a = 5;
int main()
{
printf("%d",a);
return 0;
}

Compiler error: "non static declaration of 'a' follows static declaration"

What does this error mean?

Upvotes: 4

Views: 125

Answers (7)

nobalG
nobalG

Reputation: 4630

We cannot write any assignment statement globally. For example:

#include <stdio.h>
static int i=10;   //Initialization statement
i=25;              //Assignment statement
int main(){
    printf("%d",i);
    return 0;
}

Output: Compilation error

Note: Assigning any value to the variable at the time of declaration is known as initialization while assigning any value to variable not at the time of declaration is known assignment.

Upvotes: 1

A.s. Bhullar
A.s. Bhullar

Reputation: 2788

Just a little modification to your code will help you to understand it.

#include<stdio.h>
//static int a;   //comment this statement
a = 5;
int main()
{
printf("%d",a);
return 0;
}

now compile this code [please enable your compiler's warnings]

Then you will get a warning something like this data definition has no type or storage class [enabled by default]

So now try to understand this warning it means that compiler treat statement a=5 as a definition but without data type. But compiler unable default data type that is int.

so this statement is equivalent to

int a=5;

Upvotes: 0

tohava
tohava

Reputation: 5412

A = 5 is considered as an attempt to create another variable called a. You should put the = 5 after static int a.

Static int a = 5;

Upvotes: 0

Dakorn
Dakorn

Reputation: 903

You can't set value as you did it: a = 5; before main(...). See code below:

static int a = 1; //default = 0;

int main()
{
    printf("a = %d\n", a);

    a = 2;

    printf("a = %d\n", a);

    return 0;
}

Output:

a = 1
a = 2

Upvotes: 0

undur_gongor
undur_gongor

Reputation: 15954

Your first declaration of a is static (ahas internal linkage).

The second declaration is not static (a has external linkage). Yes, a = 5; is a declaration with implicit type int in this case.

Both do not agree.

Btw. for functions this would be o.k. because the second declaration would "inherit" the internal linkage.

Upvotes: 1

djna
djna

Reputation: 55957

Outside a function you can only declare variables, you cannot have actual code statements.

a = 5;

is being interpreted as another declaration, when your intent I think is to write some code.

instead declare and initialise a at the same time

 static int a = 5;

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

what this error log means?

It is a little tricky: what looks like an assignment

a = 5;

is treated as

int a = 5;

due to an old C rule that allowed you to declare int variables and int-returning functions without specifying their type explicitly (this is definitely not a good idea in the modern version of C).

Note that this is treated as a declaration with an implicit int only in the scope outside a function body.

You can fix it by combining the declaration with initialization, like this:

static int a = 5;

Upvotes: 11

Related Questions