Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the problem with the following piece of code?

Tags:

c

This piece of code seems to be creating some troubles while compiling. Any explanation?

int i =20;
int maxlen = i;
int main()
{
     int j = i;
     printf("i=%d , j=%d\n", i , j);
}
like image 973
phoenix Avatar asked Dec 28 '22 23:12

phoenix


1 Answers

In C, you can't initialize global variables using non-constant expressions. Initializing maxlen to i fails because i is not a constant expression. It's part of the C standard.

Why not #define a constant?

#define MAXLEN 20
like image 139
Rafe Kettler Avatar answered Jan 16 '23 04:01

Rafe Kettler