mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-11 01:18:27 +00:00
Merge branch 'main' of https://github.com/janishutz/eth-summaries
This commit is contained in:
29
.github/CONTRIBUTING.md
vendored
Normal file
29
.github/CONTRIBUTING.md
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# Contributing
|
||||
Coding style is not fixed (yet). There are plans to add automatic formatting later down the line.
|
||||
If you are interested in getting that going, feel free to open a Pull Request with an actions setup that works with latexindent and my latexindent config from [here](https://git.janishutz.com/janishutz/dotfiles/src/branch/main/config/lint/latexfmt.yaml)
|
||||
|
||||
## Updating existing summaries
|
||||
- Do replace the helper files
|
||||
- Compile before uploading such that the PDFs are up-to-date
|
||||
- (Optional) Add your name to the credits for the summary
|
||||
- Open a pull request
|
||||
|
||||
## Adding a new summary
|
||||
- include all the `.tex` files that are needed to compile the latex document.
|
||||
- include any images or other files that are necessary to compile
|
||||
- Compile the document
|
||||
- (Optional) Configure auto-upload. To add a secret, please let us know that you want to do that in the PR and you will be given access to add secrets
|
||||
|
||||
## What we will accept
|
||||
- Summaries written with my helpers / styling
|
||||
- Summaries written using your own styling
|
||||
- Summaries for electives in the field of CS
|
||||
- Summaries for all subjects of BSc and MSc Computer Science at ETHZ
|
||||
|
||||
## What will not be accepted
|
||||
- Summaries with many obvious errors
|
||||
- Summaries not written in any TeX distribution or Typst
|
||||
- Summaries that do not compile on our end
|
||||
- Summaries for subjects that are not offered at ETHZ in BSc and MSc Computer Science
|
||||
- Summaries not following common design practices for LaTeX
|
||||
- Summaries for GESS subjects
|
||||
14
.github/ISSUE_TEMPLATE/error-report.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE/error-report.md
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
name: Error report
|
||||
about: Create a report to help us improve
|
||||
title: 'Error: '
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the error**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**Correct content**
|
||||
What the content should have been
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
32
.github/PULL_REQUEST_TEMPLATE/new-summary.md
vendored
Normal file
32
.github/PULL_REQUEST_TEMPLATE/new-summary.md
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
## New Summary
|
||||
<!--
|
||||
Thank you!
|
||||
-->
|
||||
|
||||
|
||||
## Link to VVZ
|
||||
|
||||
https://vvz.ethz.ch
|
||||
|
||||
## Additional information
|
||||
|
||||
Any information you consider worth mentioning
|
||||
|
||||
|
||||
## Checklist
|
||||
|
||||
<!--
|
||||
Put an `x` in the boxes that apply.
|
||||
-->
|
||||
|
||||
- [ ] The summary is compiled.
|
||||
- [ ] All relevant files for compilation are included.
|
||||
- [ ] There are no generated files included. Ignore new generated files in the `gitignore` file if needed.
|
||||
- [ ] I have added a link to the summary in the README file
|
||||
- [ ] The above link works
|
||||
|
||||
|
||||
## ComSol
|
||||
|
||||
- [ ] I want my summary auto-updated on ComSol
|
||||
- [ ] I already have set up a document there
|
||||
24
.github/PULL_REQUEST_TEMPLATE/update-summary.md
vendored
Normal file
24
.github/PULL_REQUEST_TEMPLATE/update-summary.md
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
## Updated summary
|
||||
<!--
|
||||
Thank you!
|
||||
|
||||
Please make sure that you have roughly followed the style of the rest of the document
|
||||
-->
|
||||
|
||||
|
||||
## Additional information
|
||||
|
||||
Any information you consider worth mentioning
|
||||
|
||||
|
||||
## Checklist
|
||||
|
||||
<!--
|
||||
Put an `x` in the boxes that apply.
|
||||
-->
|
||||
|
||||
- [ ] The summary is compiled.
|
||||
- [ ] All relevant files for compilation are included.
|
||||
- [ ] There are no generated files included. Ignore new generated files in the `gitignore` file if needed.
|
||||
- [ ] The style of the document is roughly similar
|
||||
- [ ] I have not changed the helper import statement or reverted it before opening the PR
|
||||
128
CODE_OF_CONDUCT.md
Normal file
128
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,128 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, religion, or sexual identity
|
||||
and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the
|
||||
overall community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or
|
||||
advances of any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email
|
||||
address, without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official e-mail address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
https://support.janishutz.com/index.php?a=add&category=12.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series
|
||||
of actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or
|
||||
permanent ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within
|
||||
the community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.0, available at
|
||||
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
||||
|
||||
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
||||
enforcement ladder](https://github.com/mozilla/diversity).
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
https://www.contributor-covenant.org/faq. Translations are available at
|
||||
https://www.contributor-covenant.org/translations.
|
||||
@@ -13,4 +13,5 @@ int main( int argc, char *argv[] ) {
|
||||
printf( "Arg %d: %s\n", i, argv[ i ] ); // Outputs the i-th argument from CLI
|
||||
|
||||
get_user_input_int( "Select a number" ); // Function calls as in any other language
|
||||
return 0; // Return a POSIX exit code
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
#include "01_func.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int get_user_input_int( char prompt[] ) {
|
||||
int input_data;
|
||||
printf( "%s", prompt ); // Always wrap strings like this for printf
|
||||
scanf( "%d", &input_data ); // Get user input from CLI
|
||||
printf( "%s", prompt ); // Always wrap strings like this for printf
|
||||
scanf( "%d", &input_data ); // Get user input from CLI
|
||||
int input_data_copy = input_data; // Value copied
|
||||
|
||||
// If statements just like any other language
|
||||
if ( input_data )
|
||||
@@ -11,6 +13,7 @@ int get_user_input_int( char prompt[] ) {
|
||||
else
|
||||
printf( "Input is zero" );
|
||||
|
||||
// Switch statements just like in any other language
|
||||
switch ( input_data ) {
|
||||
case 5:
|
||||
printf( "You win!" );
|
||||
@@ -21,17 +24,20 @@ int get_user_input_int( char prompt[] ) {
|
||||
printf( "No win" ); // Case for any not covered input
|
||||
}
|
||||
|
||||
int input_data_copy = input_data;
|
||||
|
||||
while ( input_data > 1 ) {
|
||||
input_data -= 1;
|
||||
printf( "Hello World\n" );
|
||||
}
|
||||
|
||||
// Inversed while loop (executes at least once)
|
||||
do {
|
||||
input_data -= 1;
|
||||
printf( "Bye World\n" );
|
||||
if ( input_data_copy == 0 )
|
||||
goto this_is_a_label;
|
||||
} while ( input_data_copy > 1 );
|
||||
|
||||
this_is_a_label:
|
||||
printf( "Jumped to label" );
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
int my_int; // Allocates memory on the stack.
|
||||
// Variable is global (read / writable by entire program)
|
||||
static int my_local_int; // only available locally (in this file)
|
||||
extern const char *var; // Defined in some other file
|
||||
const int MY_CONST = 10; // constant (immutable), convention: SCREAM_CASE
|
||||
|
||||
enum { ONE, TWO } num; // Enum. ONE will get value 0, TWO has value 1
|
||||
|
||||
enum { O = 2, T = 1 } n; // Enum with values specified
|
||||
|
||||
// Structs are like classes, but contain no logic
|
||||
struct MyStruct {
|
||||
int el1;
|
||||
int el2;
|
||||
};
|
||||
|
||||
int fun( int j ) {
|
||||
static int i = 0; // Persists across calls of fun
|
||||
short my_var = 1; // Block scoped (deallocated when going out of scope)
|
||||
int my_var_dbl = (int) my_var; // Explicit casting (works between almost all types)
|
||||
return i;
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
if ( ( my_local_int = fun( 10 ) ) ) {
|
||||
// Every c statement is also an expression, i.e. you can do the above!
|
||||
}
|
||||
struct MyStruct test; // Allocate memory on stack for struct
|
||||
struct MyStruct *test_p = &test; // Pointer to memory where test resides
|
||||
test.el1 = 1; // Direct element access
|
||||
test_p->el2 = 2; // Via pointer
|
||||
return 0;
|
||||
}
|
||||
20
semester3/spca/code-examples/00_c/00_basics/03_arrays.c
Normal file
20
semester3/spca/code-examples/00_c/00_basics/03_arrays.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
int data[ 10 ]; // Initialize array of 10 integers
|
||||
data[ 5 ] = 5; // element 5 is now 5
|
||||
*data = 10; // element 0 is now 5
|
||||
printf( "%d\n", data[ 0 ] ); // print element 0 (prints 10)
|
||||
printf( "%d\n", *data ); // equivalent as above
|
||||
printf( "%d\n", data[ 5 ] ); // print element 5 (prints 5)
|
||||
printf( "%d\n", *( data + 5 ) ); // equivalent as above
|
||||
int multidim[ 5 ][ 5 ]; // 2-dimensional array
|
||||
// We can iterate over it using two for-loops
|
||||
int init_array[ 2 ][ 2 ] = {
|
||||
{1, 2},
|
||||
{3, 4}
|
||||
}; // We can initialize an array like this
|
||||
int empty_arr[ 4 ] = {}; // Initialized to 0
|
||||
return 0;
|
||||
}
|
||||
15
semester3/spca/code-examples/00_c/00_basics/04_strings.c
Normal file
15
semester3/spca/code-examples/00_c/00_basics/04_strings.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
char hello[ 6 ] = "hello"; // Using double quotes
|
||||
char world[ 6 ] = { 'w', 'o', 'r', 'l', 'd', '\0' }; // As array
|
||||
|
||||
char src[ 12 ], dest[ 12 ];
|
||||
strncpy( src, "ETHZ", 12 ); // Copy strings (extra elements will be set to \0)
|
||||
strncpy( dest, src, 12 ); // Copy strings (last arg is first n chars to copy)
|
||||
if ( strncmp( src, dest, 12 ) ) // Compare two strings. Returns 1 if src > dest
|
||||
printf( "Hello World" );
|
||||
strncat( dest, " is in ZH", 12 ); // Concatenate strings
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define FOO BAZ
|
||||
#define BAR( x ) ( x + 3 )
|
||||
#define SKIP_SPACES( p ) \
|
||||
do { \
|
||||
while ( p > 0 ) { p--; } \
|
||||
} while ( 0 )
|
||||
#define COMMAND( c ) { #c, c##_command } // Produces { "<val(c)>", "<val(c)>_command" }
|
||||
|
||||
#ifdef FOO // If macro is defined, ifndef for if not defined
|
||||
#define COURSE "SPCA"
|
||||
#else
|
||||
#define COURSE "Systems Programming and Computer Architecture"
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
#define OUT HELLO // if statement
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] ) {
|
||||
int i = 10;
|
||||
SKIP_SPACES( i );
|
||||
|
||||
printf( "%s", COURSE );
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -13,3 +13,5 @@ This of course leads to \texttt{C} performing excellently and there are many pro
|
||||
but instead optimized \texttt{C} code that is then compiled into machine code using a \texttt{C} compiler.
|
||||
This has a number of benefits, most notably that \texttt{C} compilers can produce very efficient assembly,
|
||||
as lots of effort is put into the \texttt{C} compilers by the hardware manufacturers.
|
||||
|
||||
There are many great \lC\ tutorials out there, a simple one (as for many other languages too) can be found \hlhref{https://www.w3schools.com/c/index.php}{here}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
\subsection{The Syntax}
|
||||
\subsection{Basics}
|
||||
\texttt{C} uses a very similar syntax as many other programming languages, like \texttt{Java}, \texttt{JavaScript} and many more\dots
|
||||
to be precise, it is \textit{them} that use the \texttt{C} syntax, not the other way around. So:
|
||||
\inputcodewithfilename{c}{code-examples/00_c/}{00_intro.c}
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{00_intro.c}
|
||||
|
||||
In \texttt{C} we are referring to the implementation of a function as a \bi{(function) definition} (correspondingly, \textit{variable definition}, if the variable is initialized)
|
||||
and to the definition of the function signature (or variables, without initializing them) as the \bi{(function) declaration} (or, correspondingly, \textit{variable declaration}).
|
||||
|
||||
\texttt{C} code is usuallt split into the source files, ending in \texttt{.c} (where the local functions and variables are declared, as well as all function definitions)
|
||||
and the header files, ending in \texttt{.h}, where the external declarations are defined. Usually, no definition of functions are in the \texttt{.h} files
|
||||
\inputcodewithfilename{c}{code-examples/00_c/}{01_func.h}
|
||||
|
||||
\inputcodewithfilename{c}{code-examples/00_c/}{01_func.c}
|
||||
and the header files, ending in \texttt{.h}, usually sharing the filename of the source file, where the external declarations are defined.
|
||||
By convention, no definition of functions are in the \texttt{.h} files, and neither variables, but there is nothing preventing you from putting them there.
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{01_func.h}
|
||||
14
semester3/spca/parts/00_c/01_basics/01_control-flow.tex
Normal file
14
semester3/spca/parts/00_c/01_basics/01_control-flow.tex
Normal file
@@ -0,0 +1,14 @@
|
||||
\newpage
|
||||
\subsubsection{Control Flow}
|
||||
Many of the control-flow structures of \texttt{C} can be found in the below code snippet.
|
||||
A note of caution when using goto: It is almost never a good idea (can lead to unexpected behaviour, is hard to maintain, etc).
|
||||
Where it however is very handy is for error recovery (and cleanup functions) and early termination of multiple loops (jumping out of a loop).
|
||||
So, for example, if you have to run multiple functions to set something up and one of them fails,
|
||||
you can jump to a label and have all cleanup code execute that you have specified there.
|
||||
And because the labels are (as in Assembly) simply skipped over during execution, you can make very nice cleanup code.
|
||||
We can also use \texttt{continue} and \texttt{break} statements similarly to \texttt{Java}, they do not however accept labels.
|
||||
(Reminder: \texttt{continue} skips the loop body and goes to the next iteration)
|
||||
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{01_func.c}
|
||||
|
||||
|
||||
39
semester3/spca/parts/00_c/01_basics/02_declarations.tex
Normal file
39
semester3/spca/parts/00_c/01_basics/02_declarations.tex
Normal file
@@ -0,0 +1,39 @@
|
||||
\newpage
|
||||
\subsubsection{Declarations}
|
||||
We have already seen a few examples for how \texttt{C} handles declarations.
|
||||
In concept they are similar (and scoping works the same) to most other \texttt{C}-like programming languages, including \texttt{Java}.
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{02_declarations.c}
|
||||
|
||||
A peculiarity of \texttt{C} is that the bit-count is not defined by the language, but rather the hardware it is compiled for.
|
||||
\begin{fullTable}{llll}{\texttt{C} data type & typical 32-bit & ia32 & x86-64}{Comparison of byte-sizes for each datatype on different architectures}
|
||||
\texttt{char} & 1 & 1 & 1 \\
|
||||
\texttt{short} & 2 & 2 & 2 \\
|
||||
\texttt{int} & 4 & 4 & 4 \\
|
||||
\texttt{long} & 4 & 4 & 8 \\
|
||||
\texttt{long long} & 8 & 8 & 8 \\
|
||||
\texttt{float} & 4 & 4 & 4 \\
|
||||
\texttt{double} & 4 & 8 & 8 \\
|
||||
\texttt{long double} & 8 & 10/12 & 16 \\
|
||||
\end{fullTable}
|
||||
|
||||
\drmvspace
|
||||
By default, integers in \lC\ are \texttt{signed}, to declare an unsigned integer, use \texttt{unsigned int}.
|
||||
Since it is hard and annoying to remember the number of bytes that are in each data type, \texttt{C99} has introduced the extended integer types,
|
||||
which can be imported from \texttt{stdint.h} and are of form \texttt{int<bit count>\_t} and \texttt{uint<bit count>\_t},
|
||||
where we substitute the \texttt{<bit count>} with the number of bits (have to correspond to a valid type of course).
|
||||
|
||||
Another notable difference of \texttt{C} compared to other languages is that \texttt{C} doesn't natively have a \texttt{boolean} type,
|
||||
by convention a \texttt{short} is used to represent it, where any non-zero value means \texttt{true} and \texttt{0} means \texttt{false}.
|
||||
Since boolean types are quite handy, the \texttt{!} syntax for negation turns any non-zero value of any integer type into zero and vice-versa.
|
||||
\texttt{C99} has added support for a bool type via \texttt{stdbool.h}, which however is still an integer.
|
||||
|
||||
Notably, \texttt{C} doesn't have a very rigid type system and lower bit-count types are implicitly cast to higher bit-count data types, i.e.
|
||||
if you add a \texttt{short} and an \texttt{int}, the \texttt{short} is cast to \texttt{short} (bits 16-31 are set to $0$) and the two are added.
|
||||
Explicit casting between almost all types is also supported.
|
||||
Some will force a change of bit representation, but most won't (notably, when casting to and from \texttt{float}-like types, minus to \texttt{void})
|
||||
|
||||
Another important feature is that every \lC\ statement is also an expression, see above code block for example.
|
||||
|
||||
The \texttt{void} type has \bi{no} value and is used for untyped pointers and declaring functions with no return value
|
||||
|
||||
It is also possible to define a custom type using \texttt{typedef <type it represents> <name of the new type>}
|
||||
45
semester3/spca/parts/00_c/01_basics/03_operators.tex
Normal file
45
semester3/spca/parts/00_c/01_basics/03_operators.tex
Normal file
@@ -0,0 +1,45 @@
|
||||
\subsubsection{Operators}
|
||||
The list of operators in \lC\ is similar to the one of \texttt{Java}, etc.
|
||||
In Table \ref{tab:c-operators}, you can see an overview of the operators, sorted by precedence in descending order.
|
||||
You may notice that the \verb|&| and \verb|*| operators appear twice. The higher precedence occurrence is the address operator and dereference, respectively,
|
||||
and the lower precedence is \texttt{bitwise and} and \texttt{multiplication}, respectively.
|
||||
|
||||
Very low precedence belongs to boolean operators \verb|&&| and \texttt{||}, as well as the ternary operator and assignment operators
|
||||
\begin{table}[h!]
|
||||
\begin{tables}{ll}{Operator & Associativity}
|
||||
\texttt{() [] -> .} & Left-to-right \\
|
||||
\verb|! ~ ++ -- + - * & (type) sizeof| & Right-to-left \\
|
||||
\verb|* / %| & Left-to-right \\
|
||||
\verb|+ -| & Left-to-right \\
|
||||
\verb|<< >>| & Left-to-right \\
|
||||
\verb|< <= >= >| & Left-to-right \\
|
||||
\verb|== !=| & Left-to-right \\
|
||||
\verb|&| (logical and) & Left-to-right \\
|
||||
\verb|^| (logical xor) & Left-to-right \\
|
||||
\texttt{|} (logical or) & Left-to-right \\
|
||||
\verb|&&| (boolean and) & Left-to-right \\
|
||||
\texttt{||} (boolean or) & Left-to-right \\
|
||||
\texttt{? :} (ternary) & Right-to-left \\
|
||||
\verb|= += -= *= /= %= &= ^=||\verb|= <<= >>=| & Right-to-left \\
|
||||
\verb|,| & Left-to-right \\
|
||||
\end{tables}
|
||||
\caption{\lC\ operators ordered in descending order by precedence}
|
||||
\label{tab:c-operators}
|
||||
\end{table}
|
||||
|
||||
\shade{blue}{Associativity}
|
||||
\begin{itemize}
|
||||
\item Left-to-right: $A + B + C \mapsto (A + B) + C$
|
||||
\item Right-to-left: \texttt{A += B += C} $\mapsto$ \texttt{(A += B) += C}
|
||||
\end{itemize}
|
||||
|
||||
As it should be, boolean and, as well as boolean or support early termination.
|
||||
|
||||
The ternary operator works as in other programming languages \verb|result = expr ? res_true : res_false;|
|
||||
|
||||
As previously touched on, every statement is also an expression, i.e. the following works
|
||||
\mint{c}|printf("%s", x = foo(y)); // prints output of foo(y) and x has that value|
|
||||
|
||||
Pre-increment (\texttt{++i}, new value returned) and post-increment (\texttt{i++}, old value returned) are also supported by \lC.
|
||||
|
||||
\lC\ has an \texttt{assert} statement, but do not use it for error handling. The basic syntax is \texttt{assert( expr );}
|
||||
7
semester3/spca/parts/00_c/01_basics/04_arrays.tex
Normal file
7
semester3/spca/parts/00_c/01_basics/04_arrays.tex
Normal file
@@ -0,0 +1,7 @@
|
||||
\newpage
|
||||
\subsubsection{Arrays}
|
||||
\lC\ compiler does not do any array bound checks! Thus, always check array bounds.
|
||||
Unlike some other programming languages, arrays are \bi{not} dynamic length.
|
||||
|
||||
The below snippet includes already some pointer arithmetic tricks. The variable \texttt{data} is a pointer to the first element of the array.
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{03_arrays.c}
|
||||
6
semester3/spca/parts/00_c/01_basics/05_strings.tex
Normal file
6
semester3/spca/parts/00_c/01_basics/05_strings.tex
Normal file
@@ -0,0 +1,6 @@
|
||||
\subsubsection{Strings}
|
||||
\lC\ doesn't have a \texttt{string} data type, but rather, strings are represented (when using \texttt{ASCII}) as \texttt{char} arrays,
|
||||
with length of the array $n + 1$ (where $n$ is the number of characters of the string).
|
||||
The extra element is the termination character, called the \texttt{null character}, denoted \verb|\0|.
|
||||
To determine the actual length of the string (as it may be padded), we can use \verb|strnlen(str, maxlen)| from \texttt{string.h}
|
||||
\inputcodewithfilename{c}{code-examples/00_c/00_basics/}{04_strings.c}
|
||||
39
semester3/spca/parts/00_c/01_basics/06_integers.tex
Normal file
39
semester3/spca/parts/00_c/01_basics/06_integers.tex
Normal file
@@ -0,0 +1,39 @@
|
||||
\subsubsection{Integers in C}
|
||||
As a reminder, integers are encoded as follows in big endian notation, with $x_i$ being the $i$-th bit and $w$ being the number of bits used to represent the number:
|
||||
\begin{itemize}[noitemsep]
|
||||
\item \bi{Unsigned}: $\displaystyle \sum_{i = 0}^{w - 1} x_i \cdot 2^i$
|
||||
\item \bi{Signed}: $\displaystyle -x_{w - 1} \cdot 2^{w - 1} + \sum_{i = 0}^{w - 1} x_i \cdot 2^i$ (two's complement notation, with $x_{w - 1}$ being the sign-bit)
|
||||
\end{itemize}
|
||||
The minimum number representable is $0$ and $-2^{w - 1}$, respectively, whereas the maximum number representable is $2^w - 1$ and $2^{w - 1} - 1$.
|
||||
\verb|limits.h| defines constants for the minimum and maximum values of different types, e.g. \verb|ULONG_MAX| or \verb|LONG_MAX| and \verb|LONG_MIN|
|
||||
|
||||
We can use the shift operators to multiply and divide by two. Shift operations are usually \textit{much} cheaper than multiplication and division.
|
||||
Left shift (\texttt{u << k} in \lC) always fills with zeros and throws away the extra bits on the left (equivalent to multiplication by $2^k$),
|
||||
whereas right shift (\texttt{u >> k} in \lC) is implementation-defined,
|
||||
either arithmetic (fill with most significant bit, division by $2^k$. This however rounds incorrectly, see below)
|
||||
or logical shift (fill with zeros, unsigned division by $2^k$).
|
||||
|
||||
Signed division using arithmetic right shifts has the issue of incorrect rounding when number is $< 0$.
|
||||
Instead, we represent $s / 2^k = s + (2^k - 1) \texttt{ >> } k$ for $s < 0$ and $s / 2^k = s >> k$ for $s > 0$
|
||||
|
||||
\bi{In expressions, signed values are implicitly cast to unsigned}
|
||||
|
||||
This can lead to all sorts of nasty exploits (e.g. provide $-1$ as the argument to \texttt{memcpy} and watch it burn, this was an actual exploit in FreeBSD)
|
||||
|
||||
\fhlc{Cyan}{Addition \& Subtraction}
|
||||
|
||||
A nice property of the two's complement notation is that addition and subtraction works exactly the same as in normal notation, due to over- and underflow.
|
||||
This also obviously means that it implements modular arithmetic, i.e.
|
||||
\mrmvspace
|
||||
\begin{align*}
|
||||
\texttt{Add}_w (u, v) = u + v \text{ mod } 2^w \ \text{ and } \ \texttt{Sub}_w (u, v) = u - v \text{ mod } 2^w
|
||||
\end{align*}
|
||||
|
||||
\mrmvspace
|
||||
\fhlc{Cyan}{Multiplication \& Division}
|
||||
|
||||
Unsigned multiplication with addition forms a commutative ring.
|
||||
Again, it is doing modular arithmetic and
|
||||
\begin{align*}
|
||||
\texttt{UMult}_w (u, v) = u \cdot v \text{ mod } 2^w
|
||||
\end{align*}
|
||||
1
semester3/spca/parts/00_c/01_basics/07_pointers.tex
Normal file
1
semester3/spca/parts/00_c/01_basics/07_pointers.tex
Normal file
@@ -0,0 +1 @@
|
||||
\subsubsection{Pointers}
|
||||
28
semester3/spca/parts/00_c/02_preprocessor.tex
Normal file
28
semester3/spca/parts/00_c/02_preprocessor.tex
Normal file
@@ -0,0 +1,28 @@
|
||||
\newpage
|
||||
\subsection{The C preprocessor}
|
||||
To have \texttt{gcc} stop compiliation after running through \texttt{cpp}, the \texttt{C preprocessor}, use \texttt{gcc -E <file name>}.
|
||||
|
||||
Imports in \lC\ are handled by the preprocessor, that for each \verb|#include <file1.h>|, the preprocessor simply copies the contents of the file recursively into one file.
|
||||
|
||||
Depending on if we use \verb|#include <file1.h>| or \verb|#include "file1.h"| the preprocessor will search for the file either in the system headers or in the project directory.
|
||||
Be wary of including files twice, as the preprocessor will recursively include all files (i.e. it will include files from the files we included)
|
||||
|
||||
The \lC\ preprocessor gives us what are called \texttt{preprocessor macros}, which have the format \verb|#define NAME SUBSTITUTION|.
|
||||
\rmvspace
|
||||
|
||||
\inputcodewithfilename{c}{code-examples/00_c/01_preprocessor/}{00_macros.c}
|
||||
|
||||
To avoid issues with semicolons at the end of preprocessor macros that wrap statements that cannot end in semicolons, we can use a concept called semicolon swallowing.
|
||||
For that, we wrap the statements in a \texttt{do \dots\ while(0)} loop, which is removed by the compiler on compile, also taking with it the semicolon.
|
||||
|
||||
There are also a number of predefined macros:
|
||||
\begin{itemize}[noitemsep]
|
||||
\item \verb|__FILE__|: Filename of processed file
|
||||
\item \verb|__LINE__|: Line number of this usage of macro
|
||||
\item \verb|__DATE__|: Date of processing
|
||||
\item \verb|__TIME__|: Time of processing
|
||||
\item \verb|__STDC__|: Set if ANSI Standard \lC\ compiler is used
|
||||
\item \verb|__STDC_VERSION__|: The version of Standard \lC\ being compiled
|
||||
\item \dots many more
|
||||
\end{itemize}
|
||||
In headers, we typically use \verb|#ifndef __FILENAME_H_| followed by a \verb|#define __FILENAME_H_| or the like to check if the header was already included before
|
||||
0
semester3/spca/parts/00_c/05_toolchain/00_intro.tex
Normal file
0
semester3/spca/parts/00_c/05_toolchain/00_intro.tex
Normal file
Binary file not shown.
@@ -7,6 +7,8 @@
|
||||
\usepackage{lmodern}
|
||||
\setFontType{sans}
|
||||
|
||||
\newcommand{\lC}{\texttt{C}}
|
||||
|
||||
\begin{document}
|
||||
\startDocument
|
||||
\usetcolorboxes
|
||||
@@ -58,7 +60,15 @@
|
||||
\newsection
|
||||
\section{The C Programming Language}
|
||||
\input{parts/00_c/00_intro.tex}
|
||||
\input{parts/00_c/01_syntax.tex}
|
||||
\input{parts/00_c/01_basics/00_intro.tex}
|
||||
\input{parts/00_c/01_basics/01_control-flow.tex}
|
||||
\input{parts/00_c/01_basics/02_declarations.tex}
|
||||
\input{parts/00_c/01_basics/03_operators.tex}
|
||||
\input{parts/00_c/01_basics/04_arrays.tex}
|
||||
\input{parts/00_c/01_basics/05_strings.tex}
|
||||
\input{parts/00_c/01_basics/06_integers.tex}
|
||||
\input{parts/00_c/01_basics/07_pointers.tex}
|
||||
\input{parts/00_c/02_preprocessor.tex}
|
||||
|
||||
|
||||
% ── Intro to x86 asm ────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user