\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{Randomgirlll13} \pdfinfo{ /Title (c-programming-language-basics.pdf) /Creator (Cheatography) /Author (Randomgirlll13) /Subject (C Programming Language Basics 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}{2B42D6} \definecolor{LightBackground}{HTML}{F1F3FC} \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 Programming Language Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Randomgirlll13} via \textcolor{DarkBackground}{\uline{cheatography.com/186328/cs/39141/}}} \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}Randomgirlll13 \\ \uline{cheatography.com/randomgirlll13} \\ \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 17th August, 2023.\\ 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*}{3} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{General Knowledge}} \tn % Row 0 \SetRowColor{LightBackground} Syntax in C & All Statements in C must end with a semicolon ; \{\{nl\}\} keywords and other code elements are case-sensitive \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} Escape Sequences & In C, within a string the \textbackslash{}n will cut the string and move the part after to the next line. The \textbackslash{}t will tab the string after, giving it more space \tn % Row Count 11 (+ 6) % Row 2 \SetRowColor{LightBackground} Single Line Comments & //This is a single line comment within c \tn % Row Count 13 (+ 2) % Row 3 \SetRowColor{white} Block Comments & /*{\emph{ Block comments are nice for when you have a lot to say about a particular piece of code *}}/ \tn % Row Count 17 (+ 4) % Row 4 \SetRowColor{LightBackground} Compiling C Code & to compile c code, in a terminal you'll need to type in, minimally, gcc fileName.c \{\{nl\}\} this will output something call a.out which will allow you to run the program \{\{nl\}\} Alternatively, if you do gcc fileName.c -o fileName you can then run it using the denotation of filename instead \tn % Row Count 29 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Conditionals}} \tn % Row 0 \SetRowColor{LightBackground} if Statements & if(x == 3)\{ \{\{nl\}\} printf(x); \{\{nl\}\} \} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} else-if Statements & if(x == 3)\{ \{\{nl\}\} printf(x); \{\{nl\}\} \} else if (x \textless{} 3) \{ \{\{nl\}\} printf(x - 1); \{\{nl\}\} \} \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} else statements & if(x == 3)\{ \{\{nl\}\} printf(x); \{\{nl\}\} \} else if (x \textless{} 3) \{ \{\{nl\}\} printf(x - 1); \{\{nl\}\} \} else \{ \{\{nl\}\} print(0); \{\{nl\}\} \} \tn % Row Count 13 (+ 6) % Row 3 \SetRowColor{white} Ternary Operators: A condensed if-else statement & if (a \textless{} b) \{ \{\{nl\}\} min = a; \{\{nl\}\} \} else \{ \{\{nl\}\} min = b; \{\{nl\}\} \} \{\{nl\}\} \{\{nl\}\} min = a \textless{} b ? a : b; ((This is the ternary Operator for the above if) \{\{nl\}\} to briefly explain it, it essentially will set min to a if the a \textless{} b is true otherwise b, the second option, will be what min is set to \tn % Row Count 28 (+ 15) % Row 4 \SetRowColor{LightBackground} Switch Statements: A Condensed series of cascading else statements, it tests a value and compares it against multiple cases. & switch (grade) \{ \{\{nl\}\} case 9: \{\{nl\}\} printf("Freshman\textbackslash{}n"); \{\{nl\}\} break; \{\{nl\}\} default: \{\{nl\}\} printf("Invalid\textbackslash{}n"); \{\{nl\}\} break; \{\{nl\}\} \} \tn % Row Count 36 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} Functions in C & A function is a block of reusable logic that may have a defined set of input and output \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Built-In Function in C & The C Programming language comes with built-in standard library fuctions \{\{nl\}\} - printf() \{\{nl\}\} -rand() \{\{nl\}\} **Note to be able to use these make sure to \#include \textless{}stdio.h\textgreater{} \tn % Row Count 11 (+ 7) % Row 2 \SetRowColor{LightBackground} Calling Functions & int myNumber = incrementBy(5, 2); \{\{nl\}\} \{\{nl\}\} A function is called by stating the function name followed by parentheses. One or more argument values can be places in the parentheses as the function requires. \tn % Row Count 20 (+ 9) % Row 3 \SetRowColor{white} Storing a return value & int myNumber = incrementBy(5, 2); \{\{nl\}\} \{\{nl\}\} A function's return value, or output, can be stored in a variable for future use. \tn % Row Count 25 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Function Signature} \tn % Row Count 26 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Return Type} \tn % Row Count 27 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} Variable Names in C & In C, Variable names follow specific Rules: \{\{nl\}\} Names can only be composed of upper and lower case letters, numbers, and underscores. \{\{nl\}\} The first character must be a letter (upper or lower case). \{\{nl\}\} No keywords are allowed as the full name (int is not allowed but int\_count would work) \tn % Row Count 13 (+ 13) % Row 1 \SetRowColor{white} Data Types in C & The four main data types in c are: int, char, double, and float \tn % Row Count 16 (+ 3) % Row 2 \SetRowColor{LightBackground} Declaring Variables in C & int int\_count = 4; \{\{nl\}\} double priceApples; \tn % Row Count 18 (+ 2) % Row 3 \SetRowColor{white} Setting Variables in C & When you declare a variable, you do not need to set it right away, but you can set it right away if you want to \tn % Row Count 23 (+ 5) % Row 4 \SetRowColor{LightBackground} Variable Casting in C & You can implicitly or explicitly cast to variables in C. However, implicitly casting may not have the same effect as explicitly casting \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Loops and Errors}} \tn % Row 0 \SetRowColor{LightBackground} While Loops, will iteratre until a condition is met & while (a \textless{} 10) \{ \{\{nl\}\} a++; \{\{nl\}\} \} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} do -While loops, while loops that initially execute the body once before checking conditions & do \{ \{\{nl\}\} printf("not true!"); \{\{nl\}\} \} (while 2 == 3); \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} for loops, iterates a set number of times & for (int i = 0; i \textless{}= 10; i++) \{ \{\{nl\}\} printf("Hello!"); \{\{nl\}\} \} \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} Loop Keywords & All Loops can Utilize Keywords like continue and break. \{\{nl\}\} Continue will restart the loop without completing anything within the loop past the continue keyword. \{\{nl\}\} break will completely stop the loop and continue on after it within the code. \tn % Row Count 25 (+ 13) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Pointers and Memory}} \tn % Row 0 \SetRowColor{LightBackground} What is a pointer? & A pointer is a variable that sotres the hexadecimal address of the variable it is pointing to within memory \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Declaring pointers & type* pntr; \{\{nl\}\} type *pntr; \tn % Row Count 8 (+ 2) % Row 2 \SetRowColor{LightBackground} Accessing Memory Address & A Memory Address of a vairiable is obtained using the reference operator (\&). \{\{nl\}\} *example \&var \tn % Row Count 13 (+ 5) % Row 3 \SetRowColor{white} Dereferencing Pointers & A Pointer is dereference using the dereference operator (*) \{\{nl\}\} *Example *pntr \tn % Row Count 18 (+ 5) % Row 4 \SetRowColor{LightBackground} Incrementing and Decrementing Pointers & Pointers can be incremented and decremented using the + and - arithmetic operators \tn % Row Count 23 (+ 5) % Row 5 \SetRowColor{white} Accessing Arrays & Arrays can be accessed by using a pointer to the first element and icrementing and decrementing as necessary \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} Mathmatical Symbols in C & Addition: + \{\{nl\}\} Subtraction: - \{\{nl\}\} Division: / \{\{nl\}\} Multiplication: * \{\{nl\}\} Incrementing: ++ \{\{nl\}\} Decrementing: -{}- \{\{nl\}\} Modulo: \% \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Assignment Operations in C & Assignment: = \{\{nl\}\} Addition then Assignment: += \{\{nl\}\} Subtraction then Assignment: -= \{\{nl\}\} Multiplication then Assignment: *= \{\{nl\}\} Division then Assignment: /= \{\{nl\}\} Modulo then Assignment: \%= \tn % Row Count 15 (+ 9) % Row 2 \SetRowColor{LightBackground} Comparing Values in C & Both sides Equal?: == \{\{nl\}\} Two sides not equal?: != \{\{nl\}\} left lower then right?: \textless{} \{\{nl\}\} left lower or equal to right?: \textless{}= \{\{nl\}\} left greater than right?: \textgreater{} \{\{nl\}\} left greater than or equal to right?: \textgreater{}= \tn % Row Count 24 (+ 9) % Row 3 \SetRowColor{white} Logical Operators in C & and: \&\& (Both sides true?) \{\{nl\}\} or: || (At least one side true?) \{\{nl\}\} not: ! (true = false, false = true) \tn % Row Count 29 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Arrays and Strings}} \tn % Row 0 \SetRowColor{LightBackground} Creating Uninitialized Arrays & type arr{[}array\_size{]}; \{\{nl\}\} char word{[}15{]}; \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Creating initilized Arrays & type arr{[}{]} = \{el1, el2\}; \{\{nl\}\} char word{[}{]} = \{'H', 'e', 'l, 'l', 'o', '\textbackslash{}0'\} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} Accessing Array Elements & arr{[}idx{]}; \{\{nl\}\} word{[}0{]}; ('H') \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} First and Last Array Elements & firstElement = arr{[}0{]}; \{\{nl\}\} lastElement = arr{[}arraySize - 1{]}; \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} sizeof() \{\{nl\}\} {\bf{Note}} C does not have a built in way to actually find the size of an array, this is simply a handy trick to find the amount of elements an array should be able to hold. & int arr{[}17{]}; \{\{nl\}\} size\_t sizeArr = sizeof(arr) / sizeof(arr{[}0{]}); \{\{nl\}\} **Note size\_t is an unsigned integer type used to represent the size of objects in bytes, its the return type of sizeof() \{\{nl\}\} \tn % Row Count 24 (+ 11) % Row 5 \SetRowColor{white} Invalid Array Access & While it is possible to access beyond an array, it will cause the program to behave unpredictably \tn % Row Count 29 (+ 5) % Row 6 \SetRowColor{LightBackground} Creating Multidimensional Arrays & unitializedMultArr = type arr{[}12{]}{[}15{]}...{[}13{]}; \{\{nl\}\} initializedMultArr = type arr{[}{]}{[}15{]}{[}20{]}...{[}n{]} = \{\{elm1, elm2\}, \{elm3\}\} \tn % Row Count 36 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Arrays and Strings (cont)}} \tn % Row 7 \SetRowColor{LightBackground} Arrays are static & The length of a string cannot be modified as a string is a char array in C \tn % Row Count 4 (+ 4) % Row 8 \SetRowColor{white} Null Character & All Strings terminate with a null character ('\textbackslash{}0') \tn % Row Count 7 (+ 3) % Row 9 \SetRowColor{LightBackground} The length of a string & find the length using the strlen() function \tn % Row Count 10 (+ 3) % Row 10 \SetRowColor{white} string concatenation & strcat() function can be used to concate two strings \tn % Row Count 13 (+ 3) % Row 11 \SetRowColor{LightBackground} strcpy() & a string can be copied into an empty char array using this function \tn % Row Count 17 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}