diff options
-rw-r--r-- | bash.html.markdown | 7 | ||||
-rw-r--r-- | c.html.markdown | 140 |
2 files changed, 136 insertions, 11 deletions
diff --git a/bash.html.markdown b/bash.html.markdown index d208b957..afc46eb0 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -115,6 +115,13 @@ do echo "$VARIABLE" done +# while loop: +while [true] +do + echo "loop body here..." + break +done + # You can also define functions # Definition: function foo () diff --git a/c.html.markdown b/c.html.markdown index c67f8b21..84856b32 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -20,6 +20,13 @@ memory management and C will take you as far as you need to go. Multi-line comments look like this. They work in C89 as well. */ +// Constants: #define <keyword> +#define DAYS_IN_YEAR 365 + +//enumeration constants are also ways to declare constants. +enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; +// MON gets 2 automatically, TUE gets 3, etc. + // Import headers with #include #include <stdlib.h> #include <stdio.h> @@ -31,8 +38,12 @@ Multi-line comments look like this. They work in C89 as well. // Declare function signatures in advance in a .h file, or at the top of // your .c file. -void function_1(); -void function_2(); +void function_1(char c); +int function_2(void); + +// Must declare a 'function prototype' before main() when functions occur after +// your main() function. +int add_two_ints(int x1, int x2); // function prototype // Your program's entry point is a function called // main with an integer return type. @@ -72,6 +83,10 @@ int main() { unsigned int ux_int; unsigned long long ux_long_long; + // chars inside single quotes are integers in machine's character set. + '0' // => 48 in the ASCII character set. + 'A' // => 65 in the ASCII character set. + // sizeof(T) gives you the size of a variable with type T in bytes // sizeof(obj) yields the size of the expression (variable, literal, etc.). printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) @@ -135,13 +150,25 @@ int main() { int cha = 'a'; // fine char chb = 'a'; // fine too (implicit conversion from int to char) + //Multi-dimensional arrays: + int multi_array[2][5] = { + {1, 2, 3, 4, 5}, + {6, 7, 8, 9, 0} + } + //access elements: + int array_int = multi_array[0][2]; // => 3 + /////////////////////////////////////// // Operators /////////////////////////////////////// - int i1 = 1, i2 = 2; // Shorthand for multiple declaration + // Shorthands for multiple declarations: + int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; + int a, b, c; + a = b = c = 0; + // Arithmetic is straightforward i1 + i2; // => 3 i2 - i1; // => 1 @@ -181,6 +208,20 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 + //Conditional expression ( ? : ) + int a = 5; + int b = 10; + int z; + z = (a > b) ? a : b; // => 10 "if a > b return a, else return b." + + //Increment and decrement operators: + char *s = "iLoveC" + int j = 0; + s[j++]; // => "i". Returns the j-th item of s THEN increments value of j. + j = 0; + s[++j]; // => "L". Increments value of j THEN returns j-th value of s. + // same with j-- and --j + // Bitwise operators! ~0x0F; // => 0xF0 (bitwise negation, "1's complement") 0x0F & 0xF0; // => 0x00 (bitwise AND) @@ -209,9 +250,8 @@ int main() { // While loops exist int ii = 0; - while (ii < 10) { - printf("%d, ", ii++); // ii++ increments ii in-place - // after yielding its value ("postincrement"). + while (ii < 10) { //ANY value not zero is true. + printf("%d, ", ii++); // ii++ increments ii AFTER using it's current value. } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " printf("\n"); @@ -219,8 +259,7 @@ int main() { int kk = 0; do { printf("%d, ", kk); - } while (++kk < 10); // ++kk increments kk in-place, and yields - // the already incremented value ("preincrement") + } while (++kk < 10); // ++kk increments kk BEFORE using it's current value. // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " printf("\n"); @@ -233,6 +272,13 @@ int main() { printf("\n"); + // *****NOTES*****: + // Loops and Functions MUST have a body. If no body is needed: + int i; + for (i = 0; i <= 5; i++) { + ; // use semicolon to act as the body (null statement) + } + // branching with multiple choices: switch() switch (some_integral_expression) { case 0: // labels need to be integral *constant* epxressions @@ -309,7 +355,7 @@ int main() { printf("%d\n", x); // => Prints 1 // Arrays are a good way to allocate a contiguous block of memory - int x_array[20]; + int x_array[20]; //declares array of size 20 (cannot change size) int xx; for (xx = 0; xx < 20; xx++) { x_array[xx] = 20 - xx; @@ -385,8 +431,12 @@ int add_two_ints(int x1, int x2) } /* -Functions are pass-by-value, but you can make your own references -with pointers so functions can mutate their values. +Functions are call by value. When a function is called, the arguments passed to +the function are copies of the original arguments (except arrays). Anything you +do to the arguments in the function do not change the value of the original +argument where the function was called. + +Use pointers if you need to edit the original argument values. Example: in-place string reversal */ @@ -404,6 +454,19 @@ void str_reverse(char *str_in) } } +//if referring to external variables outside function, must use extern keyword. +int i = 0; +void testFunc() { + extern int i; //i here is now using external variable i +} + +//make external variables private to source file with static: +static int i = 0; //other files using testFunc() cannot access variable i +void testFunc() { + extern int i; +} +//**You may also declare functions as static to make them private** + /* char c[] = "This is a test."; str_reverse(c); @@ -494,6 +557,61 @@ typedef void (*my_fnp_type)(char *); // ... // my_fnp_type f; +//Special characters: +'\a' // alert (bell) character +'\n' // newline character +'\t' // tab character (left justifies text) +'\v' // vertical tab +'\f' // new page (formfeed) +'\r' // carriage return +'\b' // backspace character +'\0' // null character. Usually put at end of strings in C lang. + // hello\n\0. \0 used by convention to mark end of string. +'\\' // backspace +'\?' // question mark +'\'' // single quote +'\"' // double quote +'\xhh' // hexadecimal number. Example: '\xb' = vertical tab character +'\ooo' // octal number. Example: '\013' = vertical tab character + +//print formatting: +"%d" // integer +"%3d" // integer with minimum of length 3 digits (right justifies text) +"%s" // string +"%f" // float +"%ld" // long +"%3.2f" // minimum 3 digits left and 2 digits right decimal float +"%7.4s" // (can do with strings too) +"%c" // char +"%p" // pointer +"%x" // hexidecimal +"%o" // octal +"%%" // prints % + +/////////////////////////////////////// +// Order of Evaluation +/////////////////////////////////////// + +//---------------------------------------------------// +// Operators | Associativity // +//---------------------------------------------------// +// () [] -> . | left to right // +// ! ~ ++ -- + = *(type)sizeof | right to left // +// * / % | left to right // +// + - | left to right // +// << >> | left to right // +// < <= > >= | left to right // +// == != | left to right // +// & | left to right // +// ^ | left to right // +// | | left to right // +// && | left to right // +// || | left to right // +// ?: | right to left // +// = += -= *= /= %= &= ^= |= <<= >>= | right to left // +// , | left to right // +//---------------------------------------------------// + ``` ## Further Reading |