Fix c tutorial

Fixes #678

Still missing symbols, though
This commit is contained in:
Nami-Doc 2014-07-17 10:46:24 +02:00
parent 57c384420e
commit 15353d1938

View File

@ -16,15 +16,15 @@ memory management and C will take you as far as you need to go.
```c ```c
// Single-line comments start with // - only available in C99 and later. // Single-line comments start with // - only available in C99 and later.
/* /*
Multi-line comments look like this. They work in C89 as well. Multi-line comments look like this. They work in C89 as well.
*/ */
// Constants: #define <keyword> // Constants: #define <keyword>
#define DAYS_IN_YEAR 365 #define DAYS_IN_YEAR 365
// Enumeration constants are also ways to declare constants. // Enumeration constants are also ways to declare constants.
enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT}; enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
// MON gets 2 automatically, TUE gets 3, etc. // MON gets 2 automatically, TUE gets 3, etc.
// Import headers with #include // Import headers with #include
@ -34,11 +34,11 @@ enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
// (File names between <angle brackets> are headers from the C standard library.) // (File names between <angle brackets> are headers from the C standard library.)
// For your own headers, use double quotes instead of angle brackets: // For your own headers, use double quotes instead of angle brackets:
#include "my_header.h" //#include "my_header.h"
// Declare function signatures in advance in a .h file, or at the top of // Declare function signatures in advance in a .h file, or at the top of
// your .c file. // your .c file.
void function_1(char c); void function_1();
int function_2(void); int function_2(void);
// Must declare a 'function prototype' before main() when functions occur after // Must declare a 'function prototype' before main() when functions occur after
@ -84,8 +84,8 @@ int main() {
unsigned long long ux_long_long; unsigned long long ux_long_long;
// chars inside single quotes are integers in machine's character set. // chars inside single quotes are integers in machine's character set.
'0' // => 48 in the ASCII character set. '0'; // => 48 in the ASCII character set.
'A' // => 65 in the ASCII character set. 'A'; // => 65 in the ASCII character set.
// sizeof(T) gives you the size of a variable with type T in bytes // sizeof(T) gives you the size of a variable with type T in bytes
// sizeof(obj) yields the size of the expression (variable, literal, etc.). // sizeof(obj) yields the size of the expression (variable, literal, etc.).
@ -127,8 +127,8 @@ int main() {
fgets(buf, sizeof buf, stdin); fgets(buf, sizeof buf, stdin);
// strtoul parses a string to an unsigned integer // strtoul parses a string to an unsigned integer
size_t size = strtoul(buf, NULL, 10); size_t size2 = strtoul(buf, NULL, 10);
int var_length_array[size]; // declare the VLA int var_length_array[size2]; // declare the VLA
printf("sizeof array = %zu\n", sizeof var_length_array); printf("sizeof array = %zu\n", sizeof var_length_array);
// A possible outcome of this program may be: // A possible outcome of this program may be:
@ -166,8 +166,8 @@ int main() {
int i1 = 1, i2 = 2; int i1 = 1, i2 = 2;
float f1 = 1.0, f2 = 2.0; float f1 = 1.0, f2 = 2.0;
int a, b, c; int b, c;
a = b = c = 0; b = c = 0;
// Arithmetic is straightforward // Arithmetic is straightforward
i1 + i2; // => 3 i1 + i2; // => 3
@ -194,9 +194,8 @@ int main() {
2 >= 2; // => 1 2 >= 2; // => 1
// C is not Python - comparisons don't chain. // C is not Python - comparisons don't chain.
int a = 1;
// WRONG: // WRONG:
int between_0_and_2 = 0 < a < 2; //int between_0_and_2 = 0 < a < 2;
// Correct: // Correct:
int between_0_and_2 = 0 < a && a < 2; int between_0_and_2 = 0 < a && a < 2;
@ -209,8 +208,8 @@ int main() {
0 || 0; // => 0 0 || 0; // => 0
//Conditional expression ( ? : ) //Conditional expression ( ? : )
int a = 5; int e = 5;
int b = 10; int f = 10;
int z; int z;
z = (a > b) ? a : b; // => 10 "if a > b return a, else return b." z = (a > b) ? a : b; // => 10 "if a > b return a, else return b."
@ -280,7 +279,7 @@ int main() {
} }
// branching with multiple choices: switch() // branching with multiple choices: switch()
switch (some_integral_expression) { switch (a) {
case 0: // labels need to be integral *constant* expressions case 0: // labels need to be integral *constant* expressions
do_stuff(); do_stuff();
break; // if you don't break, control flow falls over labels break; // if you don't break, control flow falls over labels
@ -372,11 +371,11 @@ int main() {
int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`! int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`!
// It's of type "pointer to array" (of ten `int`s). // It's of type "pointer to array" (of ten `int`s).
// or when the array is a string literal used for initializing a char array: // or when the array is a string literal used for initializing a char array:
char arr[] = "foobarbazquirk"; char otherarr[] = "foobarbazquirk";
// or when it's the argument of the `sizeof` or `alignof` operator: // or when it's the argument of the `sizeof` or `alignof` operator:
int arr[10]; int arraythethird[10];
int *ptr = arr; // equivalent with int *ptr = &arr[0]; int *ptr = arraythethird; // equivalent with int *ptr = &arr[0];
printf("%zu, %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8" printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); // probably prints "40, 4" or "40, 8"
// Pointers are incremented and decremented based on their type // Pointers are incremented and decremented based on their type
@ -432,7 +431,7 @@ int add_two_ints(int x1, int x2)
/* /*
Functions are call by value. When a function is called, the arguments passed to 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 do to the arguments in the function do not change the value of the original
argument where the function was called. argument where the function was called.
@ -467,9 +466,9 @@ void testFunc() {
} }
//make external variables private to source file with static: //make external variables private to source file with static:
static int i = 0; //other files using testFunc() cannot access variable i static int j = 0; //other files using testFunc() cannot access variable i
void testFunc() { void testFunc2() {
extern int i; extern int j;
} }
//**You may also declare functions as static to make them private** //**You may also declare functions as static to make them private**
@ -523,7 +522,7 @@ int area(rect r)
// if you have large structs, you can pass them "by pointer" to avoid copying // if you have large structs, you can pass them "by pointer" to avoid copying
// the whole struct: // the whole struct:
int area(const rect *r) int areaptr(const rect *r)
{ {
return r->width * r->height; return r->width * r->height;
} }
@ -560,36 +559,37 @@ typedef void (*my_fnp_type)(char *);
// my_fnp_type f; // my_fnp_type f;
//Special characters: //Special characters:
'\a' // alert (bell) character /*
'\n' // newline character '\a'; // alert (bell) character
'\t' // tab character (left justifies text) '\n'; // newline character
'\v' // vertical tab '\t'; // tab character (left justifies text)
'\f' // new page (form feed) '\v'; // vertical tab
'\r' // carriage return '\f'; // new page (form feed)
'\b' // backspace character '\r'; // carriage return
'\0' // NULL character. Usually put at end of strings in C. '\b'; // backspace character
// hello\n\0. \0 used by convention to mark end of string. '\0'; // NULL character. Usually put at end of strings in C.
'\\' // backslash // hello\n\0. \0 used by convention to mark end of string.
'\?' // question mark '\\'; // backslash
'\'' // single quote '\?'; // question mark
'\"' // double quote '\''; // single quote
'\xhh' // hexadecimal number. Example: '\xb' = vertical tab character '\"'; // double quote
'\ooo' // octal number. Example: '\013' = vertical tab character '\xhh'; // hexadecimal number. Example: '\xb' = vertical tab character
'\ooo'; // octal number. Example: '\013' = vertical tab character
//print formatting: //print formatting:
"%d" // integer "%d"; // integer
"%3d" // integer with minimum of length 3 digits (right justifies text) "%3d"; // integer with minimum of length 3 digits (right justifies text)
"%s" // string "%s"; // string
"%f" // float "%f"; // float
"%ld" // long "%ld"; // long
"%3.2f" // minimum 3 digits left and 2 digits right decimal float "%3.2f"; // minimum 3 digits left and 2 digits right decimal float
"%7.4s" // (can do with strings too) "%7.4s"; // (can do with strings too)
"%c" // char "%c"; // char
"%p" // pointer "%p"; // pointer
"%x" // hexadecimal "%x"; // hexadecimal
"%o" // octal "%o"; // octal
"%%" // prints % "%%"; // prints %
*/
/////////////////////////////////////// ///////////////////////////////////////
// Order of Evaluation // Order of Evaluation
/////////////////////////////////////// ///////////////////////////////////////