- **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 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!")
```
## First things to notice
- **Python:** tab indicates code block
- **C:** `{ }` indicates code block
- white space doesn't matter - tabs are just for readability
- **C:** all code needs to be in a function, `main` is executed first
- **C:** most statements end with a semicolon `;`
## 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
// c code
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
#Python code
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
- Calculating its size after creation is cumbersome
- 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);
```
# C Functions
## Function definitions and calls
```c
int foo(int bar) {
/* do something */
return bar * 2;
}
int main() {
foo(1);
}
```
instead of `def` in Python functions, you give the function's *return type* - can be any type or `void`
parameters are *variable declarations*
## Arrays as parameters
When passing an array, you can use `[]`, but remember arrays don't know how big they are, so you might also need to pass the length as an `int`
```c
#include "stdio.h"
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);
}
}
}
int main()
{
int arr[5] = {7, 3, 1, 2, 10};
sort(arr,5);
for (int i = 0; i < 5; i++)
{
printf("The next value is: %d\n", arr[i]);
}
return 0;
}
```
# Exercises
## 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;
}
```
## `printf`
https://www.cplusplus.com/reference/cstdio/printf/
## `scanf`
https://www.cplusplus.com/reference/cstdio/scanf/
## Assignment 7
- [Assignment 7](../../assignments/assignment-7/): write a C program that includes
1. An array
2. A function other than `main`
3. A call to `scanf` to get user input
4. A call to `printf` which includes displaying the value from a variable
5. A `for` loop
- Due tonight
# Address of operator
#### Address of operator
**NB:** The `&` operator means "address of" - read from keyboard and store in a specific address in memory
```c
int a;
scanf("%d",&a);
```
What do `"%d"` and `"%f"` mean?
# Strings
## Strings
- There is no **String** type in C
-
Strings are just arrays of type `char`
```c
char str[20] = "abc";
printf("The string is %s", str);
```
-
The above code is shorthand for the following:
```c
char str[20];
str[0] = 'a';
str[1] = 'b';
str[2] = 'c';
str[3] = '\0'; // String terminating character
```
-
The remaining 16 characters are unused
## Strings
- Suppose we forget to include the `'\0'` character
```c
char str[20];
str[0] = 'a';
str[1] = 'b';
str[2] = 'c';
printf("The string is %s", str);
```
- What do you think happens?
-
`printf` continues printing characters until it finds some random `'\0'` character in memory
## Strings
- Very simple string operations now become complicated...
-
Suppose I want to concatenate two strings:
```c
char s1[20] = "hello";
char s2[20] = "world";
char s3[20] = ??? // how do I concatenate s1 and s2?
```
-
Manually copy all the characters into `s3`...
## Strings
```c
char s1[20] = "hello";
char s2[20] = "world";
char s3[20];
int j = 0; // keeps track of the next spot in s3
for (int i = 0; s1[i] != '\0'; i++) {
s3[j] = s1[i];
j++;
}
for (int i = 0; s2[i] != '\0'; i++) {
s3[j] = s2[i];
j++;
}
s3[j] = '\0';
```
## Strings
- Luckily, there is a `string.h` library with a few helpful functions that does some of this for us, but they just do the same thing as above
```c
#include