notes/education/software development/ECE1400/C.md

75 lines
3.8 KiB
Markdown
Raw Normal View History

2024-09-04 21:13:35 +00:00
# Compilation Steps
2024-09-04 20:58:35 +00:00
1. Preprocessing: The preprocessor obeys commands that begin with #, also known as directives
2024-09-04 21:03:35 +00:00
Commands for the preprocessor are called directives. Directives begin with a pound sign, and they do not end with a semicolon.
2024-09-04 20:58:35 +00:00
2024-09-04 21:03:35 +00:00
Example:
```c
#include <stdio.h>
```
2024-09-04 20:58:35 +00:00
2. Compiling. A compiler translates then translates the program into machine instructions.
3. Linking: The generated objects are combined to create a complete executable.
The preprocessor is typically integrated with the compiler.
2024-09-04 21:13:35 +00:00
# Types
## Strings
A string literal is characters enclosed in double quotation marks.
A newline can be inserted using `\n`.
## Integers
An integer is a way to store a whole number. In C, integers are signed by default.
## Floats
A float is a decimal value. Slower arithmetic and inexact values are both drawbacks of using floats.
## Variables
A variable must be declared before it is assigned.
2024-09-04 21:18:36 +00:00
# Formatting specifiers
2024-09-11 18:21:34 +00:00
# Standard library
## Formatting specifiers
| Specifier | Function |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `%d` | Decimal representation: Display a value as a base 10 (hence the decimal) integer. |
| `%f` | Fixed point decimal representation. Specify the number of places to round to by adding a decimal and a number, eg `%.2f` would round to two decimal places. |
2024-09-11 18:26:34 +00:00
| `%e` | Exponential floating point number representation. |
| `%g` | Either fixed point or exponential representation, whichever has a more compact representation. |
2024-09-11 18:16:34 +00:00
For number formatting specifiers, the convention is as follows:
`%-a.bX`
`%`: Start of the formatting specifier
`-`: (optional) If included, justify value left in space. Otherwise, justify right in space
`a`: (optional) If included, the size of the field in characters.
`.`: Separator between `a` and `b`. Optional if `b` is not specified
`b`: The number of decimal places to round to
2024-09-11 18:21:34 +00:00
`X`: The type of format to use, and the end of the specifier. Use `d` for integer base 10 (decimal) representation, `f` for fixed point decimal, and `e` for exponential notation, and `g` to select between fixed point and exponential, whichever is shorter.
## `printf`
2024-09-11 18:26:34 +00:00
Used to write a string to stdout with the ability to format variables into the string.
Write a string to standard output. `f` indicates that it's a formatting string. The string will not include move the cursor to a newline, append `\n` to the end of the string to do so.
Printf accepts a variable number of arguments, the first argument is the formatting string, then following arguments are the arguments to be inserted into the string.
2024-09-11 18:31:34 +00:00
TODO: examples
2024-09-11 18:26:34 +00:00
## `scanf`
Read value(s) from stdin.
`scanf` is to stdin as `printf` is to stdout.
2024-09-11 18:31:34 +00:00
The format of the input is specified using [formatting specifiers](#Formatting%20specifiers), and all following arguments are pointers pointing to variables to update.
2024-09-11 18:36:34 +00:00
2024-09-11 18:31:34 +00:00
### Examples
2024-09-11 18:36:34 +00:00
```c
// Read a float from standard input into the variable `v`.
float v;
// Here, `v` is uninitialized
scanf("%f", &v);
printf("You input: %f", v);
```
2024-09-11 18:31:34 +00:00
2024-09-11 18:36:34 +00:00
### Behavior
The validity of a `scanf` call is not necessarily checked at compile time, and so the number of outputs specified should match the number of inputs.
2024-09-11 18:31:34 +00:00
2024-09-11 18:36:34 +00:00
For each formatting specifier specified in the string, `scanf` will attempt to locate an appropriate value in the input, skipping whitespace and newlines if necessary. As `scanf`