summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Bard <github@adambard.com>2017-09-12 22:32:41 -0700
committerGitHub <noreply@github.com>2017-09-12 22:32:41 -0700
commita6e460618dad10b5962dbd496f8826902fab6eae (patch)
tree68f3bec4e37b63ce157a4c0d5f8f25e5aff75e35
parent67e2ff0162a8c386a51ee2cc49e36a39ad57c0f0 (diff)
Update c.html.markdown
Fixes #2846, #2849, #2851, #2852 (Thanks to @noncombatant)
-rw-r--r--c.html.markdown32
1 files changed, 25 insertions, 7 deletions
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. */