Prev | Index | Next |
The simplest expressions in C* consist of constants, variables, and function calls. The types of constants are given in the following table:
Examples | Corresponding Type | Description |
---|---|---|
42 | int | Integer constant |
1.0, 5.4e-3 | float,double,ldouble | floating point constant |
true | bool | Boolean value |
null | pointer | Used to represent a pointer that isn't pointing at an object |
'c', '\t' | char | Character constant |
There are also string and array constants, which are described later in the array section. (String literals are just syntactic sugar for array literals of characters.)
The numeric constants don't support suffixes, as in C++. Floating point constants can be assigned to any floating point type, and there is only 1 size of integer. Also, floating point constants may no longer have a trailing decimal, as in "1.
" - write "1.0
" instead. These constants were removed for pragmatic reasons (and because mathematicians don't write numbers that way anyway).
Also note that null
is now a keyword, where in C++ null pointers were represented by the value 0
(with NULL
typically defined as a macro).
Operators in C* can be thought of as special syntax for function calls. There are many built-in operators, as well as some operators whose syntax is provided but which have no predefined meaning. Since all the operators can be overloaded, even to accept primitive types (see the later section on functions), this allows the language to have operations with nice-looking syntax, without the bloat of having all those operations built-in. For example, some languages contain a ** operation for power; C* includes ** as an operator that the user can define however they want. The precedence and syntax (e.g. binary or unary) of these operators is fixed.
The chart of operators, in order from tighest to loosest precedence, is given in the table below.
Operator | Associativity | Description |
---|---|---|
not x | Right | Logical not for boolean values |
++ x | Right | Increment for numbers |
-- x | Right | Decrement for numbers |
- x | Right | Negative for numbers |
~ x | Right | Same as C++, except ~ is user-defined |
x ** y | Nonassociative | User-defined |
x ? | Nonassociative | User-defined |
x * y | Left | Multiplication of numbers |
x / y | Left | Floating-point division (even if operands are integers) |
x \ y | Left | Integer division (operands must be integers) |
x % y | Left | Modulus of numbers |
x + y | Left | Addition of numbers |
x - y | Left | Subtraction of numbers |
x < y | Left | Less than for numbers |
x > y | Left | Greater than for numbers |
x <= y | Left | Less than or equal for numbers |
x >= y | Left | Greater than or equal for numbers |
x == y | Left | Equality |
x != y | Left | Inequality |
x ~== y | Left | User-defined |
x & y | Left | User-defined |
x xor y | Left | User-defined |
x | y | Left | User-defined |
x and y | Left | Logical and for booleans |
x or y | Left | Logical or for booleans |
x := y, x += y, etc. | Left | Assignment |
Note the removal of the comma operator. Also, the ?: syntax from C/C++ is removed, and replaced with if/else expressions. For example:
a := if (x) b else c;
This notation should be familiar to functional programmers. (Regular if/else blocks are also allowed, of course.)
Prev | Index | Next |