#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 1647 Savage 1,764
Recognized Expert Top Contributor
#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 static variables) is initializated (constructed) before main
is invoked . . ."
.. . .
|
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
function-prototype scope
I think(might be wrong):
|
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 the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".
I don't know if this is a trivial matter since the reserved word
|
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 static inside functions (i.e. local static
objects)
5. objects declared at file scope.
|
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 variable in one of the methods in the flow,
|
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 defined as follows:
extern unsigned mgl_numg, mgl_cur;
extern float mgl_fact;
extern long mgl_gen_fnt;
extern short mgl_buf_fnt;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |