By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

need help for a basic question abt static,extern variable

P: 2
#include <stdio.h>
void main()
{
static int i = 1;
void cal();
printf("%d\n",i);
cal();
}

void cal()
{
printf("%d\n",i);
}

hi im totally confused abt static, extern local variables even after going through enough material abt them. for example in the above example i get an error as undeclared i in cal() function even after having declared i as static variable in main function. isnt a static variable supposed to exist in between different functions. then why should i define it again in cal() function. can anyone clear this basic question.
Apr 17 '07 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
#include <stdio.h>
void main()
{
static int i = 1;
void cal();
printf("%d\n",i);
cal();
}

void cal()
{
printf("%d\n",i);
}

hi im totally confused abt static, extern local variables even after going through enough material abt them. for example in the above example i get an error as undeclared i in cal() function even after having declared i as static variable in main function. isnt a static variable supposed to exist in between different functions. then why should i define it again in cal() function. can anyone clear this basic question.
Because it's not global,it's local static int which is initialised inside main,declare it before main and everything will be OK.

Savage
Apr 17 '07 #2

P: 2
Because it's not global,it's local static int which is initialised inside main,declare it before main and everything will be OK.

Savage
thanx savage. in fact i got it cleared after going thru some good stuff abt the scope of variables. neverthless ur reply also has been useful.
thanx
Apr 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.