The C0 Language

Downloading C0

C0 Download: Here

Learning C0 Syntax[^2]

Basic Types:

Printing:

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:

int log(int x)
//@requires x >= 1;
//@ensures \result >= 0;
{ int r = 0;
  while (x > 1) {
    x = x / 2;
    r = r + 1;
  }
  return r;
}
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;
}

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?

Debugging with C0^1

  1. printing lines are to buffer unless "/n" is added
  2. use -d flag to enable dynamic checking of syntax
  3. using //@assert to debug

General Debugging Practice

TODO: read these two links below Here Valgrind: to catch memory related errors (Here)

Error Types

  1. 1/0 gives Floating exception
  2. A[-1] gives Out of bounds array access and Segmentation fault
  3. **alloc(int*) gives Attempt to dereference null pointer and Segmentation fault

Fixing a Performance Bug

  1. Use the cc0 compiler, not coin. Coin is an interpreter and as such too slow in many cases when the input becomes large.
  2. Omit the -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.
  3. Remove any computations you might have introduced for the purpose of debugging. For example, if you decided to compute and then print the length of a queue each time around the loop to make sure it is as expected, then you may need to comment out the print statement and the computation of the length to avoid unnecessary computation.
  4. Clarify, in comments, the expected computational complexity of the critical functions in your program. For example, a linear search through an unsorted array should be O(n). Then test the functions separately multiple times and verify that the actual runtime is consistent with your expectation.

Learning C0 Libraries

TODO: read This

Writing Unit Test

TODO: read This

Running Commands for Homework

Running the Code

cc0 [file_1, file_2, file_3, ..., main_file]

Flags

-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)

Data Structure

int: is the same thing as hexadecimal

Libraries

util:

[^2]: Basic Building Blocks


Table of Content