in Programming in C
1,316 views
0 votes
0 votes

Is it static declaration or static assignment?

int main()
{
    int x=20;
    static int y=x;
    if(x==y)
    printf("Equal");
    else
    printf("Not Equal");
    return 0;
}

What is output?and why?

in Programming in C
by
1.3k views

4 Comments

@kumar.dilip it will work in C++ but won't in C. Check out the geeksforgeeks reference given above. They have explicitly mentioned that this works fine in C++.
0
0
what my suggestion is " JUST CONCENTRATE on C " for GATE.

But it's upto you to follow it or not !
0
0
Ok bro.
0
0

1 Answer

0 votes
0 votes
this will give error.

Reason: X is an auto variable.. So it will get memory in run time.

where as Y is a static variable hence it will get memory in compile time. but in compile time value of X is not known. Hence we can not initialize Y with such a value that is unknown at the time of Y's initialization.. So it will generate compile error.

Related questions