NXC  Version 1.2.1 r5
 All Data Structures Files Functions Variables Groups Pages

All variables in NXC are defined using one of the types listed below:

Variables are declared using the keyword(s) for the desired type, followed by a comma-separated list of variable names and terminated by a semicolon (';'). Optionally, an initial value for each variable may be specified using an equals sign ('=') after the variable name. Several examples appear below:

int x; // declare x
bool y,z; // declare y and z
long a=1,b; // declare a and b, initialize a to 1
float f=1.15, g; // declare f and g, initialize f
int data[10]; // an array of 10 zeros in data
bool flags[] = {true, true, false, false};
string msg = "hello world";

Global variables are declared at the program scope (outside of any code block). Once declared, they may be used within all tasks, functions, and subroutines. Their scope begins at declaration and ends at the end of the program.

Local variables may be declared within tasks and functions. Such variables are only accessible within the code block in which they are defined. Specifically, their scope begins with their declaration and ends at the end of their code block. In the case of local variables, a compound statement (a group of statements bracketed by '{' and '}') is considered a block:

int x; // x is global
task main()
int y; // y is local to task main
x = y; // ok
{ // begin compound statement
int z; // local z declared
y = z; // ok
y = z; // error - z no longer in scope
task foo()
x = 1; // ok
y = 2; // error - y is not global