\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{prewd6} \pdfinfo{ /Title (c-exam.pdf) /Creator (Cheatography) /Author (prewd6) /Subject (C Exam Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{0B0BA3} \definecolor{LightBackground}{HTML}{F7F7FC} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C Exam Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{prewd6} via \textcolor{DarkBackground}{\uline{cheatography.com/51151/cs/14045/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}prewd6 \\ \uline{cheatography.com/prewd6} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 19th December, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{4} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{recursive functions, parameter passing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{recursive functions, parameter passing \newline /{\emph{ \newline }} Recursive descent parser for simple C expressions. \newline {\emph{ Very little error checking. \newline }}/ \newline \newline \#include \textless{}stdio.h\textgreater{} \newline \#include \textless{}stdlib.h\textgreater{} \newline \newline int expr(void); \newline int mul\_exp(void); \newline int unary\_exp(void); \newline int primary(void); \newline \newline main()\{ \newline int val; \newline \newline for(;;)\{ \newline printf("expression: "); \newline val = expr(); \newline if(getchar() != '\textbackslash{}n')\{ \newline printf("error\textbackslash{}n"); \newline while(getchar() != '\textbackslash{}n') \newline ; /{\emph{ NULL }}/ \newline \} else\{ \newline printf("result is \%d\textbackslash{}n", val); \newline \} \newline \} \newline exit(EXIT\_SUCCESS); \newline \} \newline \newline int \newline expr(void)\{ \newline int val, ch\_in; \newline \newline val = mul\_exp(); \newline for(;;)\{ \newline switch(ch\_in = getchar())\{ \newline default: \newline ungetc(ch\_in,stdin); \newline return(val); \newline case '+': \newline val = val + mul\_exp(); \newline break; \newline case '-': \newline val = val - mul\_exp(); \newline break; \newline \} \newline \} \newline \} \newline \newline int \newline mul\_exp(void)\{ \newline int val, ch\_in; \newline \newline val = unary\_exp(); \newline for(;;)\{ \newline switch(ch\_in = getchar())\{ \newline default: \newline ungetc(ch\_in, stdin); \newline return(val); \newline case '{\emph{': \newline val = val }} unary\_exp(); \newline break; \newline case '/': \newline val = val / unary\_exp(); \newline break; \newline case '\%': \newline val = val \% unary\_exp(); \newline break; \newline \} \newline \} \newline \} \newline \newline int \newline unary\_exp(void)\{ \newline int val, ch\_in; \newline \newline switch(ch\_in = getchar())\{ \newline default: \newline ungetc(ch\_in, stdin); \newline val = primary(); \newline break; \newline case '+': \newline val = unary\_exp(); \newline break; \newline case '-': \newline val = -unary\_exp(); \newline break; \newline \} \newline return(val); \newline \} \newline \newline int \newline primary(void)\{ \newline int val, ch\_in; \newline \newline ch\_in = getchar(); \newline if(ch\_in \textgreater{}= '0' \&\& ch\_in \textless{}= '9')\{ \newline val = ch\_in - '0'; \newline goto out; \newline \} \newline if(ch\_in == '(')\{ \newline val = expr(); \newline getchar(); /{\emph{ skip closing ')' }}/ \newline goto out; \newline \} \newline printf("error: primary read \%d\textbackslash{}n", ch\_in); \newline exit(EXIT\_FAILURE); \newline out: \newline return(val); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{malloc() and free()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Malloc() is used to allocate a certain amount of memory during the execution of a program. It requests a block of memory from the heap, if the request is granted the operating system will reserve the amount of memory. When the amount of memory is not needed anymore you must return it to the operating system by calling the free() function. \newline \#include\textless{}stdio.h\textgreater{} \newline int main() \newline \{ \newline int {\emph{ptr\_one; \newline ptr\_one = (int }})malloc(sizeof(int)); \newline if (ptr\_one == 0) \newline \{ \newline printf("ERROR: Out of memory\textbackslash{}n"); \newline return 1; \newline \} \newline {\emph{ptr\_one = 25; \newline printf("\%d\textbackslash{}n", }}ptr\_one); \newline free(ptr\_one); \newline return 0; \newline \} \newline The malloc statement will ask of an amount of memory with size of an integer (32 bits or 4 bytes) If there is not enough memory available the malloc function will return a NULL If the request is granted the address of the reserved block will be placed into the pointer variable. \newline \#include\textless{}stdio.h\textgreater{} \newline typedef struct rec \newline \{ \newline int i; \newline float PI; \newline char A; \newline \}RECORD; \newline int main() \newline \{ \newline RECORD {\emph{ptr\_one; \newline ptr\_one = (RECORD }}) malloc (sizeof(RECORD)); \newline ({\emph{ptr\_one).i = 10; \newline (}}ptr\_one).PI = 3.14; \newline ({\emph{ptr\_one).A = 'a'; \newline printf("First value: \%d\textbackslash{}n",(}}ptr\_one).i); \newline printf("Second value: \%f\textbackslash{}n", ({\emph{ptr\_one).PI); \newline printf("Third value: \%c\textbackslash{}n", (}}ptr\_one).A); \newline free(ptr\_one); \newline return 0; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{multiple inclusion protection}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{The basic use of header files is to provide symbol declarations for functions and globals. Because multiple declarations of a given symbol in a single translation unit are a syntax error, you have to defensively structure your header files to not redefine anything in case they are included multiple times. \newline Keep in mind that you just cannot prevent header files from being included more than once unless you were to forbid header files themselves from including other header files... and doing that would be suboptimal at best as we shall see in a future post on self-containment. \newline Just follow this pattern and encapsulate the whole contents of the whole header file within a guard: \newline \newline \#if !defined(PROJECT\_MODULE\_H) \newline \#define PROJECT\_MODULE\_H \newline ... all header file contents go here ... \newline \#endif /{\emph{ !defined(PROJECT\_MODULE\_H) }}/ \newline \newline properly scope the guard names. These names must be unique within your project and within any project that may ever include them. Therefore, it is good practice to always prefix your guard names with the name of your project and follow them by the name of the module. Compilers expect the structure above in order to apply optimizations against multiple inclusions of a single file. If you break the pattern, you can unknowingly incur higher build times. \newline The exception \newline As with any rule there is an exception: not all header files can safely be included more than once. If a header file defines a static symbol or helper function, you have to ensure that it is not pulled in from more than one place. \newline Yes, the compiler would detect this on its own but, for readability purposes, your header file should explicitly state this fact. Use this other pattern instead: \newline \#if \seqsplit{defined(PROJECT\_MODULE\_H)} \newline \#error "Must only be included once and only from .c files" \newline \#endif \newline \#define PROJECT\_MODULE\_H \newline \newline ... all header file contents go here ... \newline But when can this happen? Very rarely, really. A specific case of the above would be a header file providing helper functions for testing, both their definitions and their implementation. Theoretically, you could split the two into a traditional header file and a source file, compile them separately and link them together with each test program you write. However, doing so may complicate your build unnecessarily. } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{comparator functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Standard C library provides qsort() that can be used for sorting an array. As the name suggests, the function uses QuickSort algorithm to sort the given array. Following is prototype of qsort() \newline void qsort (void{\emph{ base, size\_t num, size\_t size, \newline             int (}}comparator)(const void{\emph{,const void}})); \newline \newline The key point about qsort() is comparator function comparator. The comparator function takes two arguments and contains logic to decide their relative order in sorted output. The idea is to provide flexibility so that qsort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other). \newline The comparator function takes two pointers as arguments (both type-casted to const void{\emph{) and defines the order of the elements by returning (in a stable and transitive manner \newline int comparator(const void }}p, const void {\emph{q) \newline \{ \newline     int l = ((struct Student }})p)-\textgreater{}marks; \newline     int r = ((struct Student {\emph{)q)-\textgreater{}marks; \newline     return (l - r); \newline \} \newline // This function is used in qsort to decide the relative order \newline // of elements at addresses p and q. \newline int comparator(const void }}p, const void {\emph{q) \newline \{ \newline     // Get the values at given addresses \newline     int l = }}(const int {\emph{)p; \newline     int r = }}(const int {\emph{)q; \newline   \newline     // both odd, put the greater of two first. \newline     if ((l\&1) \&\& (r\&1)) \newline         return (r-l); \newline   \newline     // both even, put the smaller of two first \newline     if ( !(l\&1) \&\& !(r\&1) ) \newline         return (l-r); \newline   \newline     // l is even, put r first \newline     if (!(l\&1)) \newline         return 1; \newline   \newline     // l is odd, put l first \newline     return -1; \newline \} \newline   \newline // A utility function to print an array \newline void printArr(int arr{[}{]}, int n) \newline \{ \newline     int i; \newline     for (i = 0; i \textless{} n; ++i) \newline         printf("\%d ", arr{[}i{]}); \newline \} \newline   \newline // Driver program to test above function \newline int main() \newline \{ \newline     int arr{[}{]} = \{1, 6, 5, 2, 3, 9, 4, 7, 8\}; \newline   \newline     int size = sizeof(arr) / sizeof(arr{[}0{]}); \newline     qsort((void}})arr, size, sizeof(arr{[}0{]}), comparator); \newline   \newline     printf("Output array is\textbackslash{}n"); \newline     printArr(arr, size); \newline   \newline     return 0; \newline \} \newline Output: \newline Output array is \newline 9 7 5 3 1 2 4 6 8} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{unions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{\#include \textless{}stdio.h\textgreater{} \newline \#include \textless{}string.h\textgreater{} \newline \newline union Data \{ \newline int i; \newline float f; \newline char str{[}20{]}; \newline \}; \newline int main( ) \{ \newline union Data data; \newline printf( "Memory size occupied by data : \%d\textbackslash{}n", sizeof(data)); \newline return 0; \newline \} // returns: Memory size occupied by data : 20 \newline \newline \newline To access any member of a union, we use the member access operator (.). The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program − \newline \newline \#include \textless{}stdio.h\textgreater{} \newline \#include \textless{}string.h\textgreater{} \newline union Data \{ \newline int i; \newline float f; \newline char str{[}20{]}; \newline \}; \newline int main( ) \{ \newline union Data data; \newline data.i = 10; \newline data.f = 220.5; \newline strcpy( data.str, "C Programming"); \newline printf( "data.i : \%d\textbackslash{}n", data.i); \newline printf( "data.f : \%f\textbackslash{}n", data.f); \newline printf( "data.str : \%s\textbackslash{}n", data.str); \newline return 0; \newline \}//returns the following: data.i : 1917853763 \newline //data.f : \seqsplit{4122360580327794860452759994368}.000000 \newline //data.str : C Programming} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}