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
Printable View
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
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();
}
sorry i just worked it out you have to declare it before the function you wish to use it 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();
}