From 58cd4b274f4ef6dce15bf30eccad4691aaf835ed Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 09:50:13 -0500 Subject: add special characters to c --- c.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 24a96463..c8c3e33b 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -20,6 +20,12 @@ 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. */ +//Special characters: +'\n' // newline character +'\t' // tab character +'\b' // backspace character +'\0' // null character + // Import headers with #include #include #include -- cgit v1.2.3 From deaaf3412d01a69fdc4354564fba5506ef65f4c8 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 10:51:48 -0500 Subject: Add print formatting characters --- c.html.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index c8c3e33b..a9fd8e50 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -23,9 +23,22 @@ Multi-line comments look like this. They work in C89 as well. //Special characters: '\n' // newline character '\t' // tab character +'\v' // vertical tab +'\f' // new page +'\r' // carriage return '\b' // backspace character '\0' // null character +//print formatting: +"%d" // integer +"%3d" // minimum length of 3 digits for integer (right justifies text) +"%s" // string +"%f" // float +"%3.2f" // minimum 3 digits left and 2 digits right decimal float +"%7.4s" // (can do with strings too) +"%c" // char +"%p" // pointer + // Import headers with #include #include #include -- cgit v1.2.3 From e03cda583dd6127831a32fcc8bd8cd8b53399794 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 11:44:45 -0500 Subject: Add more to string formatting. --- c.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index a9fd8e50..4e1e1e56 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -22,7 +22,7 @@ Multi-line comments look like this. They work in C89 as well. //Special characters: '\n' // newline character -'\t' // tab character +'\t' // tab character (left justifies text) '\v' // vertical tab '\f' // new page '\r' // carriage return @@ -38,6 +38,12 @@ Multi-line comments look like this. They work in C89 as well. "%7.4s" // (can do with strings too) "%c" // char "%p" // pointer +"%x" // hexidecimal +"%o" // octal +"%%" // prints % + +// Constants: use #define keyword, no semicolon at end. +#define DAYS_IN_YEAR = 365 // Import headers with #include #include -- cgit v1.2.3 From dba7ec8b9643f693bdb023f9827999b2b14d008b Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 12:50:58 -0500 Subject: Add getchar(), putchar(), EOF to C --- c.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 4e1e1e56..7010b9d5 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -427,6 +427,16 @@ void str_reverse(char *str_in) } } +// Built in functions: +// from stdio.h: +int c = getchar(); //reads character from user. If user types hello, only h is read. +// getchar() can be stored into int or char. I am using int because char is not large +// enough to store EOF used below. +while (c != EOF) { // EOF is value for "end of file". Linux: CTRL+D, Windows: CTRL+X + putchar(c); //prints character (without newline at end) + char c = getchar(); +} + /* char c[] = "This is a test."; str_reverse(c); -- cgit v1.2.3 From f1b1fd8e2efd6c30e553aae1c4f6cc28a6554e8f Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 13:06:37 -0500 Subject: Add clarity to EOF. Add %ld for longs in C --- c.html.markdown | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 7010b9d5..869ab984 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -34,6 +34,7 @@ Multi-line comments look like this. They work in C89 as well. "%3d" // minimum length of 3 digits for integer (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 @@ -429,10 +430,12 @@ void str_reverse(char *str_in) // Built in functions: // from stdio.h: -int c = getchar(); //reads character from user. If user types hello, only h is read. -// getchar() can be stored into int or char. I am using int because char is not large -// enough to store EOF used below. -while (c != EOF) { // EOF is value for "end of file". Linux: CTRL+D, Windows: CTRL+X +int c = getchar(); //reads character from input. If input = hi, only h is read. +// getchar() can be stored into int or char. I am using int because +// char is not large enough to store EOF used below. +while ((c = getchar()) != EOF) { // EOF constant "end of file". + // Linux: CTRL+D, Windows: CTRL+X + // must have () around getchar() as != is run before =. putchar(c); //prints character (without newline at end) char c = getchar(); } -- cgit v1.2.3 From b0c5ca0a1cc5a62fc8569fffffcdf35bf44e8dc8 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 15:57:18 -0500 Subject: Add note about loops C --- c.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 869ab984..166c43e6 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -231,6 +231,12 @@ int main() { printf("I print\n"); } + // Notes: + // Loops MUST always have a body. If no body is needed, do this: + for (i = 0; i <= 5; i++) { + ; // use semicolon to act as the body (null statement) + } + // While loops exist int ii = 0; while (ii < 10) { -- cgit v1.2.3 From 5c711eb30fe8cd810627742a30c016f458bd773c Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 16:24:56 -0500 Subject: Add more shorthand notes, array notes to C --- c.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 166c43e6..c6fee5d7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -166,6 +166,10 @@ int main() { int i1 = 1, i2 = 2; // Shorthand for multiple declaration float f1 = 1.0, f2 = 2.0; + //more shorthands: + int a, b, c; + a = b = c = 0; + // Arithmetic is straightforward i1 + i2; // => 3 i2 - i1; // => 1 @@ -339,7 +343,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; -- cgit v1.2.3 From dfd8afb4969c76b71a852e7bc8378b7e7f8f4bb3 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 16:36:16 -0500 Subject: Add ASCII chars to Types of C file. --- c.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index c6fee5d7..96f253b7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -98,6 +98,10 @@ int main() { unsigned int ux_int; unsigned long long ux_long_long; + // chars inside single quotes '*' are ASCII versions of + '0' //==> 48 on the ASCII table. + 'A' //==> 65 on the ASCII table. + // 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) -- cgit v1.2.3 From 79fdd62c6bcb13c550050efe02d6753d198a5a83 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 16:51:35 -0500 Subject: Add function prototype to C. --- c.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 96f253b7..75025b0c 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -422,6 +422,16 @@ int add_two_ints(int x1, int x2) return x1 + x2; // Use return to return a value } +// Must declare a 'funtion prototype' before main() when creating functions +// in file. +int getInt(char c); // function prototype +int main() { + return 0; +} +int getInt(char w) { //parameter name does not need to match function prototype + return 1; +} + /* Functions are pass-by-value, but you can make your own references with pointers so functions can mutate their values. -- cgit v1.2.3 From 41f65bb3415e7002ef171c351376c1c4d5336746 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 17:04:47 -0500 Subject: Clarified function call by value description. --- c.html.markdown | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 75025b0c..e6e2559e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -433,8 +433,12 @@ int getInt(char w) { //parameter name does not need to match function prototype } /* -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. So when a function is called, the arguments passed +to the function are copies of original arguments (except arrays). Anything you +do to your arguments do not change the value of the actual argument where the +function was called. + +You can use pointers if you need to edit the original argument values. Example: in-place string reversal */ -- cgit v1.2.3 From 394ca958b3b61853b1fe8ec1063b3adb03239178 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 18:42:05 -0500 Subject: Add more notes to null character in C --- c.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index e6e2559e..90d0d358 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -27,7 +27,8 @@ Multi-line comments look like this. They work in C89 as well. '\f' // new page '\r' // carriage return '\b' // backspace character -'\0' // null 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. //print formatting: "%d" // integer -- cgit v1.2.3 From d632203255085171ce79a106a40797d1d5fa54a3 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 18:54:07 -0500 Subject: Add more notes to function prototype in C. --- c.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 90d0d358..ee60d168 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -425,13 +425,16 @@ int add_two_ints(int x1, int x2) // Must declare a 'funtion prototype' before main() when creating functions // in file. -int getInt(char c); // function prototype +void getInt(char c); // function prototype int main() { return 0; } -int getInt(char w) { //parameter name does not need to match function prototype - return 1; +void getInt(char w) { //parameter name does not need to match function prototype + ; } +//if function takes no parameters, do: int getInt(void); for function prototype +// and for the function declaration: int getInt(void) {} +// this is to keep compatibility with older versions of C. /* Functions are call by value. So when a function is called, the arguments passed -- cgit v1.2.3 From 8caf3768fb4a1f0fa14ca4765e7f168534c7cd8f Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 19:24:15 -0500 Subject: Add extern notes to C. --- c.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index ee60d168..2a828ab7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -99,7 +99,7 @@ int main() { unsigned int ux_int; unsigned long long ux_long_long; - // chars inside single quotes '*' are ASCII versions of + // chars inside single quotes '*' are character constants. '0' //==> 48 on the ASCII table. 'A' //==> 65 on the ASCII table. @@ -472,6 +472,12 @@ while ((c = getchar()) != EOF) { // EOF constant "end of file". char c = getchar(); } +//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 +} + /* char c[] = "This is a test."; str_reverse(c); -- cgit v1.2.3 From 411d9a9813e8c7174f35540198b6dadc8c0b665c Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 19:29:25 -0500 Subject: Add more escape sequences to C. --- c.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 2a828ab7..4764c38e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -21,14 +21,21 @@ Multi-line comments look like this. They work in C89 as well. */ //Special characters: +'\a' // alert (bell) character '\n' // newline character '\t' // tab character (left justifies text) '\v' // vertical tab -'\f' // new page +'\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 -- cgit v1.2.3 From 2e8e2b5f3552bd9541a4eac6abba94a767dda8e4 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 20:51:11 -0500 Subject: Add enum to C. --- c.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 4764c38e..0ac603a7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -54,6 +54,10 @@ Multi-line comments look like this. They work in C89 as well. // Constants: use #define keyword, no semicolon at end. #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 #include -- cgit v1.2.3 From ca32d7a5f2edfe9dad2b43fc849b1495c2b5f889 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 21:16:32 -0500 Subject: Add clarity to characters in character set. --- c.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 0ac603a7..4e7dae5c 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -110,9 +110,9 @@ int main() { unsigned int ux_int; unsigned long long ux_long_long; - // chars inside single quotes '*' are character constants. - '0' //==> 48 on the ASCII table. - 'A' //==> 65 on the ASCII table. + // chars inside single quotes '*' are integers in your character set. + '0' //==> 48 on the ASCII character set. + 'A' //==> 65 on 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.). -- cgit v1.2.3 From e9c92321f4442ca4af75f6cce16d435287dad750 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 31 Aug 2013 21:18:59 -0500 Subject: Add include statement to popular C standar libraries. --- c.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 4e7dae5c..6d95f28b 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -62,6 +62,7 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; #include #include #include +#include // (File names between are headers from the C standard library.) // For your own headers, use double quotes instead of angle brackets: -- cgit v1.2.3 From 642e7c551e0f6292be403ed893a6695130cf350a Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 1 Sep 2013 10:44:55 -0500 Subject: Add note to while loop in C. --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 6d95f28b..f63adc05 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -260,7 +260,7 @@ int main() { // While loops exist int ii = 0; - while (ii < 10) { + while (ii < 10) { //ANY value not zero is true. printf("%d, ", ii++); // ii++ increments ii in-place // after yielding its value ("postincrement"). } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " -- cgit v1.2.3 From 977d226d26c056094a33f38da79d1d9395915f52 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 1 Sep 2013 10:59:30 -0500 Subject: Reworded incrementing operators in C. --- c.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f63adc05..c5ac7708 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -261,8 +261,7 @@ int main() { // While loops exist int ii = 0; while (ii < 10) { //ANY value not zero is true. - printf("%d, ", ii++); // ii++ increments ii in-place - // after yielding its value ("postincrement"). + 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"); @@ -270,8 +269,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"); -- cgit v1.2.3 From 2fab4dc971e3c249dc8be7c365309afb8ab9c209 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 1 Sep 2013 11:10:04 -0500 Subject: Add specific examples of increment and decrement operators to C. --- c.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index c5ac7708..e6179ba6 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -226,6 +226,17 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 + //Increment and decrement operators: + int j = 0; + char s[]; + int w = 0; + j++; //difference between postfix and prefix explained below + ++j; // in string example. + j--; + --j; + s[j++]; //returns value of j to s THEN increments value of j. + s[++j]; //increments value of j THEN returns value of j to s. + // Bitwise operators! ~0x0F; // => 0xF0 (bitwise negation, "1's complement") 0x0F & 0xF0; // => 0x00 (bitwise AND) -- cgit v1.2.3 From 0ab144ff976c7a701acef552cc8e33b38faa7dbc Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 1 Sep 2013 11:51:25 -0500 Subject: Add conditinal expression (?:) to C. --- c.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index e6179ba6..ea047ea0 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -226,6 +226,10 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 + //Conditional expression ( ?: ) + int a, b, z; + z = (a > b) ? a : b; // z = max(a, b); + //Increment and decrement operators: int j = 0; char s[]; -- cgit v1.2.3 From c4f541dc92ae149947a49a885292d8ad7ead7aa6 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 1 Sep 2013 13:17:26 -0500 Subject: Add order of evaluation table to C. --- c.html.markdown | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index ea047ea0..fd0b7964 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -593,6 +593,31 @@ typedef void (*my_fnp_type)(char *); // ... // my_fnp_type f; + +/////////////////////////////////////// +// 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 -- cgit v1.2.3 From f28d33fb187bc834e6e2956117039f9abe3b6d9b Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 19 Sep 2013 17:10:53 -0500 Subject: Edit wording on in c changes. --- c.html.markdown | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index fd0b7964..89bfbe6d 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -39,7 +39,7 @@ Multi-line comments look like this. They work in C89 as well. //print formatting: "%d" // integer -"%3d" // minimum length of 3 digits for integer (right justifies text) +"%3d" // integer with minimum of length 3 digits (right justifies text) "%s" // string "%f" // float "%ld" // long @@ -51,7 +51,7 @@ Multi-line comments look like this. They work in C89 as well. "%o" // octal "%%" // prints % -// Constants: use #define keyword, no semicolon at end. +// Constants: #define (no semicolon at end) #define DAYS_IN_YEAR = 365 //enumeration constants are also ways to declare constants. @@ -62,7 +62,6 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; #include #include #include -#include // (File names between are headers from the C standard library.) // For your own headers, use double quotes instead of angle brackets: @@ -111,7 +110,7 @@ int main() { unsigned int ux_int; unsigned long long ux_long_long; - // chars inside single quotes '*' are integers in your character set. + // chars inside single quotes are integers in machine's character set. '0' //==> 48 on the ASCII character set. 'A' //==> 65 on the ASCII character set. @@ -226,20 +225,14 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 - //Conditional expression ( ?: ) + //Conditional expression ( ? : ) int a, b, z; - z = (a > b) ? a : b; // z = max(a, b); + z = (a > b) ? a : b; // "if a > b return a, else return b." //Increment and decrement operators: - int j = 0; - char s[]; - int w = 0; - j++; //difference between postfix and prefix explained below - ++j; // in string example. - j--; - --j; s[j++]; //returns value of j to s THEN increments value of j. s[++j]; //increments value of j THEN returns value of j to s. + // same with j-- and --j // Bitwise operators! ~0x0F; // => 0xF0 (bitwise negation, "1's complement") @@ -267,12 +260,6 @@ int main() { printf("I print\n"); } - // Notes: - // Loops MUST always have a body. If no body is needed, do this: - for (i = 0; i <= 5; i++) { - ; // use semicolon to act as the body (null statement) - } - // While loops exist int ii = 0; while (ii < 10) { //ANY value not zero is true. @@ -297,6 +284,12 @@ int main() { printf("\n"); + // *****NOTES*****: + // Loops MUST always have a body. If no body is needed, do: + 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 @@ -448,18 +441,20 @@ int add_two_ints(int x1, int x2) return x1 + x2; // Use return to return a value } -// Must declare a 'funtion prototype' before main() when creating functions -// in file. +// Must declare a 'funtion prototype' when creating functions before main() void getInt(char c); // function prototype -int main() { +int main() { // main function return 0; } void getInt(char w) { //parameter name does not need to match function prototype ; } -//if function takes no parameters, do: int getInt(void); for function prototype -// and for the function declaration: int getInt(void) {} -// this is to keep compatibility with older versions of C. + +//if function takes no parameters, do: +int getInt(void); for function prototype +// and for the function declaration: +int getInt(void) {} +// (this is to keep compatibility with older versions of C). /* Functions are call by value. So when a function is called, the arguments passed @@ -485,11 +480,13 @@ void str_reverse(char *str_in) } } +///////////////////////////////////// // Built in functions: +///////////////////////////////////// // from stdio.h: -int c = getchar(); //reads character from input. If input = hi, only h is read. -// getchar() can be stored into int or char. I am using int because -// char is not large enough to store EOF used below. +// getchar() +int c = getchar(); //reads character from input. +// If input = hi, 'h' is returned then next call, 'i' returned. while ((c = getchar()) != EOF) { // EOF constant "end of file". // Linux: CTRL+D, Windows: CTRL+X // must have () around getchar() as != is run before =. -- cgit v1.2.3 From 2ef41199266fefc868dd28c3e0ac5dc55daf9d8f Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 21:53:42 -0500 Subject: Fix typo for 'funtion prototype' --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index fd0b7964..68ef7f03 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -448,7 +448,7 @@ int add_two_ints(int x1, int x2) return x1 + x2; // Use return to return a value } -// Must declare a 'funtion prototype' before main() when creating functions +// Must declare a 'function prototype' before main() when creating functions // in file. void getInt(char c); // function prototype int main() { -- cgit v1.2.3 From 7c559d57a88143b0cbe7b16dbd071f0e07c83b32 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:05:08 -0500 Subject: Move special characters to end of file --- c.html.markdown | 61 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 2a65d82c..9745ed9a 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -20,37 +20,6 @@ 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. */ -//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 % - // Constants: #define (no semicolon at end) #define DAYS_IN_YEAR = 365 @@ -590,6 +559,36 @@ 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 -- cgit v1.2.3 From 35909645f2dbfedc38b566ec9838c2202cd51e3c Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:13:10 -0500 Subject: Edit note on function prototypes. --- c.html.markdown | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 9745ed9a..df6cd036 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -41,6 +41,10 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; void function_1(); void function_2(); +// 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. int main() { @@ -410,15 +414,6 @@ int add_two_ints(int x1, int x2) return x1 + x2; // Use return to return a value } -// Must declare a 'function prototype' before main() when creating functions -void getInt(char c); // function prototype -int main() { // main function - return 0; -} -void getInt(char w) { //parameter name does not need to match function prototype - ; -} - //if function takes no parameters, do: int getInt(void); for function prototype // and for the function declaration: -- cgit v1.2.3 From ec7ee88698bf32bfe6e931865e94d2dac8f41c80 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:19:07 -0500 Subject: Add to ? : example in C. --- c.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index df6cd036..f1e328cf 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -199,8 +199,10 @@ int main() { 0 || 0; // => 0 //Conditional expression ( ? : ) - int a, b, z; - z = (a > b) ? a : b; // "if a > b return a, else return b." + 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: s[j++]; //returns value of j to s THEN increments value of j. -- cgit v1.2.3 From 868be425a6602020f275404a8776d48f3d6d2715 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:31:04 -0500 Subject: Add to notes on increment and decrement operators. --- c.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f1e328cf..f67f6c4a 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -205,8 +205,11 @@ int main() { z = (a > b) ? a : b; // => 10 "if a > b return a, else return b." //Increment and decrement operators: - s[j++]; //returns value of j to s THEN increments value of j. - s[++j]; //increments value of j THEN returns value of j to s. + char *s = "iLoveC" + int j = 0; + s[j++]; // => "i" Returns value of j to s THEN increments value of j. + j = 0; + s[++j]; // => "L" Increments value of j THEN returns value of j to s. // same with j-- and --j // Bitwise operators! -- cgit v1.2.3 From 67cd987efa06141e5dac74f07fa8f561265c835a Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:33:19 -0500 Subject: Declare variable before empty for loop. --- c.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f67f6c4a..d075ea0e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -264,6 +264,7 @@ int main() { // *****NOTES*****: // Loops MUST always have a body. If no body is needed, do: + int i; for (i = 0; i <= 5; i++) { ; // use semicolon to act as the body (null statement) } -- cgit v1.2.3 From 981cc871a4d4e5473e580dee8574a119ac8a9723 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 20 Sep 2013 22:37:40 -0500 Subject: Edit getchar() notes. --- c.html.markdown | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index d075ea0e..ee35cdd2 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -453,15 +453,14 @@ void str_reverse(char *str_in) ///////////////////////////////////// // Built in functions: ///////////////////////////////////// -// from stdio.h: -// getchar() -int c = getchar(); //reads character from input. +// from: #include +// ** getchar() ** +// int c = getchar(); //reads character from input. // If input = hi, 'h' is returned then next call, 'i' returned. while ((c = getchar()) != EOF) { // EOF constant "end of file". // Linux: CTRL+D, Windows: CTRL+X // must have () around getchar() as != is run before =. putchar(c); //prints character (without newline at end) - char c = getchar(); } //if referring to external variables outside function, must use extern keyword. -- cgit v1.2.3 From 38de481179484d597d44fa934f87a5f21ebfaa6c Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 23 Nov 2013 22:40:52 -0600 Subject: Add variety to function prototypes. --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index ee35cdd2..b4a874d3 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -38,8 +38,8 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; // 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 s[]); +int function_2(void); // Must declare a 'function prototype' before main() when functions occur after // your main() function. -- cgit v1.2.3 From e63232d2de1e2058dd381fe1094c7f775f2e3eb8 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 24 Nov 2013 14:17:32 -0600 Subject: Add static description to C. --- c.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index b4a874d3..7b95c969 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -469,6 +469,13 @@ void testFunc() { extern int i; //i here is now using external variable i } +//if external variable should only be visible to functions in the source file +// they are declared in, use static: +static int i = 0; //other source files using testFunc() cannot access variable i +void testFunc() { + extern int i; +} + /* char c[] = "This is a test."; str_reverse(c); -- cgit v1.2.3 From af9cc6e9b9748e43cb1c02013fe92465c3455503 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 24 Nov 2013 14:25:30 -0600 Subject: Edit static description wording. Add note about private functions. --- c.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 7b95c969..ab09a4a2 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -469,12 +469,12 @@ void testFunc() { extern int i; //i here is now using external variable i } -//if external variable should only be visible to functions in the source file -// they are declared in, use static: -static int i = 0; //other source files using testFunc() cannot access 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."; -- cgit v1.2.3 From bb4f644a19a086c3c6b32891a7240cc140b379f4 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 24 Nov 2013 15:01:24 -0600 Subject: Fix #define as it does not use = sign. --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index ab09a4a2..d8eccd78 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -21,7 +21,7 @@ Multi-line comments look like this. They work in C89 as well. */ // Constants: #define (no semicolon at end) -#define DAYS_IN_YEAR = 365 +#define DAYS_IN_YEAR 365 //enumeration constants are also ways to declare constants. enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; -- cgit v1.2.3 From 5c7f6d70b9477fcd4fe64460704d7dcf48799f98 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 24 Nov 2013 16:58:21 -0600 Subject: Remove bad description of function prototypes. --- c.html.markdown | 6 ------ 1 file changed, 6 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index d8eccd78..d029a00f 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -420,12 +420,6 @@ int add_two_ints(int x1, int x2) return x1 + x2; // Use return to return a value } -//if function takes no parameters, do: -int getInt(void); for function prototype -// and for the function declaration: -int getInt(void) {} -// (this is to keep compatibility with older versions of C). - /* Functions are call by value. So when a function is called, the arguments passed to the function are copies of original arguments (except arrays). Anything you -- cgit v1.2.3 From 67a5236629b6c7748b609f23c0362925b5c4774d Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sun, 24 Nov 2013 23:26:11 -0600 Subject: Add multi-dimensional array description. --- c.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index d029a00f..f4edfca5 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -148,6 +148,14 @@ 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 /////////////////////////////////////// -- cgit v1.2.3 From df3cc00f5233dac96c0e063d87d3552f493e25f6 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 25 Nov 2013 09:21:40 -0600 Subject: Remove bad examples, reword places. --- c.html.markdown | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f4edfca5..4187d757 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -20,7 +20,7 @@ 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 (no semicolon at end) +// Constants: #define #define DAYS_IN_YEAR 365 //enumeration constants are also ways to declare constants. @@ -38,7 +38,7 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; // Declare function signatures in advance in a .h file, or at the top of // your .c file. -void function_1(char s[]); +void function_1(char c); int function_2(void); // Must declare a 'function prototype' before main() when functions occur after @@ -84,8 +84,8 @@ int main() { unsigned long long ux_long_long; // chars inside single quotes are integers in machine's character set. - '0' //==> 48 on the ASCII character set. - 'A' //==> 65 on the ASCII 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.). @@ -154,16 +154,16 @@ int main() { {6, 7, 8, 9, 0} } //access elements: - int array_int = multi_array[0][2]; //=> 3 + 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; - //more shorthands: int a, b, c; a = b = c = 0; @@ -215,9 +215,9 @@ int main() { //Increment and decrement operators: char *s = "iLoveC" int j = 0; - s[j++]; // => "i" Returns value of j to s THEN increments value of j. + 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 value of j to s. + s[++j]; // => "L". Increments value of j THEN returns j-th value of s. // same with j-- and --j // Bitwise operators! @@ -271,7 +271,7 @@ int main() { printf("\n"); // *****NOTES*****: - // Loops MUST always have a body. If no body is needed, do: + // 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) @@ -429,12 +429,12 @@ int add_two_ints(int x1, int x2) } /* -Functions are call by value. So when a function is called, the arguments passed -to the function are copies of original arguments (except arrays). Anything you -do to your arguments do not change the value of the actual argument where the -function was called. +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. -You can use pointers if you need to edit the original argument values. +Use pointers if you need to edit the original argument values. Example: in-place string reversal */ @@ -452,19 +452,6 @@ void str_reverse(char *str_in) } } -///////////////////////////////////// -// Built in functions: -///////////////////////////////////// -// from: #include -// ** getchar() ** -// int c = getchar(); //reads character from input. -// If input = hi, 'h' is returned then next call, 'i' returned. -while ((c = getchar()) != EOF) { // EOF constant "end of file". - // Linux: CTRL+D, Windows: CTRL+X - // must have () around getchar() as != is run before =. - putchar(c); //prints character (without newline at end) -} - //if referring to external variables outside function, must use extern keyword. int i = 0; void testFunc() { -- cgit v1.2.3