From cc782a7cf279cf607872b86815322d617f0e2468 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Asselin Date: Sun, 12 Feb 2017 13:33:22 -0500 Subject: fix further reading url (#2655) Original link displays "This file has moved to process/coding-style.rst" --- 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 57ce72d2..6d111be6 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -775,7 +775,7 @@ If you have a question, read the [compl.lang.c Frequently Asked Questions](http: It's very important to use proper spacing, indentation and to be consistent with your coding style in general. Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the -[Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle). +[Linux kernel coding style](https://www.kernel.org/doc/Documentation/process/coding-style.rst). Other than that, Google is your friend. -- cgit v1.2.3 From 98f213bc68fe549df5ddcbcc131036ab6cae72ea Mon Sep 17 00:00:00 2001 From: Tyler Chong Date: Mon, 22 May 2017 22:37:46 -1000 Subject: Update c.html.markdown (#2734) --- 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 6d111be6..3da269f9 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -269,7 +269,7 @@ int main (int argc, char** argv) // While loops exist int ii = 0; - while (ii < 10) { //ANY value not zero is true. + while (ii < 10) { //ANY value less than ten is true. printf("%d, ", ii++); // ii++ increments ii AFTER using its current value. } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " -- cgit v1.2.3 From bea057020fb1a410fcb0f60a925deaf464d596a1 Mon Sep 17 00:00:00 2001 From: Aaroh Mankad Date: Fri, 26 May 2017 13:02:32 -0700 Subject: [c/en-en] Make reference link in footer/summary link to Stack Overflow article (#2739) * Make reference link in footer/summary link to Stack Overflow article * remove name from list of contributors --- 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 3da269f9..18503eab 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -779,4 +779,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] [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) -- cgit v1.2.3 From 985d23a52b76593a120adff5381c2df3a80fe298 Mon Sep 17 00:00:00 2001 From: HairyFotr Date: Wed, 23 Aug 2017 10:14:39 +0200 Subject: Fix a bunch of typos --- 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 18503eab..637311ca 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -336,10 +336,10 @@ int main (int argc, char** argv) goto error; } error : - printf("Error occured at i = %d & j = %d.\n", i, j); + printf("Error occurred at i = %d & j = %d.\n", i, j); /* https://ideone.com/GuPhd6 - this will print out "Error occured at i = 52 & j = 99." + this will print out "Error occurred at i = 52 & j = 99." */ /////////////////////////////////////// -- cgit v1.2.3 From a6e460618dad10b5962dbd496f8826902fab6eae Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Tue, 12 Sep 2017 22:32:41 -0700 Subject: Update c.html.markdown Fixes #2846, #2849, #2851, #2852 (Thanks to @noncombatant) --- c.html.markdown | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 637311ca..1ad0dd53 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -16,6 +16,15 @@ C is the lowest-level language most programmers will ever use, but it more than makes up for it with raw speed. Just be aware of its manual memory management and C will take you as far as you need to go. +> **About compiler flags** +> +> By default, gcc and clang are pretty quiet about compilation warnings and +> errors, which can be very useful information. Using some +> stricter compiler flags is recommended. Here is an example you can +> tweak to your liking: +> +> `-Wall -Wextra -Werror -O0 -ansi -pedantic -std=c11` + ```c // Single-line comments start with // - only available in C99 and later. @@ -302,7 +311,7 @@ int main (int argc, char** argv) // branching with multiple choices: switch() switch (a) { - case 0: // labels need to be integral *constant* expressions + case 0: // labels need to be integral *constant* expressions (such as enums) printf("Hey, 'a' equals 0!\n"); break; // if you don't break, control flow falls over labels case 1: @@ -438,17 +447,25 @@ 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) + + // Be careful passing user-provided values to malloc! If you want + // to be safe, you can use calloc instead (which, unlike malloc, also zeros out the memory) + int* my_other_ptr = calloc(20, sizeof(int)); // 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 // to keep track of the number of elements (size) of an array. See the // functions section for more info. - int size = 10; - int *my_arr = malloc(sizeof(int) * size); + size_t size = 10; + int *my_arr = calloc(size, sizeof(int)); // Add an element to the array size++; my_arr = realloc(my_arr, sizeof(int) * size); + if (my_arr == NULL) { + //Remember to check for realloc failure! + return + } my_arr[10] = 5; // Dereferencing memory that you haven't allocated gives @@ -546,7 +563,7 @@ array in C. */ // Size must be passed! // Otherwise, this function has no way of knowing how big the array is. -void printIntArray(int *arr, int size) { +void printIntArray(int *arr, size_t size) { int i; for (i = 0; i < size; i++) { printf("arr[%d] is: %d\n", i, arr[i]); @@ -559,7 +576,7 @@ printIntArray(my_arr, size); // will print "arr[0] is: 1" etc */ -// if referring to external variables outside function, must use extern keyword. +// if referring to external variables outside function, you should use the extern keyword. int i = 0; void testFunc() { extern int i; //i here is now using external variable i @@ -656,6 +673,7 @@ typedef void (*my_fnp_type)(char *); // ... // my_fnp_type f; + //Special characters: /* '\a'; // alert (bell) character @@ -742,10 +760,10 @@ as the C file. #define ADD(a, b) (a + b) /* Structs and typedefs can be used for consistency between files. */ -typedef struct node +typedef struct Node { int val; - struct node *next; + struct Node *next; } Node; /* So can enumerations. */ -- cgit v1.2.3 From a5c23e8af4be114ed5e04c1034acbc6ef4890719 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Tue, 12 Sep 2017 22:42:18 -0700 Subject: Update c.html.markdown Note on macro hygiene. Fixes #2853 --- c.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 1ad0dd53..c47a2df7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -756,8 +756,14 @@ as the C file. /* Like c source files macros can be defined in headers and used in files */ /* that include this header file. */ #define EXAMPLE_NAME "Dennis Ritchie" -/* Function macros can also be defined. */ -#define ADD(a, b) (a + b) + +/* Function macros can also be defined. */ +#define ADD(a, b) ((a) + (b)) + +/* Notice the parenthesis surrounding the arguments -- this is important to */ +/* ensure that a and b don't get expanded in an unexpected way (e.g. consider */ +/* MUL(x, y) (x * y); MUL(1 + 2, 3) would expand to (1 + 2 * 3), yielding an */ +/* incorrect result) */ /* Structs and typedefs can be used for consistency between files. */ typedef struct Node -- cgit v1.2.3 From 33d6dce8ba38070e5f1fe38112e0f17ed48f2192 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Tue, 12 Sep 2017 22:43:55 -0700 Subject: Update c.html.markdown Fix presence of bool (Fixes #2854) --- 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 c47a2df7..1ff8658c 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -333,7 +333,7 @@ int main (int argc, char** argv) using "goto" in C */ typedef enum { false, true } bool; - // for C don't have bool as data type :( + // for C don't have bool as data type before C99 :( bool disaster = false; int i, j; for(i=0;i<100;++i) -- cgit v1.2.3 From 8f990317b79d17953755269d3383d3f94e9c5f7f Mon Sep 17 00:00:00 2001 From: Jakob Rieck Date: Mon, 25 Sep 2017 16:12:53 +0200 Subject: [c/en] Correct off-by-one error --- 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 1ff8658c..87a047be 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -348,7 +348,7 @@ int main (int argc, char** argv) printf("Error occurred at i = %d & j = %d.\n", i, j); /* https://ideone.com/GuPhd6 - this will print out "Error occurred at i = 52 & j = 99." + this will print out "Error occurred at i = 51 & j = 99." */ /////////////////////////////////////// -- cgit v1.2.3 From 95d41ddf6c798131cd343099bcc6b3408657e8c6 Mon Sep 17 00:00:00 2001 From: 0x6a6f7368 Date: Thu, 4 Jan 2018 00:06:44 -0500 Subject: Update c.html.markdown Fixed and improved compiler flag default recommendations. --- 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 87a047be..cfb93aa8 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"] + - ["Joshua Li", "https://github.com/JoshuaRLi"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -23,7 +24,7 @@ memory management and C will take you as far as you need to go. > stricter compiler flags is recommended. Here is an example you can > tweak to your liking: > -> `-Wall -Wextra -Werror -O0 -ansi -pedantic -std=c11` +> `-Wall -Wextra -Werror -O2 -std=c99 -pedantic` ```c // Single-line comments start with // - only available in C99 and later. -- cgit v1.2.3 From 81a637e1dd82ec4625a8e2f6eba3b5d06a7767be Mon Sep 17 00:00:00 2001 From: 0x6a6f7368 Date: Thu, 4 Jan 2018 00:15:28 -0500 Subject: Update c.html.markdown How to get information on compiler flags. --- c.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index cfb93aa8..0c6df413 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -20,11 +20,12 @@ memory management and C will take you as far as you need to go. > **About compiler flags** > > By default, gcc and clang are pretty quiet about compilation warnings and -> errors, which can be very useful information. Using some -> stricter compiler flags is recommended. Here is an example you can -> tweak to your liking: +> errors, which can be very useful information. Explicitly using stricter +> compiler flags is recommended. Here are some recommended defaults: > > `-Wall -Wextra -Werror -O2 -std=c99 -pedantic` +> +> For information on what these flags do as well as other flags, consult the man page for your C compiler (e.g. `man 1 gcc`) or just search online. ```c // Single-line comments start with // - only available in C99 and later. -- cgit v1.2.3 From f1cc661f50976aec5253666f9bb9a6f286f27fdd Mon Sep 17 00:00:00 2001 From: Yoav Date: Sun, 25 Mar 2018 18:04:53 +0300 Subject: Update c.html.markdown --- 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 0c6df413..684d330a 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -101,7 +101,7 @@ int main (int argc, char** argv) char y_char = 'y'; // Char literals are quoted with '' // longs are often 4 to 8 bytes; long longs are guaranteed to be at least - // 64 bits + // 8 bytes long x_long = 0; long long x_long_long = 0; -- cgit v1.2.3 From 5a78f9b3fc8af1370a7a2a9f01abe3a485177079 Mon Sep 17 00:00:00 2001 From: "Dragos B. Chirila" Date: Tue, 4 Sep 2018 17:24:42 +0200 Subject: Add C99 comments, fix some typos and add clarifications in 'c.html.markdown' --- c.html.markdown | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 684d330a..2d54560b 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -9,6 +9,7 @@ contributors: - ["Zachary Ferguson", "https://github.io/zfergus2"] - ["himanshu", "https://github.com/himanshu81494"] - ["Joshua Li", "https://github.com/JoshuaRLi"] + - ["Dragos B. Chirila", "https://github.com/dchirila"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -18,7 +19,7 @@ it more than makes up for it with raw speed. Just be aware of its manual memory management and C will take you as far as you need to go. > **About compiler flags** -> +> > By default, gcc and clang are pretty quiet about compilation warnings and > errors, which can be very useful information. Explicitly using stricter > compiler flags is recommended. Here are some recommended defaults: @@ -89,6 +90,8 @@ int main (int argc, char** argv) // 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 + // (however, C99-compliant compilers allow declarations near the point where + // the value is used) // ints are usually 4 bytes int x_int = 0; @@ -141,6 +144,17 @@ int main (int argc, char** argv) // You can initialize an array to 0 thusly: char my_array[20] = {0}; + // where the "{0}" part is called an "array initializer". + // NOTE that you get away without explicitly declaring the size of the array, + // IF you initialize the array on the same line. So, the following declaration + // is equivalent: + char my_array[] = {0}; + // BUT, then you have to evaluate the size of the array at run-time, like this: + size_t my_array_size = sizeof(my_array) / sizeof(my_array[0]); + // WARNING If you adopt this approach, you should evaluate the size *before* + // you begin passing the array to function (see later discussion), because + // arrays get "downgraded" to raw pointers when they are passed to functions + // (so the statement above will produce the wrong result inside the function). // Indexing an array is like other languages -- or, // rather, other languages are like C @@ -433,7 +447,7 @@ int main (int argc, char** argv) // or when it's the argument of the `sizeof` or `alignof` operator: int arraythethird[10]; int *ptr = arraythethird; // equivalent with int *ptr = &arr[0]; - printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); + printf("%zu, %zu\n", sizeof(arraythethird), sizeof(ptr)); // probably prints "40, 4" or "40, 8" // Pointers are incremented and decremented based on their type @@ -449,7 +463,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) - + // Be careful passing user-provided values to malloc! If you want // to be safe, you can use calloc instead (which, unlike malloc, also zeros out the memory) int* my_other_ptr = calloc(20, sizeof(int)); @@ -522,9 +536,11 @@ Example: in-place string reversal void str_reverse(char *str_in) { char tmp; - int ii = 0; + size_t ii = 0; size_t len = strlen(str_in); // `strlen()` is part of the c standard library - for (ii = 0; ii < len / 2; ii++) { + // NOTE: length returned by `strlen` DOESN'T include the + // terminating NULL byte ('\0') + for (ii = 0; ii < len / 2; ii++) { // in C99 you can directly declare type of `ii` here tmp = str_in[ii]; str_in[ii] = str_in[len - ii - 1]; // ii-th char from end str_in[len - ii - 1] = tmp; @@ -703,7 +719,8 @@ typedef void (*my_fnp_type)(char *); "%3.2f"; // minimum 3 digits left and 2 digits right decimal float "%7.4s"; // (can do with strings too) "%c"; // char -"%p"; // pointer +"%p"; // pointer. NOTE: need to (void *)-cast the pointer, before passing + // it as an argument to `printf`. "%x"; // hexadecimal "%o"; // octal "%%"; // prints % -- cgit v1.2.3 From 70f896eed7d3a3733ebcdf2fd280a7ffa660304b Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Thu, 13 Sep 2018 00:18:37 +0530 Subject: Fix printf, closes #2730 --- 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 2d54560b..bf93dcf5 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -388,8 +388,8 @@ int main (int argc, char** argv) // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from // Integral types can be cast to floating-point types, and vice-versa. - printf("%f\n", (float)100); // %f formats a float - printf("%lf\n", (double)100); // %lf formats a double + printf("%f\n", (double) 100); // %f always formats a double... + printf("%f\n", (float) 100); // ...even with a float. printf("%d\n", (char)100.0); /////////////////////////////////////// -- cgit v1.2.3 From 4311e3866710ed02cef29cd62d66c123b9300f14 Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Tue, 9 Oct 2018 23:10:17 +0530 Subject: Make description of static keyword stronger, closes #2848 --- 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 bf93dcf5..7975a1c2 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -605,6 +605,14 @@ static int j = 0; //other files using testFunc2() cannot access variable j void testFunc2() { extern int j; } +// The static keyword makes a variable inaccessible to code outside the +// compilation unit. (On almost all systems, a "compilation unit" is a .c +// file.) static can apply both to global (to the compilation unit) variables, +// functions, and function-local variables. When using static with +// function-local variables, the variable is effectively global and retains its +// value across function calls, but is only accessible within the function it +// is declared in. Additionally, static variables are initialized to 0 if not +// declared with some other starting value. //**You may also declare functions as static to make them private** /////////////////////////////////////// -- cgit v1.2.3 From da2caced2209b7e60699790715ff84585864e37f Mon Sep 17 00:00:00 2001 From: Heitor Pascoal de Bittencourt Date: Tue, 8 Oct 2019 21:22:47 -0300 Subject: [c/en] Fix link for Learn C the Hard Way book --- 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 7975a1c2..e5ffc379 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -10,6 +10,7 @@ contributors: - ["himanshu", "https://github.com/himanshu81494"] - ["Joshua Li", "https://github.com/JoshuaRLi"] - ["Dragos B. Chirila", "https://github.com/dchirila"] + - ["Heitor P. de Bittencourt", "https://github.com/heitorPB/"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -820,7 +821,7 @@ Best to find yourself a copy of [K&R, aka "The C Programming Language"](https:// It is *the* book about C, written by Dennis Ritchie, the creator of C, and Brian Kernighan. Be careful, though - it's ancient and it contains some inaccuracies (well, ideas that are not considered good anymore) or now-changed practices. -Another good resource is [Learn C The Hard Way](http://c.learncodethehardway.org/book/). +Another good resource is [Learn C The Hard Way](http://learncodethehardway.org/c/). If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com). -- cgit v1.2.3 From bf1fe71a83a244dcfe36c543913829aaaf624f2e Mon Sep 17 00:00:00 2001 From: KoenigKrote Date: Fri, 21 Feb 2020 18:41:29 -0700 Subject: Rephrase variable declaration comments --- c.html.markdown | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index e5ffc379..5b02390d 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -89,10 +89,12 @@ 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 - // (however, C99-compliant compilers allow declarations near the point where - // the value is used) + // Compilers that are not C99-compliant requires that variables MUST be + // declared at the top of the current block scope. + // Compilers that ARE C99-compliant allow declarations near the point where + // the value is used. + // For the sake of the tutorial, variables are declared dynamically under + // C99-compliant standards. // ints are usually 4 bytes int x_int = 0; -- cgit v1.2.3 From 842b247b74a81d8e00dab83875d1f5de15db3cc7 Mon Sep 17 00:00:00 2001 From: KoenigKrote Date: Fri, 21 Feb 2020 18:46:28 -0700 Subject: Requires -> Require --- 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 5b02390d..a57be1dc 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -89,7 +89,7 @@ int main (int argc, char** argv) // Types /////////////////////////////////////// - // Compilers that are not C99-compliant requires that variables MUST be + // Compilers that are not C99-compliant require that variables MUST be // declared at the top of the current block scope. // Compilers that ARE C99-compliant allow declarations near the point where // the value is used. -- cgit v1.2.3