The language is very similar to standard C in syntax, but has a different feature set. Most notably, there are no pointers, no structs, and no preprocessor. A string data type has been added, and values are somewhat loosely typed. The next section is for those who are already somewhat familiar with C. A complete guide is also available for those not familiar with C.
There are four types in PocketC: integers (int
), floating point numbers (float
),
characters (char
), strings (string
), and single-dimensional
arrays.
An expression consists of any number of variables, function calls, and constants of any
type, joined by operators and parentheses. Thus, the following expressions are valid:
3 + 5 6.7 + 23 "Hello " + "World" 5*3 + foobar()
When an expression contains values of different types, the two values are first cast to the same type, and then the operator applied. For example in the following expression:
5.7 + 8
The 8 is first cast to a float (8.0), and the two numbers are added (5.7 + 8.0). This becomes slightly more interesting when we use strings. For example:
"The result is " + (5 + 8)
5 is first added to 8, resulting in 13. The 13 is then converted to a string, and appended to "The result is " resulting in "The result is 13". However,
"The result is " + 5 + 8
would result in "The result is 58", since the the 5 is first converted to a string, then appended to "The result is "...
Of course, explicit casting is possible:
56 + (int)"7"
evaluates to 63.
The following operators are provided (in order of precedence, with associativity):
Operator | Assoc | Description |
---|---|---|
= | right | assigns the value of the expression on the right to the variable on the left. |
|| | left | logical or, returns 0 if false, 1 if true |
&& | left | logical and |
== != < <= > >= | left | relational operators |
+ - | left | addition, subtraction |
* / % | left | multiplication, division, modulus |
- ! ++ -- | left | unary operators |
Note: No shortcut logic is performed on the operands of || and &&
Note2: The +=, -=, *=, etc. operators are not supported.
int, float, char, string,
and
single-dimensional arrays. int x=5;
is not yet
legal). The following statements are fully supported: for, while, do, break, continue,
if, then, return
.
Note: for
requires a condition expression (i.e. for (;;)
is not legal, instead, use for (;1;)
or while (1)
)
include
rather than #include
Example:
// My large applet include "Part1" include "Part2" main() { // call functions defined in other memos }
library
to use a native library.Example:
// My Applet library "PocketCLib" main() { int x; // call functions defined by native library x = times5(7); // Where times5(int) is defined by "PocketCLib" }
There are two ways to add special characters to a string. The first is by appending them by number, such as:
str = "Here is a neat little square: " + (char)149;
The other method is through using escape sequences. The following escape sequences are supported:
Escape sequence | \\ | \' | \" | \n | \t | \x |
---|---|---|---|---|---|---|
Interpretation | \ | ' | " | newline | tab | character specified by the following two hex digits. Example: '\x95' is the block character (decimal 149) |