# self doubt variable scope

122 views
#include <stdio.h>

/* global variable declaration */
int g = 20;

int main () {

/* local variable declaration */
int g ;

printf ("value of g = %d\n",  g);

return 0;
}



why this printing value of g is 0 instead of garbage  because g is declared which is local variable  in main function.    are variables inside main function by default global?

0
It should be garbage.
0

no, by default variables inside main function is not by default global

## Related questions

1
135 views
void main() { int *ptr; ..... ..... { int ch; ptr = &ch; } ..... // Here ptr is dangling pointer } Since here's no function calling and only main(). So, I am not able to understand which variable is going out of scope? ptr or ch? Thanks all!