Jump to content

confused about this global variables

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
i re-read the previous chapters, and i caught this confusion here


A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.


I thought the author initially said "outside all functions"
but the 2nd line he said even inside functions

Variables. Data Types.


so what does he means?
i read that from the link (scroll to the mid-body of the page)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have a look at this code:
int globalint;
float globalfloat;

int test()
{
  int testint=globalint; //OK, globalint is visible
  int testfloat=mainfloat; //error! mainfloat is out of scope
  return 0;
}

int main()
{
  int mainint=5;
  globalint=10; //OK, in scope
  globalfloat=10.0; //OK, in scope
  float mainfloat=globalfloat; //OK
  testfloat=mainfloat; //error!  testfloat not in scope
  return test(); //OK test() is global so in scope
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
from my interpretation, i read it like this

okay, we made globalint and globalfloat as gloabl varibles, so they can be use in anywhere, without re-declare again

when tesfloat is assigned to mainfloat, which hasn't yet declare, is not valid



int globalint;

float globalfloat;


int test()

{

  int testint=globalint; //OK, globalint is visible

  int testfloat=mainfloat; //error! mainfloat is out of scope

  return 0;

}


int main()

{

  int mainint=5;

  globalint=10; //OK, in scope

  globalfloat=10.0; //OK, in scope

  float mainfloat=globalfloat; //OK

  testfloat=mainfloat; //error!  testfloat not in scope

  return test(); //OK test() is global so in scope

}

when you assign testfloat to mainfloat, again, testfloat is no where (no declaration)

so simply you mean, local variable is anything within the block of that function body, but can only use within the block of the body

while gloabl, can be use anywhere, any block of function, in this case, can be either main, or test, or even both....

right?

if i make testfloat as a local variable, then this statement is true


int test()

{

 int testfloat,


  int testint=globalint; //OK, globalint is visible

  testfloat=mainfloat; //error! mainfloat is out of scope

  return 0;

}


if i am right everything, beside any comment, if i don't want to change the code at all, but to make testfloat (the one in test function) valid?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Here's the thing, when you exit a block of code, the variable declared in that block cease to exist. Functions CANNOT see each others variables. Parameters (especially pointers and references) are used to pass information back and forth between functions.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
yeah i remembered that pointers thing
it's like first we assume we know where that data is store, and assign something to that address (using pointer / references)
thanks