SecreC 2 language
2.8.0 (2023.09)
Language and standard library reference
|
Variables in SecreC
Variables in SecreC consist of lowercase and uppercase Latin characters, underscores and decimal digits. The first character of a variable must not be a decimal digit. Reserved keywords are not allowed to be used as variable names.
Variables are declared by writing a type annotation followed by one or more variable names. Optionally, it is possible to assign a value right after the variable declaration by writing an expression after the assignment sign. All declared variables are assigned reasonable default values. For integers this value is 0, for booleans it is false.
Listing 1: Some variable declarations
As previously mentioned, types are formed by writing security domain before the data type, and public may be omitted. Array declarations allow the shape (sizes of dimensions) to be specified after the variable name between parenthesis. Initial shape may be non-static and can be an arbitrary public integer expression. The shape is specified with public signed integer type.
Listing 2: Array declarations
It is possible to define an empty array by not specifying the shape, or by having any of the dimensions have no elements. If an array definition is immediately followed by an assignment, and the shape is not specified then the shape is inherited from the right hand side expression.
Listing 3: (Non)empty arrays
The scope of a variable always ends with the containing statement block. Variables with the same name can not be declared within the same scope, but can be overshadowed by declaring a new variable with same name in a deeper nested scope. Global variables never fall out of scope, and can not be overshadowed. Privacy domains, and domain kinds can not be overshadowed. Variables with the same names can be declared in non-overlapping scopes.
Listing 4: Variable overshadowing