[C] Introduction to C

The Big Ideas:

  1. High-level and a low-level language:
    1. C is a compiled language (C -> assembly -> machine language).
    2. It has a few high-level constructs that can be readily translated into assembly.
  2. Imperative:
    1. Assignment statements explicitly change memory values.
    2. As opposed to declarative, where statements describe function.
  3. Procedural:
    1. All code statements are contained in functions.
    2. Functions can be bundled into libraries which allow you to build large programs from smaller pieces.
    3. Functions are implemented using a stack.
  4. File-oriented.
  5. Portability: the same C code can be compiled for different ISAs.
The Compilation Process:
  1. The code files that you write are compiled into assembly code which is then assembled into machine code.
  2. Some compilers, like clang, perform both phases and simply output the binary, executable result.
All C programs must start in the function "main". Recall that C is procedural. Everything is inside a function.

Variables in C:

There are four built-in data types. The width of each data type is machine dependent:
  1. char: 8 bits
  2. int: 16/32/64 bits
  3. float: 32 bits (IEEE Floating Point Standard)
  4. double: 64 bits (double the size of float)
Arrays in C:
  1. Array names are like pointers, but they're not pointers. The name of an array is a label for the starting address in memory.
  2. An array name is simply an assembly label. A pointer gets a slot in memory. An array name is the name of the slot.
Global Variables:
  1. There're two types of variables: automatic and static.
    1. Automatic variables close their values when their block terminates. Local variables are by default automatic.
    2. Static variables retain values between invocations. Global variables are static.
  2. Local variables can be declared static.
Constants:
  1. There are two ways to do it:
    1. Modifier const
    2. Pre-processor directive #define
  2. We can "#define"d values easily, but const types never change.

Comments

Popular posts from this blog

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee