|
|
Line 1: |
Line 1: |
| on the basics of C for you to study. We do not aim to present this information completely, but only to remind of things you should have learned reading.
| | #REDIRECT [[C Basics]] |
| | |
| =C Data Types=
| |
| Note - # of bytes is for on the CS server using gcc (as of 2022). This varies depending on your system and compiler.
| |
| * '''integer data types''': <code>char</code> (1 byte), <code>short</code> (2 bytes), <code>int</code> (4 bytes), <code>long</code> (8 bytes)
| |
| ** '''signed/unsigned''': integer types are <code>signed</code> by default, use <code>unsigned</code> before any of the above for unsigned.
| |
| ** '''max/min values''': for unsigned types, the value ranges from 0 to 2<sup># of bits</sup>-1. For signed types, the value ranges from (-2<sup># of bits -1</sup>) to (2<sup># of bits - 1</sup>). E.g., <code>char</code> ranges from -128 to 127, while <code>unsigned char</code> ranges from 0 to 255.
| |
| * '''floating point data types:''' <code>float</code> (4 bytes), <code>double</code> (8 bytes), <code>long double</code> (16 bytes)
| |
| ** '''precision and max/min values''': each floating point type uses 1 bit for the sign, most of the bits for the mantissa (aka significand), and the remainder for the exponent. <code>float</code> uses 23 bits for the significand, resulting in about 7 digits of precision, and 8 bits for the exponent, meaning the exponent can range from -128 to 127 and the maximum value is around 10<sup>38</sup>. <code>double</code> uses 52 bits for the mantissa and 11 bits for the exponent.
| |
| * '''pointers''' - memory address, note that <code>sizeof(char *)</code> is however many bytes a memory address takes up, which in 2022 on the CS server is 8.
| |
| * any type of data in C can be interpreted as an integer and printed that way
| |
| * '''C string''' - array of characters, "last" character in the string is <code>'\0'</code> (literally all 0 bits in the byte), also can use <code>NULL</code> (a synonym for <code>'\0'</code>)
| |
| * '''array''' - block of memory large enough for multiple int's (or float's, etc.). Declared like <code>int A[10];</code> In the program, <code>A</code> evaluates to the address of the first element in the array.
| |
| | |
| =Operators=
| |
| * '''Bit operations -''' <code>&</code> and, <code>^</code> xor, <code>|</code> or, <code>~</code> complement, <code><<</code> left bit shift, <code>>></code> right bit shift
| |
| * '''Logical (Boolean) operations -''' <code>&&</code> and, <code>||</code> or, <code>!</code> not
| |
| * '''Arithmetic -''' <code>+</code> <code>-</code> <code>*</code> <code>/</code> <code>%</code>
| |
| * '''Assignment -''' <code>=</code>, also = after all bit/arithmetic operations (<code>+=</code>, <code>-=</code>, <code>&=</code>, etc.)
| |
| * '''Memory access -''' <code>&</code> gives the address of a variable, <code>*</code> dereferences (aka gets the value of) a pointer variable
| |
| * '''Precedence and associativity -''' see https://en.cppreference.com/w/c/language/operator_precedence
| |
| | |
| =Grouping=
| |
| * <code>{</code> <code>}</code> for compound statements
| |
| * <code>(</code> <code>)</code> for order of operations / grouping expressions
| |
| * <code>(</code> <code>,</code> <code>)</code> for defining functions, calling functions
| |
| * <code>[</code> <code>]</code> for indexing arrays
| |
| | |
| =Statements=
| |
| * <code>for</code> - <code>for</code> <code>(</code> ''initializer'' <code>;</code> ''loop condition'' <code>;</code> ''increment'' <code>)</code> ''loop body statement''
| |
| * <code>while</code> - <code>while</code> <code>(</code> ''loop condition'' <code>)</code> ''loop body statement''
| |
| * <code>do</code> <code>while</code> - runs loop body at least once, before checking the loop condition
| |
| * <code>continue</code> - for the innermost currently executing loop, go back to the beginning of that loop
| |
| * <code>break</code> - for the innermost currently executing loop, break out of the loop and start with the code right after the loop
| |
| * <code>break</code> - used in a switch statement to break out of the switch statement
| |
| * <code>switch</code> - only for integer types (including char's), note that must use <code>break</code> with each <code>case</code> (or multiple case's will run the same code). If none of the cases's given, and if a <code>default</code> is given, then the default is run.
| |
| * <code>if</code> / <code>else</code>
| |
| | |
| =Variables, Data, Memory=
| |
| * '''Initialization''' - is not guaranteed, you must initialize all variables.
| |
| * '''Scope''' - variables exist within the <code>{</code> <code>}</code> they are declared within, globals are declared outside of functions and have scope for the whole file
| |
| * <code>malloc</code>/<code>free</code> - for allocating and freeing dynamically allocated memory (from the heap)
| |
| * '''pointers''' / memory addresses - <code>&</code> gets the address (aka memory location) of a variable, <code>*</code> gets the value at a given memory address, <code>int *</code> declares a pointer variable that stores a memory address
| |
| * '''array''' - if declared as <code>int A[10];</code> then <code>A[i]</code> is the (i+1)st slot in the array, and <code>A</code> (on its own, no brackets) is the address of the first slot in the array
| |
| * '''Basic 4 types of memory''' - local variables (kept on ''"the stack"''), global variables (kept in ''"data"''), the program code (kept in ''"code"''), dynamic memory (kept in ''"the heap"'')
| |
| * '''The stack''' - local variables automatically allocated space, stack pointer keeps track of the top of the stack, at end of <code>}</code> local variables are "free'd" by changing the stack pointer (so next allocated variables will overwrite previous ones). Stack memory is limited (e.g., 1MB), so large arrays and such should be allocated in the heap or as global variables. Stack memory is reused, which is one of the reasons that variables should always be initialized.
| |
| * '''The heap''' - big chunk of memory, <code>malloc</code> allocates space, <code>free</code> frees it, it's up to program to keep track of malloc'ed memory. Can be as large as memory.
| |
| * '''Code''' - memory that has the program code itself is read/execute-only.
| |
| * '''Data''' - global variables are known at compile time, space is allocated when the program is loaded and persists until program is finished, can declare large arrays as globals. Can be larger than the stack, still best practice to store really large things in the heap.
| |
| | |
| =Functions=
| |
| * Parameters are ''passed by value'' (whatever changes are made in the function do not get "sent back" to where the function was called from), use pointers to have modified variables "returned"
| |
| * <code>return</code> - returns value from function, stops function
| |
| | |
| =Pre-processor=
| |
| * Is executed ''before'' compiling - first pre-processor is run through the entire file(s), second the compiler runs
| |
| * <code>#include</code> - it is as if that file were copy/pasted into the present file
| |
| * <code>#define PI 3.14</code> - when pre-processor is run, all occurrences of <code>PI</code> in the program will be replaced with <code>3.14</code>, before the compiler is run.
| |
| | |
| =Command-Line Arguments=
| |
| * <code>argc</code> is count, <code>argv</code> is array of C strings, <code>argv[0]</code> is the name of the program itself, <code>argv[1]</code> is first actual argument, arguments are all C strings so use <code>atoi</code> or <code>atof</code> if need to convert to int or float.
| |
| | |
| =Other Punctuation=
| |
| * <code>//</code> comment to the end of the line
| |
| * <code>/*</code> multiple lines <code>*/</code>
| |
| | |
| =Other Keywords, Operators=
| |
| See https://en.cppreference.com/w/c/keyword for a list of all keywords in C. Some on the list are not commonly used. If that link's explanation is not making sense, you can try one of these: https://www.programiz.com/c-programming/list-all-keywords-c-language, or http://aboutc.weebly.com/keywords.html
| |
| | |
| For a list of more "language reference" information, see https://en.cppreference.com/w/c/language
| |
| | |
| =Standard Library=
| |
| * string functions - strlen, strcmp, strstr, strtok, atoi, atof, strcpy
| |
| * I/O - printf, scanf, fgetc, getline, fgets, fprintf, fscanf, fputc
| |
| * math functions - (use -lm flag when compiling with gcc), log, log2, log10, sqrt, pow
| |
| | |
| =Assignments=
| |
| It is traditional to have a series of quizzes over the operators, evaluating expressions, keywords, etc. A few are listed here that you can practice with.
| |
| | |
| * Practice quizzes from here - https://indstate.instructure.com/courses/12565/quizzes
| |
| ** C Operators and Expressions Quiz
| |
| ** C Keywords and Data Types Quiz
| |
| ** C Fixing Errors Quiz
| |
| ** C Errors Quiz
| |
| | |
| '''Pass rating check''' If you are assigned these quizzes for your course, then ''pass'' is at least 95%.
| |