#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.
2 1630
#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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jose luis fernandez diaz |
last post by:
Hi,
I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:
"A variable defined outside any function (that is global, namespace,
and class...
|
by: pembed2003 |
last post by:
Hi all,
I am reading the book "C++ How to Program" and in the chapter where it
discuss scope rule, it says there are four scopes for a variable:
function scope
file scope
block scope...
|
by: Gernot Frisch |
last post by:
Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
|
by: Jianli Shen |
last post by:
New c++:-)
When look through code, I always find lot of funtions are
declared as (in .h):
static void function1();
when implementing,(in .cpp), just
void function1(){
}
|
by: fox |
last post by:
How do I (portably) make a forward reference to a static (I mean
file-scope) variable?
I've been using "extern" for years, for example:
extern int x;
int foo(void)
{
return x++;
}
|
by: lcdgoncalves |
last post by:
Hi everyone
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Many textbooks avoid to discuss this matter and/or discuss only...
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared...
|
by: cs1975 |
last post by:
Hi Everyone,
I wanted to make a global variable
double rowCols = {{1,0,0},{0,1,0},{1,0,0}};
to static global
static double rowCols = {{1,0,0},{0,1,0},{1,0,0}};
I am updating this global...
|
by: Jaco Naude |
last post by:
Hi,
I'm using a static library in my application which links fine except
for a few global variables. The static library only contains a bunch
of .cpp and .h files and the global variables are...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |