Files
eth-summaries/semester3/spca/code-examples/00_c/00_basics/02_declarations.c

44 lines
1.5 KiB
C

int my_int; // Allocates memory on the stack.
// Variable is global (read / writable by entire program)
static int my_local_int; // only available locally (in this file)
extern const char *var; // Defined in some other file
const int MY_CONST = 10; // constant (immutable), convention: SCREAM_CASE
enum { ONE, TWO } num; // Enum. ONE will get value 0, TWO has value 1
enum { O = 2, T = 1 } n; // Enum with values specified
// Structs are like classes, but contain no logic
struct MyStruct {
int el1;
int el2;
};
// Like structs, but can only hold one of the values!
union MyUnion {
int ival;
float fval;
char *sval;
};
int fun( int j ) {
static int i = 0; // Persists across calls of fun
short my_var = 1; // Block scoped (deallocated when going out of scope)
int my_var_dbl = (int) my_var; // Explicit casting (works between almost all types)
return i;
}
int main( int argc, char *argv[] ) {
if ( ( my_local_int = fun( 10 ) ) ) {
// Every c statement is also an expression, i.e. you can do the above!
}
struct MyStruct test; // Allocate memory on stack for struct
struct MyStruct *test_p = &test; // Pointer to memory where test resides
struct MyStruct test2;
union MyUnion my_uval; // Work exactly like structs for access
test.el1 = 1; // Direct element access
test_p->el2 = 2; // Via pointer
test2 = test; // Copies the struct
return 0;
}