-
December 19th, 2013, 10:16 PM
#1
global variable
expeceted output:20 but what i got is:22.why
while calling sub function it should take the
global variable am i right
#include <iostream>
using namespace std;
int a=0;
void sub()
{
cout<<a;
}
int main()
{ a=2;
cout<<a;
sub();
}
-
December 20th, 2013, 02:38 AM
#2
Re: global variable
Originally Posted by bhuvaneshnick
expeceted output:20 but what i got is:22.why
Because you changed the value of 'a' to 2 before you printed it (twice).
Step through your program with the debugger and look at the value of 'a'.
Oh, and please use code tags when posting code.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 20th, 2013, 04:47 AM
#3
Re: global variable
Why would you expect this when you set global a to 2 in main() ?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 20th, 2013, 11:44 AM
#4
Re: global variable
Please format your code and use code tags. As far as why, why not run your code in a debugger and figure out what is going on? Also, do a bit better job formatting so two 2's in a row doesn't look like 22.
Try this code.
Code:
#include <iostream>
using namespace std;
int a=0;
void sub()
{
cout<< "in sub " << a << endl;
}
int main()
{
a = 2;
cout << "in main " << a << endl;
a = 4;
sub();
return 0;
}
-
December 20th, 2013, 10:31 PM
#5
Re: global variable
next time i correct that.i am new to c.all are saying run it through.but i actually don't know what it mean.debugger .please detail me
-
December 21st, 2013, 07:47 AM
#6
Re: global variable
"Debuggers are software tools which enable the programmer to monitor the execution of a program, stop it, restart it, set breakpoints, and change values in memory. Generally, high-level programming languages, such as c/c++, make debugging easier, because they have features such as exception handling that make real sources of erratic behaviour easier to spot. In programming languages such as C/c++, bugs may cause silent problems such as memory corruption, and it is often difficult to see where the initial problem happened. In those cases, memory debugger tools may be needed."
See
http://en.wikipedia.org/wiki/Debugging
What compiler are you using? They nearly all come with their own debugger which can be used to find run-time problems with code and to understand how code is executed.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 21st, 2013, 11:43 AM
#7
Re: global variable
Originally Posted by 2kaud
"Debuggers are software tools which enable the programmer to monitor the execution of a program, stop it, restart it, set breakpoints, and change values in memory. Generally, high-level programming languages, such as c/c++, make debugging easier, because they have features such as exception handling that make real sources of erratic behaviour easier to spot. In programming languages such as C/c++, bugs may cause silent problems such as memory corruption, and it is often difficult to see where the initial problem happened. In those cases, memory debugger tools may be needed."
See
http://en.wikipedia.org/wiki/Debugging
What compiler are you using? They nearly all come with their own debugger which can be used to find run-time problems with code and to understand how code is executed.
i am using code::block(ide).i found the debugging option but still lot there like watches and more.soon i learn they are really mean.thanks sir.one last question i am new to forums.what do u get if i give reputaion or like to post.because here peoples are serious on helping.thats made me ask such question.sorry if i am wrong.again thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|