program

hey, i am a newbie in c++ . so i need a help
can anyone tell me what is :

#include < iostream>
namespace std

variable :

- global scope
- local scope

i hope someone will make me understand about this particular thing

thank you
ashierock
When a variable is in global scope, the whole program can use it.

When the variable is in local scope, only the scope that has the variable may use it.

In order to create a global variable, you must create the variable outside of int main().

In order to create a local variable, you must create the variable within the {} brackets of anything. But it would only exist it those brackets. Once you go to a different bracket, it will disappear.

Example: Global scope variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int number;   // this is a global variable
int main()
{
number = 7;   //you can use this variable even tho it wasnt created in this scope.
return 0;
}



Example: Local scope variable.

[code]
int main()
{
int number;     // this variable can only be used within the int main scope.
number = 7;
return 0;
}


iostream is the header file that allows you to do input and output.
You would need it if you would like to do something like
std::cout<< "Hello world \n";

namespace std is a shortcut of using std without having to type std all the time.

So, when you use namespace std, all you need to type is
cout<< "Hello world \n";

instead of

std::cout<< "Hello world\n";

Okay I hope I helped. I'm new too!
Last edited on
@Hellomoon- Local variables don't have to necessarily be initialized in int main, they can be initialized in any function or class outside of int main.
Oh woops. Yeah I should be more careful haha. Yeah, Nova's right. A local variable only exists between it's whitespace brackets that's its in. Now, you can use references and pointers to access those but that's more advanced stuff saved for later.
Topic archived. No new replies allowed.