mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-17 13:38:38 +01:00
Made learnc's switch statement clearer
Also cleaned up trailing whitespace.
This commit is contained in:
parent
50fe4d9859
commit
a9f51d5bbf
@ -23,9 +23,9 @@ 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
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -43,7 +43,7 @@ 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
|
||||||
// your main() function.
|
// your main() function.
|
||||||
int add_two_ints(int x1, int x2); // function prototype
|
int add_two_ints(int x1, int x2); // function prototype
|
||||||
|
|
||||||
// Your program's entry point is a function called
|
// Your program's entry point is a function called
|
||||||
// main with an integer return type.
|
// main with an integer return type.
|
||||||
@ -57,7 +57,7 @@ int main() {
|
|||||||
// Types
|
// Types
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
// ints are usually 4 bytes
|
// ints are usually 4 bytes
|
||||||
int x_int = 0;
|
int x_int = 0;
|
||||||
|
|
||||||
// shorts are usually 2 bytes
|
// shorts are usually 2 bytes
|
||||||
@ -70,7 +70,7 @@ int main() {
|
|||||||
// longs are often 4 to 8 bytes; long longs are guaranteed to be at least
|
// longs are often 4 to 8 bytes; long longs are guaranteed to be at least
|
||||||
// 64 bits
|
// 64 bits
|
||||||
long x_long = 0;
|
long x_long = 0;
|
||||||
long long x_long_long = 0;
|
long long x_long_long = 0;
|
||||||
|
|
||||||
// floats are usually 32-bit floating point numbers
|
// floats are usually 32-bit floating point numbers
|
||||||
float x_float = 0.0;
|
float x_float = 0.0;
|
||||||
@ -83,9 +83,9 @@ int main() {
|
|||||||
unsigned int ux_int;
|
unsigned int ux_int;
|
||||||
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.).
|
||||||
@ -96,7 +96,7 @@ int main() {
|
|||||||
// is not evaluated (except VLAs (see below)).
|
// is not evaluated (except VLAs (see below)).
|
||||||
// The value it yields in this case is a compile-time constant.
|
// The value it yields in this case is a compile-time constant.
|
||||||
int a = 1;
|
int a = 1;
|
||||||
// size_t is an unsigned integer type of at least 2 bytes used to represent
|
// size_t is an unsigned integer type of at least 2 bytes used to represent
|
||||||
// the size of an object.
|
// the size of an object.
|
||||||
size_t size = sizeof(a++); // a++ is not evaluated
|
size_t size = sizeof(a++); // a++ is not evaluated
|
||||||
printf("sizeof(a++) = %zu where a = %d\n", size, a);
|
printf("sizeof(a++) = %zu where a = %d\n", size, a);
|
||||||
@ -163,7 +163,7 @@ int main() {
|
|||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
// Shorthands for multiple declarations:
|
// Shorthands for multiple declarations:
|
||||||
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 b, c;
|
int b, c;
|
||||||
@ -184,7 +184,7 @@ int main() {
|
|||||||
// Comparison operators are probably familiar, but
|
// Comparison operators are probably familiar, but
|
||||||
// there is no Boolean type in c. We use ints instead.
|
// there is no Boolean type in c. We use ints instead.
|
||||||
// (Or _Bool or bool in C99.)
|
// (Or _Bool or bool in C99.)
|
||||||
// 0 is false, anything else is true. (The comparison
|
// 0 is false, anything else is true. (The comparison
|
||||||
// operators always yield 0 or 1.)
|
// operators always yield 0 or 1.)
|
||||||
3 == 2; // => 0 (false)
|
3 == 2; // => 0 (false)
|
||||||
3 != 2; // => 1 (true)
|
3 != 2; // => 1 (true)
|
||||||
@ -211,14 +211,14 @@ int main() {
|
|||||||
int e = 5;
|
int e = 5;
|
||||||
int f = 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."
|
||||||
|
|
||||||
//Increment and decrement operators:
|
//Increment and decrement operators:
|
||||||
char *s = "iLoveC";
|
char *s = "iLoveC";
|
||||||
int j = 0;
|
int j = 0;
|
||||||
s[j++]; // => "i". Returns the j-th item of s THEN increments value of j.
|
s[j++]; // => "i". Returns the j-th item of s THEN increments value of j.
|
||||||
j = 0;
|
j = 0;
|
||||||
s[++j]; // => "L". Increments value of j THEN returns j-th value of s.
|
s[++j]; // => "L". Increments value of j THEN returns j-th value of s.
|
||||||
// same with j-- and --j
|
// same with j-- and --j
|
||||||
|
|
||||||
// Bitwise operators!
|
// Bitwise operators!
|
||||||
@ -249,7 +249,7 @@ int main() {
|
|||||||
|
|
||||||
// While loops exist
|
// While loops exist
|
||||||
int ii = 0;
|
int ii = 0;
|
||||||
while (ii < 10) { //ANY value not zero is true.
|
while (ii < 10) { //ANY value not zero is true.
|
||||||
printf("%d, ", ii++); // ii++ increments ii AFTER using its current value.
|
printf("%d, ", ii++); // ii++ increments ii AFTER using its current value.
|
||||||
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
||||||
|
|
||||||
@ -281,10 +281,10 @@ int main() {
|
|||||||
// branching with multiple choices: switch()
|
// branching with multiple choices: switch()
|
||||||
switch (a) {
|
switch (a) {
|
||||||
case 0: // labels need to be integral *constant* expressions
|
case 0: // labels need to be integral *constant* expressions
|
||||||
//do_stuff();
|
printf("Hey, 'a' equals 0!\n");
|
||||||
break; // if you don't break, control flow falls over labels
|
break; // if you don't break, control flow falls over labels
|
||||||
case 1:
|
case 1:
|
||||||
//do_something_else();
|
printf("Huh, 'a' equals 1!\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// if `some_integral_expression` didn't match any of the labels
|
// if `some_integral_expression` didn't match any of the labels
|
||||||
@ -292,7 +292,6 @@ int main() {
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Typecasting
|
// Typecasting
|
||||||
@ -324,7 +323,7 @@ int main() {
|
|||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
// A pointer is a variable declared to store a memory address. Its declaration will
|
// A pointer is a variable declared to store a memory address. Its declaration will
|
||||||
// also tell you the type of data it points to. You can retrieve the memory address
|
// also tell you the type of data it points to. You can retrieve the memory address
|
||||||
// of your variables, then mess with them.
|
// of your variables, then mess with them.
|
||||||
|
|
||||||
int x = 0;
|
int x = 0;
|
||||||
@ -362,7 +361,7 @@ int main() {
|
|||||||
|
|
||||||
// Declare a pointer of type int and initialize it to point to x_array
|
// Declare a pointer of type int and initialize it to point to x_array
|
||||||
int* x_ptr = x_array;
|
int* x_ptr = x_array;
|
||||||
// x_ptr now points to the first element in the array (the integer 20).
|
// x_ptr now points to the first element in the array (the integer 20).
|
||||||
// This works because arrays often decay into pointers to their first element.
|
// This works because arrays often decay into pointers to their first element.
|
||||||
// For example, when an array is passed to a function or is assigned to a pointer,
|
// For example, when an array is passed to a function or is assigned to a pointer,
|
||||||
// it decays into (implicitly converted to) a pointer.
|
// it decays into (implicitly converted to) a pointer.
|
||||||
@ -396,7 +395,7 @@ int main() {
|
|||||||
// "unpredictable results" - the program is said to invoke "undefined behavior"
|
// "unpredictable results" - the program is said to invoke "undefined behavior"
|
||||||
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash.
|
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash.
|
||||||
|
|
||||||
// When you're done with a malloc'd block of memory, you need to free it,
|
// When you're done with a malloc'd block of memory, you need to free it,
|
||||||
// or else no one else can use it until your program terminates
|
// or else no one else can use it until your program terminates
|
||||||
// (this is called a "memory leak"):
|
// (this is called a "memory leak"):
|
||||||
free(my_ptr);
|
free(my_ptr);
|
||||||
@ -430,12 +429,12 @@ 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.
|
||||||
|
|
||||||
Use pointers if you need to edit the original argument values.
|
Use pointers if you need to edit the original argument values.
|
||||||
|
|
||||||
Example: in-place string reversal
|
Example: in-place string reversal
|
||||||
*/
|
*/
|
||||||
@ -528,18 +527,18 @@ int areaptr(const rect *r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Function pointers
|
// Function pointers
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
/*
|
/*
|
||||||
At run time, functions are located at known memory addresses. Function pointers are
|
At run time, functions are located at known memory addresses. Function pointers are
|
||||||
much like any other pointer (they just store a memory address), but can be used
|
much like any other pointer (they just store a memory address), but can be used
|
||||||
to invoke functions directly, and to pass handlers (or callback functions) around.
|
to invoke functions directly, and to pass handlers (or callback functions) around.
|
||||||
However, definition syntax may be initially confusing.
|
However, definition syntax may be initially confusing.
|
||||||
|
|
||||||
Example: use str_reverse from a pointer
|
Example: use str_reverse from a pointer
|
||||||
*/
|
*/
|
||||||
void str_reverse_through_pointer(char *str_in) {
|
void str_reverse_through_pointer(char *str_in) {
|
||||||
// Define a function pointer variable, named f.
|
// Define a function pointer variable, named f.
|
||||||
void (*f)(char *); // Signature should exactly match the target function.
|
void (*f)(char *); // Signature should exactly match the target function.
|
||||||
f = &str_reverse; // Assign the address for the actual function (determined at run time)
|
f = &str_reverse; // Assign the address for the actual function (determined at run time)
|
||||||
// f = str_reverse; would work as well - functions decay into pointers, similar to arrays
|
// f = str_reverse; would work as well - functions decay into pointers, similar to arrays
|
||||||
@ -556,7 +555,7 @@ typedef void (*my_fnp_type)(char *);
|
|||||||
|
|
||||||
// Then used when declaring the actual pointer variable:
|
// Then used when declaring the actual pointer variable:
|
||||||
// ...
|
// ...
|
||||||
// my_fnp_type f;
|
// my_fnp_type f;
|
||||||
|
|
||||||
//Special characters:
|
//Special characters:
|
||||||
/*
|
/*
|
||||||
@ -567,8 +566,8 @@ typedef void (*my_fnp_type)(char *);
|
|||||||
'\f'; // new page (form feed)
|
'\f'; // new page (form feed)
|
||||||
'\r'; // carriage return
|
'\r'; // carriage return
|
||||||
'\b'; // backspace character
|
'\b'; // backspace character
|
||||||
'\0'; // NULL character. Usually put at end of strings in C.
|
'\0'; // NULL character. Usually put at end of strings in C.
|
||||||
// hello\n\0. \0 used by convention to mark end of string.
|
// hello\n\0. \0 used by convention to mark end of string.
|
||||||
'\\'; // backslash
|
'\\'; // backslash
|
||||||
'\?'; // question mark
|
'\?'; // question mark
|
||||||
'\''; // single quote
|
'\''; // single quote
|
||||||
@ -582,13 +581,13 @@ typedef void (*my_fnp_type)(char *);
|
|||||||
"%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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user