- **Compiler**: Converts C into assembly language
- **Assembler**: Converts assembly into machine code
- **Linker**: Combines machine code files into an executable
- **Loader**: Loads an executable into memory and executes it
![C Program Compiler Process](/teaching/2021f/cs130/assets/images/COD/compiler-diagram.png)
# C Progams
## An Example
```c
#include "stdio.h"
int main()
{
char name[20];
int age;
printf("Hi! What is your name?\n");
scanf("%s", name);
printf("Hello, %s, it is nice to meet you!\n", name);
printf("What is your age?\n");
scanf("%d", &age);
printf("Wow, %d is pretty old!\n", age);
return 0;
}
```
```py
# Python equivalent
name = input("Hi! What is your name?\n")
print("Hello,", name, "it is nice to meet you!")
age = int(input("What is your age?\n"))
print("Wow,", age, "is pretty old!")
```
## Variables and Types
- In C, the **type** of a variable must be declared before it is first used
```c
int x = 3 + 4; // Must give a type the first time
...
x = x + 1;
```
## Variables and Types
- Integers
```c
int x = 5; // usually 32-bits
long y = -13; // usually 64-bits
short z = 7; // usually 16-bits
unsigned int u = 7; // makes number interpreted as positive
```
-
Floating point
```c
float f = 3.14; // usually 32-bits
double d = -7.123; // usually 64-bits
```
## Variables and Types
- Character
```c
char c = 'a'; // usually 8-bits
```
-
No Boolean type---uses `int` instead
+ `0` means false
+ Any other `int` is true
## Comments
- Uses `/* ... */` for multi-line comments
- Uses `// ...` for single-line comments
```c
/* This is a comment that spans
multiple lines. Everything
in-between is ignored */
int x = 10; // This is a single line comment.
x = x + 5; // Everything after it is ignored.
```
## Operators
- C uses symbols `||`, `&&`, and `!` for Boolean expressions instead of `and`, `or`, and `not`
```py
# Python code
result = ((a > 0 or b > 0) and (c > 0)) == False
```
```c
int result = ((a > 0 || b > 0) && c > 0) == 0;
```
## Operators
- `a / b` behaves differently in C
+ Does **integer division** if both are integral
+ Does **floating point division** if one or both are floating point
## For Loops
- In C, the `for` loop means something else entirely
- The following two code blocks are equivalent
```c
for (init; test; update) {
body
}
```
```c
init
while (test) {
body
update
}
```
## For Loops
- A common use is to iterate through an array
```c
int arr[5] = {7, 3, 1, 2, 10};
for (int i = 0; i < 5; i++) {
printf("The next value is: %d\n", arr[i]);
}
```
```py
arr = [7, 3, 1, 2, 10]
for i in range(10):
print("The next value is:", arr[i])
```
## Arrays
- Fixed size---they cannot be made smaller or larger
-
No way to check its size after creation
-
No index out of bounds checks
```c
int arr[50]; // creates an array with 50 spots
int val = arr[60]; // will happily try to do this
printf("%d", val);
```
## Functions
```c
void swap(int v[], int k)
{
int temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
void sort (int v[], int n)
{
for (int i = 0; i < n; i++) {
for (int j = i - 1; j >= 0 && v[j] > v[j + 1]; j--) {
swap(v, j);
}
}
}
```
## Exercises
- Start by setting up **Visual Studio Code** on your computer
- You will need the [C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
- If you are running Windows, you will also to follow the [Using GCC with MinGW](https://code.visualstudio.com/docs/cpp/config-mingw) tutorial to install the C compiler
## Exercise 1
- Write a program that asks the users to type in four numbers and prints the average of those four numbers
## Exercise 2
- Create a function `void reverse(int arr[], int n)` that takes an array with `n` elements and reverses the order of the elements
- Test it out in a `main` function such as:
```c
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
reverse(arr, 5);
for (int i = 0; i < 5; i++) {
printf("The next value is: %d\n", arr[i]);
}
return 0;
}
```