i just need to see if i'm getting things right.
a local veriable is one that can only be used inside a single funtion but a globel variable can be used inside all functions.
if this is true how do you declare globel variables
i just need to see if i'm getting things right.
a local veriable is one that can only be used inside a single funtion but a globel variable can be used inside all functions.
if this is true how do you declare globel variables
WhAtHA hell Is GoInG ON
declare the varible outside of a function....
Code:int this_is_global = 1; void myFunc() { int this_is_local = 2; }
Thats correct. You declare global variables either in a header file (.h) which would be included by all source files that need to access the global variable or in the source file but not inside any function.
well, if you declare it in a header file, it will need to be given the keyword extern and will still need to be declared somewhere else. If you don't do this, the linker will get a conflict when more than 1 file includes the header.
should this work or am i getting the whole concept wrong?
Code:#include <iostream> using namespace std; inline void b() { cout<< a <<endl; cin.get(); } int a; int main () { cout<<"number"; cin>> a; cin.get(); cout<< a <<endl; cin.get(); b(); }
WhAtHA hell Is GoInG ON
sorry i just worked it out you have to declare it before the function you wish to use it on
WhAtHA hell Is GoInG ON
This won't compile
But this willCode:inline void b() { cout<< a <<endl; cin.get(); } int a;
You have to declare global variables before their first use.Code:int a; inline void b() { cout<< a <<endl; cin.get(); }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.