mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Update c.html.markdown
Fixes #2846, #2849, #2851, #2852 (Thanks to @noncombatant)
This commit is contained in:
parent
67e2ff0162
commit
a6e460618d
@ -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:
|
||||
@ -439,16 +448,24 @@ int main (int argc, char** argv)
|
||||
*(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. */
|
||||
|
Loading…
Reference in New Issue
Block a user