diff options
Diffstat (limited to 'c.html.markdown')
-rw-r--r-- | c.html.markdown | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/c.html.markdown b/c.html.markdown index 3d632eab..92f07fe2 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -36,7 +36,6 @@ Multi-line comments don't nest /* Be careful */ // comment ends on this line... 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> @@ -76,7 +75,7 @@ int main (int argc, char** argv) /////////////////////////////////////// // Types /////////////////////////////////////// - + // All variables MUST be declared at the top of the current block scope // we declare them dynamically along the code for the sake of the tutorial @@ -114,7 +113,6 @@ int main (int argc, char** argv) // sizeof(obj) yields the size of the expression (variable, literal, etc.). printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words) - // If the argument of the `sizeof` operator is an expression, then its argument // is not evaluated (except VLAs (see below)). // The value it yields in this case is a compile-time constant. @@ -130,7 +128,6 @@ int main (int argc, char** argv) int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes // (assuming 4-byte words) - // You can initialize an array to 0 thusly: char my_array[20] = {0}; @@ -146,17 +143,12 @@ int main (int argc, char** argv) // can be declared as well. The size of such an array need not be a compile // time constant: printf("Enter the array size: "); // ask the user for an array size - int size; - fscanf(stdin, "%d", &size); - char buf[size]; - fgets(buf, sizeof buf, stdin); - - // strtoul parses a string to an unsigned integer - size_t size2 = strtoul(buf, NULL, 10); - int var_length_array[size2]; // declare the VLA + int array_size; + fscanf(stdin, "%d", &array_size); + int var_length_array[array_size]; // declare the VLA printf("sizeof array = %zu\n", sizeof var_length_array); - // A possible outcome of this program may be: + // Example: // > Enter the array size: 10 // > sizeof array = 40 @@ -244,9 +236,9 @@ int main (int argc, char** argv) z = (e > f) ? e : f; // => 10 "if e > f return e, else return f." // Increment and decrement operators: - char *s = "iLoveC"; + char *s = "ILoveC"; int j = 0; - s[j++]; // => "i". Returns the j-th item of 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 j-th value of s. // same with j-- and --j @@ -318,9 +310,15 @@ int main (int argc, char** argv) case 1: printf("Huh, 'a' equals 1!\n"); break; + // Be careful - without a "break", execution continues until the + // next "break" is reached. + case 3: + case 4: + printf("Look at that.. 'a' is either 3, or 4\n"); + break; default: // if `some_integral_expression` didn't match any of the labels - fputs("error!\n", stderr); + fputs("Error!\n", stderr); exit(-1); break; } @@ -345,8 +343,7 @@ int main (int argc, char** argv) https://ideone.com/GuPhd6 this will print out "Error occured at i = 52 & j = 99." */ - - + /////////////////////////////////////// // Typecasting /////////////////////////////////////// @@ -385,7 +382,6 @@ int main (int argc, char** argv) // (%p formats an object pointer of type void *) // => Prints some address in memory; - // Pointers start with * in their declaration int *px, not_a_pointer; // px is a pointer to an int px = &x; // Stores the address of x in px @@ -431,7 +427,6 @@ int main (int argc, char** argv) printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); // probably prints "40, 4" or "40, 8" - // Pointers are incremented and decremented based on their type // (this is called pointer arithmetic) printf("%d\n", *(x_ptr + 1)); // => Prints 19 @@ -445,7 +440,7 @@ int main (int argc, char** argv) for (xx = 0; xx < 20; xx++) { *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) - + // Note that there is no standard way to get the length of a // dynamically allocated array in C. Because of this, if your arrays are // going to be passed around your program a lot, you need another variable @@ -454,7 +449,8 @@ int main (int argc, char** argv) int size = 10; int *my_arr = malloc(sizeof(int) * size); // Add an element to the array - my_arr = realloc(my_arr, ++size); + size++; + my_arr = realloc(my_arr, sizeof(int) * size); my_arr[10] = 5; // Dereferencing memory that you haven't allocated gives @@ -496,7 +492,7 @@ int add_two_ints(int x1, int x2) /* 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 +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. @@ -577,8 +573,6 @@ void testFunc2() { } //**You may also declare functions as static to make them private** - - /////////////////////////////////////// // User-defined types and structs /////////////////////////////////////// @@ -695,6 +689,7 @@ typedef void (*my_fnp_type)(char *); "%o"; // octal "%%"; // prints % */ + /////////////////////////////////////// // Order of Evaluation /////////////////////////////////////// @@ -721,13 +716,13 @@ typedef void (*my_fnp_type)(char *); /******************************* Header Files ********************************** -Header files are an important part of c as they allow for the connection of c -source files and can simplify code and definitions by seperating them into -seperate files. +Header files are an important part of c as they allow for the connection of c +source files and can simplify code and definitions by separating them into +separate files. -Header files are syntaxtically similar to c source files but reside in ".h" -files. They can be included in your c source file by using the precompiler -command #include "example.h", given that example.h exists in the same directory +Header files are syntactically similar to c source files but reside in ".h" +files. They can be included in your c source file by using the precompiler +command #include "example.h", given that example.h exists in the same directory as the c file. */ @@ -763,7 +758,7 @@ enum traffic_light_state {GREEN, YELLOW, RED}; Node createLinkedList(int *vals, int len); /* Beyond the above elements, other definitions should be left to a c source */ -/* file. Excessive includeds or definitions should, also not be contained in */ +/* file. Excessive includes or definitions should, also not be contained in */ /* a header file but instead put into separate headers or a c file. */ #endif /* End of the if precompiler directive. */ |