Pages: 1 [2]   Go Down
Author Topic: Noob asks WHY????  (Read 1275 times)
0 Members and 1 Guest are viewing this topic.
Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Forget trolls,     
Quote
  If its declared globally,  isn't it declared for EVERY scope?
    is this true or not?
Mostly, but there are some gotchas:
  • Declarations are processed in the order they occur in the source file.  So if you have a function that references a global variable earlier in the source file than the declaration of the global variable, the compiler will complain.
  • If you have split the code into multiple libraries, the libraries will only be able to reference the global variable if they have a declaration in scope.  Typically you would put the declaration of a global variable in a common include file (using the extern keyword), and a definition of the global variable in the library source file.
  • Declarations of the same variable in an inner scope hides the reference to the global variable for the scope of the block that the definition is in.
  • Similarly, if you use #define of the name, it hides reference to the name until you #undef the name.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Huha   Thanks Michael,

I think I have run foul of a couple of those gotchas along the way, and had to Mgyver my way out.

I will keep a note of these for next time.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also - If you declare a global, and then inside a function declare another variable with the same name the local gets used and not the global. Have seen a couple times where a noob did that thinking it was the same variable. The local worked, but it didn't have the global value...
 and then when the function exited the global value was unchanged...

Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Amazed to see "WTF"...
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26195
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Amazed to see "WTF"...
?

This must be getting on for one of the longest threads with the least OP input.

Conjecture on, dudes.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL,
The thread starter used "WTF" in his first post. I was amazed to read...
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26195
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still puzzled by your amazement (if that's a sentence)

Quote
The thread starter used "WTF" in his first post.
S/he also used four question marks and the word "wanna", which I find almost as disappointing.
If they had spelled-out what "WTF" expands to, I'd've been upset.
« Last Edit: March 20, 2013, 03:33:00 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2]   Go Up
Jump to: