From b31fda3a8e9f4d0ff021dc707f1e47af4add90ac Mon Sep 17 00:00:00 2001 From: Jody Leonard Date: Mon, 26 Oct 2015 19:38:36 -0400 Subject: Edit variable-length array example The current example seems to be trying to set a size for a char buffer, use fgets to populate that buffer, and then use strtoul to convert the char content to an unsigned integer. However, this doesn't work as intended (in fact, it results in printing "sizeof array = 0"), and so adapt to a simpler fscanf example. Also remove some ambiguous language in the example output. --- c.html.markdown | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 3d632eab..7c2386ef 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -148,15 +148,10 @@ int main (int argc, char** argv) 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 var_length_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 -- cgit v1.2.3 From 3b1940b9cc9c0e46a9275b2ae64e4c5996d7de75 Mon Sep 17 00:00:00 2001 From: Alex Luehm Date: Wed, 28 Oct 2015 17:45:31 -0500 Subject: [C/en] Added tidbit about fall-though in switch statements. Another pitfall, as not all languages have fall-through in switches. --- c.html.markdown | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 3d632eab..2a5e460f 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -76,7 +76,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 @@ -318,6 +318,12 @@ 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); @@ -345,8 +351,8 @@ int main (int argc, char** argv) https://ideone.com/GuPhd6 this will print out "Error occured at i = 52 & j = 99." */ - - + + /////////////////////////////////////// // Typecasting /////////////////////////////////////// @@ -445,7 +451,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 @@ -721,13 +727,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 +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 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 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 as the c file. */ -- cgit v1.2.3 From 7560ea819965604099a3ed1dbf4e2fa8919e929b Mon Sep 17 00:00:00 2001 From: George Gognadze Date: Thu, 24 Dec 2015 23:24:09 +0400 Subject: typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit some type mistakes. It is: syntaxtically It should be: syntactically It is: iLoveC Better: ILoveC It is: passed to ≈the function It should be: passed to the function It is: error It should be: Error --- c.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 8226ddef..d92d2ee6 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -239,7 +239,7 @@ 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. j = 0; @@ -321,7 +321,7 @@ int main (int argc, char** argv) break; default: // if `some_integral_expression` didn't match any of the labels - fputs("error!\n", stderr); + fputs("Error!\n", stderr); exit(-1); break; } @@ -497,7 +497,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. @@ -726,7 +726,7 @@ 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 syntaxtically similar to c source files but reside in ".h" +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. -- cgit v1.2.3 From 6981980ad5698e135b4185ef9fc5f3026509d88c Mon Sep 17 00:00:00 2001 From: Jacob Ward Date: Thu, 18 Feb 2016 13:02:55 -0700 Subject: [c/en] typos --- 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 d92d2ee6..d4ff529d 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -723,8 +723,8 @@ 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. +source files and can simplify code and definitions by separating them into +separate files. 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 @@ -764,7 +764,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. */ -- cgit v1.2.3 From bb74c468c2673de7ff92cd63830d93c455e18a33 Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Thu, 3 Mar 2016 11:41:52 +0530 Subject: fixed whitespaces removed whitespaces all over document --- c.html.markdown | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index d4ff529d..06673588 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -8,6 +8,7 @@ contributors: - ["Marco Scannadinari", "https://marcoms.github.io"] - ["Zachary Ferguson", "https://github.io/zfergus2"] - ["himanshu", "https://github.com/himanshu81494"] + - ["Divay Prakash", "https://github.com/divayprakash"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -36,7 +37,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 #include @@ -114,7 +114,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 +129,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}; @@ -347,7 +345,6 @@ int main (int argc, char** argv) this will print out "Error occured at i = 52 & j = 99." */ - /////////////////////////////////////// // Typecasting /////////////////////////////////////// @@ -386,7 +383,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 @@ -432,7 +428,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 @@ -578,8 +573,6 @@ void testFunc2() { } //**You may also declare functions as static to make them private** - - /////////////////////////////////////// // User-defined types and structs /////////////////////////////////////// @@ -696,6 +689,7 @@ typedef void (*my_fnp_type)(char *); "%o"; // octal "%%"; // prints % */ + /////////////////////////////////////// // Order of Evaluation /////////////////////////////////////// @@ -786,4 +780,4 @@ Readable code is better than clever code and fast code. For a good, sane coding Other than that, Google is your friend. -[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member +[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member \ No newline at end of file -- cgit v1.2.3 From f29bc250279790269315aa0258f96134390e3c61 Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Sat, 12 Mar 2016 13:39:06 +0530 Subject: fixed --- c.html.markdown | 1 - 1 file changed, 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 06673588..7c9dd590 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -8,7 +8,6 @@ contributors: - ["Marco Scannadinari", "https://marcoms.github.io"] - ["Zachary Ferguson", "https://github.io/zfergus2"] - ["himanshu", "https://github.com/himanshu81494"] - - ["Divay Prakash", "https://github.com/divayprakash"] --- Ah, C. Still **the** language of modern high-performance computing. -- cgit v1.2.3