mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
parent
57c384420e
commit
15353d1938
110
c.html.markdown
110
c.html.markdown
@ -16,15 +16,15 @@ memory management and C will take you as far as you need to go.
|
||||
```c
|
||||
// Single-line comments start with // - only available in C99 and later.
|
||||
|
||||
/*
|
||||
/*
|
||||
Multi-line comments look like this. They work in C89 as well.
|
||||
*/
|
||||
*/
|
||||
|
||||
// Constants: #define <keyword>
|
||||
// Constants: #define <keyword>
|
||||
#define DAYS_IN_YEAR 365
|
||||
|
||||
// Enumeration constants are also ways to declare constants.
|
||||
enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
|
||||
// Enumeration constants are also ways to declare constants.
|
||||
enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
|
||||
// MON gets 2 automatically, TUE gets 3, etc.
|
||||
|
||||
// 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.)
|
||||
// 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
|
||||
// your .c file.
|
||||
void function_1(char c);
|
||||
void function_1();
|
||||
int function_2(void);
|
||||
|
||||
// Must declare a 'function prototype' before main() when functions occur after
|
||||
@ -84,8 +84,8 @@ int main() {
|
||||
unsigned long long ux_long_long;
|
||||
|
||||
// chars inside single quotes are integers in machine's character set.
|
||||
'0' // => 48 in the ASCII character set.
|
||||
'A' // => 65 in the ASCII character set.
|
||||
'0'; // => 48 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(obj) yields the size of the expression (variable, literal, etc.).
|
||||
@ -127,8 +127,8 @@ int main() {
|
||||
fgets(buf, sizeof buf, stdin);
|
||||
|
||||
// strtoul parses a string to an unsigned integer
|
||||
size_t size = strtoul(buf, NULL, 10);
|
||||
int var_length_array[size]; // declare the VLA
|
||||
size_t size2 = strtoul(buf, NULL, 10);
|
||||
int var_length_array[size2]; // declare the VLA
|
||||
printf("sizeof array = %zu\n", sizeof var_length_array);
|
||||
|
||||
// A possible outcome of this program may be:
|
||||
@ -166,8 +166,8 @@ int main() {
|
||||
int i1 = 1, i2 = 2;
|
||||
float f1 = 1.0, f2 = 2.0;
|
||||
|
||||
int a, b, c;
|
||||
a = b = c = 0;
|
||||
int b, c;
|
||||
b = c = 0;
|
||||
|
||||
// Arithmetic is straightforward
|
||||
i1 + i2; // => 3
|
||||
@ -194,9 +194,8 @@ int main() {
|
||||
2 >= 2; // => 1
|
||||
|
||||
// C is not Python - comparisons don't chain.
|
||||
int a = 1;
|
||||
// WRONG:
|
||||
int between_0_and_2 = 0 < a < 2;
|
||||
//int between_0_and_2 = 0 < a < 2;
|
||||
// Correct:
|
||||
int between_0_and_2 = 0 < a && a < 2;
|
||||
|
||||
@ -209,8 +208,8 @@ int main() {
|
||||
0 || 0; // => 0
|
||||
|
||||
//Conditional expression ( ? : )
|
||||
int a = 5;
|
||||
int b = 10;
|
||||
int e = 5;
|
||||
int f = 10;
|
||||
int z;
|
||||
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()
|
||||
switch (some_integral_expression) {
|
||||
switch (a) {
|
||||
case 0: // labels need to be integral *constant* expressions
|
||||
do_stuff();
|
||||
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 *`!
|
||||
// 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:
|
||||
char arr[] = "foobarbazquirk";
|
||||
char otherarr[] = "foobarbazquirk";
|
||||
// or when it's the argument of the `sizeof` or `alignof` operator:
|
||||
int arr[10];
|
||||
int *ptr = arr; // equivalent with int *ptr = &arr[0];
|
||||
printf("%zu, %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8"
|
||||
int arraythethird[10];
|
||||
int *ptr = arraythethird; // equivalent with int *ptr = &arr[0];
|
||||
printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr); // probably prints "40, 4" or "40, 8"
|
||||
|
||||
|
||||
// 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
|
||||
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
|
||||
argument where the function was called.
|
||||
|
||||
@ -467,9 +466,9 @@ void testFunc() {
|
||||
}
|
||||
|
||||
//make external variables private to source file with static:
|
||||
static int i = 0; //other files using testFunc() cannot access variable i
|
||||
void testFunc() {
|
||||
extern int i;
|
||||
static int j = 0; //other files using testFunc() cannot access variable i
|
||||
void testFunc2() {
|
||||
extern int j;
|
||||
}
|
||||
//**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
|
||||
// the whole struct:
|
||||
int area(const rect *r)
|
||||
int areaptr(const rect *r)
|
||||
{
|
||||
return r->width * r->height;
|
||||
}
|
||||
@ -560,36 +559,37 @@ typedef void (*my_fnp_type)(char *);
|
||||
// my_fnp_type f;
|
||||
|
||||
//Special characters:
|
||||
'\a' // alert (bell) character
|
||||
'\n' // newline character
|
||||
'\t' // tab character (left justifies text)
|
||||
'\v' // vertical tab
|
||||
'\f' // new page (form feed)
|
||||
'\r' // carriage return
|
||||
'\b' // backspace character
|
||||
'\0' // NULL character. Usually put at end of strings in C.
|
||||
// hello\n\0. \0 used by convention to mark end of string.
|
||||
'\\' // backslash
|
||||
'\?' // question mark
|
||||
'\'' // single quote
|
||||
'\"' // double quote
|
||||
'\xhh' // hexadecimal number. Example: '\xb' = vertical tab character
|
||||
'\ooo' // octal number. Example: '\013' = vertical tab character
|
||||
/*
|
||||
'\a'; // alert (bell) character
|
||||
'\n'; // newline character
|
||||
'\t'; // tab character (left justifies text)
|
||||
'\v'; // vertical tab
|
||||
'\f'; // new page (form feed)
|
||||
'\r'; // carriage return
|
||||
'\b'; // backspace character
|
||||
'\0'; // NULL character. Usually put at end of strings in C.
|
||||
// hello\n\0. \0 used by convention to mark end of string.
|
||||
'\\'; // backslash
|
||||
'\?'; // question mark
|
||||
'\''; // single quote
|
||||
'\"'; // double quote
|
||||
'\xhh'; // hexadecimal number. Example: '\xb' = vertical tab character
|
||||
'\ooo'; // octal number. Example: '\013' = vertical tab character
|
||||
|
||||
//print formatting:
|
||||
"%d" // integer
|
||||
"%3d" // integer with minimum of length 3 digits (right justifies text)
|
||||
"%s" // string
|
||||
"%f" // float
|
||||
"%ld" // long
|
||||
"%3.2f" // minimum 3 digits left and 2 digits right decimal float
|
||||
"%7.4s" // (can do with strings too)
|
||||
"%c" // char
|
||||
"%p" // pointer
|
||||
"%x" // hexadecimal
|
||||
"%o" // octal
|
||||
"%%" // prints %
|
||||
|
||||
"%d"; // integer
|
||||
"%3d"; // integer with minimum of length 3 digits (right justifies text)
|
||||
"%s"; // string
|
||||
"%f"; // float
|
||||
"%ld"; // long
|
||||
"%3.2f"; // minimum 3 digits left and 2 digits right decimal float
|
||||
"%7.4s"; // (can do with strings too)
|
||||
"%c"; // char
|
||||
"%p"; // pointer
|
||||
"%x"; // hexadecimal
|
||||
"%o"; // octal
|
||||
"%%"; // prints %
|
||||
*/
|
||||
///////////////////////////////////////
|
||||
// Order of Evaluation
|
||||
///////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user