C0 Download: Here
Basic Types:
int
: from -2^{31} to 2^{31}-1
bool
:
char
: for a character like c
string
: "like this"
t[]
: array with fixed size
t*
: a pointer
struct s
: structs or records
Printing:
print()
println()
: print line
printint()
printchar()
printbool()
printf()
: formatting with %d
or%i
for int, s%
for string, %c
for char, and %x
for hexadecimal.
int64_t x = 4294967296; // 4,294,967,296 is 2^32
unsigned int y = 4294967295; // 4,294,967,295 is 2^32 - 1
printf("%d and %d", x, y); // Prints 0 and -1
:warning: this specifier character assumes that the input is a 32 bit, signed integer, so if the input is not a 32-bit signed integer, it will automatically be cast to an int32_t.
TODO: read about details about different types Here
Contracts:
//@requires
and //@ensures
should only be added below function declaration.int log(int x)
//@requires x >= 1;
//@ensures \result >= 0;
{ int r = 0;
while (x > 1) {
x = x / 2;
r = r + 1;
}
return r;
}
//@loop_invariant
: tested just before the exit condition is tested.int log(int x)
//@requires x >= 1;
//@ensures \result >= 0;
{ int r = 0;
int y = x;
while (y > 1)
//@loop_invariant y >= 1 && r >= 0;
{
y = y / 2;
r = r + 1;
}
return r;
}
//@assert
: just assert.If a contract is violated, program execution will abort with an error message pinpointing the failing contract. (you can use flag
-d
to remove warnings)
TODO: see if structs performs like a class?
-d
flag to enable dynamic checking of syntax//@assert
to debugTODO: read these two links below Here Valgrind: to catch memory related errors (Here)
1/0
gives Floating exception
A[-1]
gives Out of bounds array access
and Segmentation fault
**alloc(int*)
gives Attempt to dereference null pointer
and Segmentation fault
-d
flag. Dynamic checking of contracts can change the asymptotic complexity of functions. Do not be deterred by this, however: write expressive contracts (even if they are slow) and test your code on small examples, then run it without dynamic checking on large ones.TODO: read This
TODO: read This
autolab122 hw
for a list of homework.
autolab122 download [hw]
to download sample code to current directory.
cc0 -d factorial.c0
to compile code into a lower level machine code. the flag -d
is for debug mode. Compiler will give you a.out
if there is no error.
./a.out
to run the program starting from main()
coin -d factorial.c0
step by step running to check grammar for testing and real time feedback.
#quit
or C-d
to exit the interpreter
autolab122 handin [hw] {files...}
to submit your code.
autolab122 feedback
to view overall feedback
cc0 [file_1, file_2, file_3, ..., main_file]
:warning: main file should be placed last
:warning: main function should always return something
:warning: conio lets you print -> stands for console i/o (otherwise you can't print) so use <conio>
in the main file.
-d // debug, run @requires ext... (now with backtrace)
-l // load library (util)
-W // warn you unused variables
-o // out put a name you define (not a.out)
int: is the same thing as hexadecimal
util:
int2hex();
hex2int();
[^2]: Basic Building Blocks
Table of Content