[C] Introduction to C
The Big Ideas:
- High-level and a low-level language:
- C is a compiled language (C -> assembly -> machine language).
- It has a few high-level constructs that can be readily translated into assembly.
- Imperative:
- Assignment statements explicitly change memory values.
- As opposed to declarative, where statements describe function.
- Procedural:
- All code statements are contained in functions.
- Functions can be bundled into libraries which allow you to build large programs from smaller pieces.
- Functions are implemented using a stack.
- File-oriented.
- Portability: the same C code can be compiled for different ISAs.
The Compilation Process:
- The code files that you write are compiled into assembly code which is then assembled into machine code.
- 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:
- char: 8 bits
- int: 16/32/64 bits
- float: 32 bits (IEEE Floating Point Standard)
- double: 64 bits (double the size of float)
Arrays in C:
- Array names are like pointers, but they're not pointers. The name of an array is a label for the starting address in memory.
- 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:
- There're two types of variables: automatic and static.
- Automatic variables close their values when their block terminates. Local variables are by default automatic.
- Static variables retain values between invocations. Global variables are static.
- Local variables can be declared static.
Constants:
- There are two ways to do it:
- Modifier const
- Pre-processor directive #define
- We can "#define"d values easily, but const types never change.
Comments
Post a Comment