How to Fix “initializer element is not constant” error

Look at the code below:

#include <stdio.h>
int a = 1;
int b = 2;
int c = a+b;

int main() {
    printf("c is %d\n", c);
    return 0;
}

gcc-o test test.c error during compilation: initializer element is not constant

—–
Reason: the value of the global variable C cannot be determined at compile time; it must be determined at run time.
Solutions:

#include <stdio.h>
int a = 1;
int b = 2;
int c; 

int main() {
	c = a + b; 
	printf("c is %d\n", c);
	return 0;
}

 


Read More: