Jump to content

Variable Scope

Recommended Posts

SB V7.05


#include <system.h>

void main()
  for( unsigned char x = 0; x < 32; x++ )



This compiles ok. Is this correct? surely when x++ is executed, 'x' is out of scope (x exists only in the for loop)

Edited by TomF
Link to post
Share on other sites

Hi Reynard



Probably yes, but without the scope constraints of C++..


In fact in 'C' (K&R) you can put almost anything in the for( ; ; ) construct.

The only really importatnt things are:

The first statement only gets executed once, when entering the loop

The second controls the exist of the loop. The expression result gets interpreted as a boolean.and is evaluated at the beginning of each loop.

The third is executed at the end of the each loop.


Besides this, put there anything you want.

And don't forget the ',' operator that enables you to stuff more than 3 statements inside the "for".



Best regards



Edited by JorgeF
Link to post
Share on other sites

When I try to compile with gcc on Linux (removed the nop) I got this error:


poo.c:5: error: 'for' loop initial declaration used outside C99 mode


When using g++ I got:

poo.c:1: error: '::main' must return 'int'

poo.c: In function 'int main()':

poo.c:8: error: name lookup of 'x' changed for new ISO 'for' scoping

poo.c:3: error: using obsolete binding at 'x'


The first line is no big deal. They just want a return code. But I believe this is the complaint you were looking for.


Normally, the scope is taken care of with compilers by allocating and deallocating stack space. In BoostC because PICs are a bit evil, they do not allocate and deallocate the stack since there really is none for variables. Instead, the compiler cleverly reuses fixed variable locations based on the scope (and I believe subroutine nesting) to reuse memory.


This may be why the compiler does not complain since it just reuses the last location.


I do believe this should be flagged at compile time since x is not defined out of the for loop and clearly the coder made a mistake.

Link to post
Share on other sites

The reason I asked about the declaration inside the for loop was I tried another compiler (MikroC) and it wasn't having it.


After some reading on the net, C99 introduced the declaration into the for loop.





Link to post
Share on other sites

Hi Tom,


Given variable declaration has been allowed within a for loop, then yes it is a bug to access the variable out of scope.


I don't think SourceBoost C says it is ISO C99 compatable, but as it allows some C++ features to be used, it should conform to the rules.





Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...