473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help for a basic question abt static,extern variable

#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
2 1630
Savage
1,764 Expert 1GB
#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
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

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

Similar topics

8
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...
6
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...
10
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"
4
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(){ }
12
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++; }
32
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...
14
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...
14
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...
1
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.