\chapter{The C Programming Language}

\epigraph{If you want to teach systems, don't drum up the programmers, sort the issues, and make PRs. Instead, teach them to yearn for the vast and endless C.}{Antoine de Saint-Exup\'{e}ry (With edits)}

Note: This chapter is long and goes into a lot of detail. Feel free to gloss over parts with which you have experience in.

C is the de-facto programming language to do serious system serious programming.
Why?
Most kernels have their API accessible through C.
The Linux kernel \cite{Love} and the XNU kernel \cite{xnukernel} of which MacOS is based on are written in C and have C API - Application Programming Interface.
The Windows Kernel uses C++, but doing system programming on that is much harder on windows that UNIX for novice system programmers.
C doesn't have abstractions like classes and Resource Acquisition Is Initialization (RAII) to clean up memory.
C also gives you much more of an opportunity to shoot yourself in the foot, but it lets you do things at a much more fine-grained level.

\input{introc/history_of_c.tex}
\input{introc/crash_course_introduction_to_c.tex}
\input{introc/language_facilities.tex}
\input{introc/the_c_and_linux.tex}
\input{introc/common_c_functions.tex}
\input{introc/c_memory_model.tex}
\input{introc/pointers.tex}
\input{introc/common_bugs.tex}
\input{introc/logic_and_program_flow_mistakes.tex}

\section{Topics}

\begin{itemize}
	\tightlist
	\item
	      C-strings representation
	\item
	      C-strings as pointers
	\item
	      char p{[}{]}vs char* p
	\item
	      Simple C string functions (strcmp, strcat, strcpy)
	\item
	      sizeof char
	\item
	      sizeof x vs x*
	\item
	      Heap memory lifetime
	\item
	      Calls to heap allocation
	\item
	      Dereferencing pointers
	\item
	      Address-of operator
	\item
	      Pointer arithmetic
	\item
	      String duplication
	\item
	      String truncation
	\item
	      double-free error
	\item
	      String literals
	\item
	      Print formatting.
	\item
	      memory out of bounds errors
	\item
	      static memory
	\item
	      file input / output. POSIX vs. C library
	\item
	      C input output: fprintf and printf
	\item
	      POSIX file IO (read, write, open)
	\item
	      Buffering of stdout
\end{itemize}

\section{Questions/Exercises}

\begin{itemize}
	\item
	      What does the following print out?

	      \begin{lstlisting}[language=C]
int main(){
fprintf(stderr, "Hello ");
fprintf(stdout, "It's a small ");
fprintf(stderr, "World\n");
fprintf(stdout, "place\n");
return 0;
}
\end{lstlisting}
	\item
	      What are the differences between the following two declarations? What
	      does \keyword{sizeof} return for one of them?

	      \begin{lstlisting}[language=C]
char str1[] = "first one";
char *str2 = "another one";
\end{lstlisting}
	\item
	      What is a string in C?
	\item
	      Code up a simple \keyword{my\_strcmp}. How about \keyword{my\_strcat}, \keyword{my\_strcpy}, or \keyword{my\_strdup}? Bonus: Code the functions while only going through the strings \emph{once}.
	\item
	      What should each of the following lines usually return?

	      \begin{lstlisting}[language=C]
int *ptr;
sizeof(ptr);
sizeof(*ptr);
\end{lstlisting}
	\item
	      What is \keyword{malloc}? How is it different from \keyword{calloc}. Once memory is allocated how can we use \keyword{realloc}?
	\item
	      What is the \keyword{\&} operator? How about \keyword{*}?
	\item
	      Pointer Arithmetic. Assume the following addresses. What are the following shifts?

	      \begin{lstlisting}[language=C]
char** ptr = malloc(10); //0x100
ptr[0] = malloc(20); //0x200
ptr[1] = malloc(20); //0x300
\end{lstlisting}

	      \begin{itemize}
		      \tightlist
		      \item
		            \keyword{ptr\ +\ 2}
		      \item
		            \keyword{ptr\ +\ 4}
		      \item
		            \keyword{ptr{[}0{]}\ +\ 4}
		      \item
		            \keyword{ptr{[}1{]}\ +\ 2000}
		      \item
		            \keyword{*((int)(ptr\ +\ 1))\ +\ 3}
	      \end{itemize}
	\item
	      How do we prevent double free errors?
	\item
	      What is the printf specifier to print a string, \keyword{int}, or
	      \keyword{char}?
	\item
	      Is the following code valid? Why? Where is \keyword{output}
	      located?

	      \begin{lstlisting}[language=C]
char *foo(int var){
static char output[20];
snprintf(output, 20, "%d", var);
return output;
}
\end{lstlisting}
	\item
	      Write a function that accepts a path as a string, and opens that file, prints the file contents 40 bytes at a time but, every other print reverses the string (try using the POSIX API for this).
	\item
	      What are some differences between the POSIX file descriptor model and C's \keyword{FILE*} (i.e. what function calls are used and which is buffered)? Does POSIX use C's \keyword{FILE*} internally or vice versa?
\end{itemize}

\section{Rapid Fire: Pointer Arithmetic}

Pointer arithmetic is important!
Take a deep breath and figure out how many bytes each operation moves a pointer.
The following is a rapid fire section.
We'll use the following definitions:

\begin{lstlisting}[language=C]
int *int_; // sizeof(int) == 4;
long *long_; // sizeof(long) == 8;
char *char_;
int *short_; //sizeof(short) == 2;
int **int_ptr; // sizeof(int*) == 8;
\end{lstlisting}

How many bytes are moved over from the following additions?

\begin{enumerate}
\item \keyword{int\_ + 1}
\item \keyword{long\_ + 7}
\item \keyword{short\_ - 6}
\item \keyword{short\_ - sizeof(long)}
\item \keyword{long\_ - sizeof(long) + sizeof(int\_)}
\item \keyword{long\_ - sizeof(long) / sizeof(int)}
\item \keyword{(char*)(int\_ptr + sizeof(long)) + sizeof(int\_)}
\end{enumerate}

\subsection{Rapid Fire Solutions}

\begin{enumerate}
\item 4
\item 56
\item -12
\item -16
\item 0
\item -16
\item 72
\end{enumerate}

\bibliographystyle{plainnat}
\bibliography{introc/introc}
