From 119fad49bb5431cabdeb49d9fe2ed8aa5f76726a Mon Sep 17 00:00:00 2001 From: FireIsGood Date: Thu, 16 May 2024 03:09:37 -0700 Subject: [PATCH] fix: c format fixed bitwise OR operators for the tables --- source/_posts/c.md | 442 ++++++++++++++++----------------------------- 1 file changed, 154 insertions(+), 288 deletions(-) diff --git a/source/_posts/c.md b/source/_posts/c.md index 77b9db6..d6451b9 100644 --- a/source/_posts/c.md +++ b/source/_posts/c.md @@ -6,15 +6,12 @@ tags: categories: - Programming intro: | - C quick reference cheat sheet that provides basic syntax and methods. + C quick reference cheat sheet that provides basic syntax and methods. plugins: - - copyCode + - copyCode --- -Getting Started ----- - - +## Getting Started ### hello.c {.row-span-2} @@ -42,8 +39,6 @@ $ ./hello Output => Hello World! - - ### Variables {.row-span-2} ```c @@ -66,8 +61,6 @@ int sum = x + y; // add variables to sum int x = 5, y = 6, z = 50; ``` - - ### Constants ```c @@ -81,8 +74,6 @@ Best Practices const int BIRTHYEAR = 1980; ``` - - ### Comment ```c @@ -93,8 +84,6 @@ printf("Hello World!"); // Can comment anywhere in file to the screen, it's awesome */ ``` - - ### Print text ```c @@ -126,8 +115,6 @@ printf("a=%hX, b=%X, c=%lX\n", a, b, c); // output => a=56, b=5CB, c=1DAB83 ``` - - ### Control the number of spaces ```c @@ -145,17 +132,17 @@ printf("%-9d %-9d %-9d\n", d1, d2, d3); output result ```bash -20 345 700 -56720 9999 20098 -233 205 1 -34 0 23 +20 345 700 +56720 9999 20098 +233 205 1 +34 0 23 ``` -In `%-9d`, `d` means to output in `10` base, `9` means to occupy at least `9` characters width, and the width is not enough to fill with spaces, `-` means left alignment - - +In `%-9d`, `d` means to output in `10` base, `9` means to occupy at least `9` characters width, and the width is not +enough to fill with spaces, `-` means left alignment ### Strings + ```c char greetings[] = "Hello World!"; printf("%s", greetings); @@ -188,17 +175,18 @@ printf("%s", greetings); ``` Creating String using character pointer (String Literals) + ```c char *greetings = "Hello"; printf("%s", greetings); // print "Hello!" ``` -**NOTE**: String literals might be stored in read-only section of memory. Modifying a string literal invokes undefined behavior. You can't modify it.! + +**NOTE**: String literals might be stored in read-only section of memory. Modifying a string literal invokes undefined +behavior. You can't modify it.! `C` **does not** have a String type, use `char` type and create an `array` of characters - - ### Condition {.row-span-2} ```c @@ -220,8 +208,6 @@ if (time < 10) { // Output -> "Good evening!" ``` - - ### Ternary operator {.col-span-2} ```c @@ -229,8 +215,6 @@ int age = 20; (age > 19) ? printf("Adult") : printf("Teenager"); ``` - - ### Switch ```c @@ -245,8 +229,6 @@ switch (day) { // output -> "Thursday" (day 4) ``` - - ### While Loop ```c @@ -258,9 +240,8 @@ while (i < 5) { } ``` -**NOTE**: Don't forget to increment the variable used in the condition, otherwise the loop will never end and become an "infinite loop"! - - +**NOTE**: Don't forget to increment the variable used in the condition, otherwise the loop will never end and become an +"infinite loop"! ### Do/While Loop @@ -273,8 +254,6 @@ do { } while (i < 5); ``` - - ### For Loop ```c @@ -283,8 +262,6 @@ for (int i = 0; i < 5; i++) { } ``` - - ### Break out of the loop Break/Continue {.row-span-2} ```c @@ -309,8 +286,6 @@ for (int i = 0; i < 10; i++) { Example to skip the value of `4` - - ### While Break Example ```c @@ -326,8 +301,6 @@ while (i < 10) { } ``` - - ### While continue example ```c @@ -343,8 +316,6 @@ while (i < 10) { } ``` - - ### Arrays {.row-span-2} ```c @@ -387,8 +358,6 @@ myNumbers[2] = 75; myNumbers[3] = 100; ``` - - ### Enumeration Enum {.col-span-2} ```c @@ -411,8 +380,6 @@ enum week a = Mon, b = Wed, c = Sat; enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a = Mon, b = Wed, c = Sat; ``` - - ### Enumerate sample applications ```c @@ -429,8 +396,6 @@ switch(day) { } ``` - - ### User input ```c @@ -447,8 +412,6 @@ scanf("%d", &myNum); printf("The number you entered: %d", myNum); ``` - - ### User input string ```c @@ -462,8 +425,6 @@ scanf("%s", &firstName); printf("Hello %s.", firstName); ``` - - ### memory address When a variable is created, it is assigned a memory address @@ -477,8 +438,6 @@ printf("%p", &myAge); To access it, use the reference operator (`&`) - - ### create pointer ```c @@ -489,8 +448,6 @@ printf("%d", myAge); // output the value of myAge(43) printf("%p", &myAge); ``` - - ### pointer variable {.col-span-2} ```c @@ -503,8 +460,6 @@ printf("%p\n", &myAge); // output the memory address of myAge (0x7ffe5367e044) printf("%p\n", ptr); // use the pointer (0x7ffe5367e044) to output the memory address of myAge ``` - - ### Dereference ```c @@ -518,10 +473,7 @@ printf("%p\n", ptr); printf("%d\n", *ptr); ``` -Operators ---- - - +## Operators ### Arithmetic Operators @@ -532,37 +484,33 @@ int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400) ``` ----- +--- | Operator | Name | Example | -|----------|-----------|---------| +| -------- | --------- | ------- | | `+` | Add | `x + y` | -| `-` | Subtract | `x - y` | -| `*` | Multiply | `x * y` | -| `/` | Divide | `x / y` | +| `-` | Subtract | `x - y` | +| `*` | Multiply | `x * y` | +| `/` | Divide | `x / y` | | `%` | Modulo | `x % y` | -| `++` | Increment | `++x` | +| `++` | Increment | `++x` | | `--` | Decrement | `--x` | - - ### Assignment operator -| example | as | -|-----------|----------------| -| x `=` 5 | x `=` 5 | -| x `+=` 3 | x `=` x `+` 3 | -| x `-=` 3 | x `=` x `-` 3 | -| x `*=` 3 | x `=` x `*` 3 | -| x `/=` 3 | x `=` x `/` 3 | -| x `%=` 3 | x `=` x `%` 3 | -| x `&=` 3 | x `=` x `&` 3 | -| x `|=` 3 | x `=` x `|` 3 | -| x `^=` 3 | x `=` x `^` 3 | -| x `>>=` 3 | x `=` x `>>` 3 | -| x `<<=` 3 | x `=` x `<<` 3 | - - +| example | as | +| -------------------- | ------------------------- | +| x `=` 5 | x `=` 5 | +| x `+=` 3 | x `=` x `+` 3 | +| x `-=` 3 | x `=` x `-` 3 | +| x `*=` 3 | x `=` x `*` 3 | +| x `/=` 3 | x `=` x `/` 3 | +| x `%=` 3 | x `=` x `%` 3 | +| x `&=` 3 | x `=` x `&` 3 | +| x \|= 3 | x `=` x \| 3 | +| x `^=` 3 | x `=` x `^` 3 | +| x `>>=` 3 | x `=` x `>>` 3 | +| x `<<=` 3 | x `=` x `<<` 3 | ### Comparison Operators @@ -574,30 +522,26 @@ printf("%d", x > y); // returns 1 (true) because 5 is greater than 3 ``` ----- +--- -| Symbol | Name | Example | -| --------| -------| -------| -| `==` | equals | x `==` y | -| `!=` | not equal to | x `!=` y | -| `>` | greater than | x `>` y | -| `<` | less than | x `<` y | -| `>=` | greater than or equal to | x `>=` y | -| `<=` | less than or equal to | x `<=` y | +| Symbol | Name | Example | +| ------ | ------------------------ | -------- | +| `==` | equals | x `==` y | +| `!=` | not equal to | x `!=` y | +| `>` | greater than | x `>` y | +| `<` | less than | x `<` y | +| `>=` | greater than or equal to | x `>=` y | +| `<=` | less than or equal to | x `<=` y | Comparison operators are used to compare two values - - ### Logical Operators {.col-span-2} -| Symbol | Name | Description | Example | -| --------| --------| --------| --------| -| `&&` | `and` logical | returns true if both statements are true | `x < 5 && x < 10` | -| `||` | `or` logical | returns true if one of the statements is true | `x < 5 || x < 4` | -| `!` | `not` logical | Invert result, return false if true | `!(x < 5 && x < 10)` | - - +| Symbol | Name | Description | Example | +| ----------------- | ------------- | --------------------------------------------- | ----------------------------- | +| `&&` | `and` logical | returns true if both statements are true | `x < 5 && x < 10` | +| \|\| | `or` logical | returns true if one of the statements is true | x < 5 \|\| x < 4 | +| `!` | `not` logical | Invert result, return false if true | `!(x < 5 && x < 10)` | ### Operator Examples {.row-span-2} @@ -621,45 +565,38 @@ c = a >> 2; /*15 = 0000 1111 */ printf("Line 6 -The value of c is %d\n", c); ``` - - ### Bitwise operators {.col-span-2} -operator | description | instance -:-|:-|:- -`&` | Bitwise AND operation, "AND" operation by binary digits | `(A & B)` will get `12` which is 0000 1100 -`|` | Bitwise OR operator, "or" operation by binary digit | `(A | B)` will get `61` which is 0011 1101 -`^` | XOR operator, perform "XOR" operation by binary digits | `(A ^ B)` will get `49` which is 0011 0001 -`~` | Inversion operator, perform "inversion" operation by binary bit | `(~A)` will get `-61` which is 1100 0011 -`<<` | binary left shift operator | `A << 2` will get `240` which is 1111 0000 -`>>` | binary right shift operator | `A >> 2` will get `15` which is 0000 1111 - -Data Types ---- - +| operator | description | instance | +| :-------------- | :-------------------------------------------------------------- | :---------------------------------------------------- | +| `&` | Bitwise AND operation, "AND" operation by binary digits | `(A & B)` will get `12` which is 0000 1100 | +| \| | Bitwise OR operator, "or" operation by binary digit | (A \| B) will get`61` which is 0011 1101 | +| `^` | XOR operator, perform "XOR" operation by binary digits | `(A ^ B)` will get `49` which is 0011 0001 | +| `~` | Inversion operator, perform "inversion" operation by binary bit | `(~A)` will get `-61` which is 1100 0011 | +| `<<` | binary left shift operator | `A << 2` will get `240` which is 1111 0000 | +| `>>` | binary right shift operator | `A >> 2` will get `15` which is 0000 1111 | +## Data Types ### Basic data types {.col-span-2} -| Data Type | Size Size | Range Range | Description Description | -| -----| -----| -----| -----| -| `char` | 1 byte | `−128` ~ `127` | single character/alphanumeric/ASCII | -| `signed char` | 1 byte | `−128` ~ `127` | -| -| `unsigned char` | 1 byte | `0` ~ `255` | -| -| `int` | `2` to `4` bytes | `−32,768` ~ `32,767` | store integers | -| `signed int` | 2 bytes | `−32,768` ~ `32,767` | | -| `unsigned int` | 2 bytes | `0` ~ `65,535` | | -| `short int` | 2 bytes | `−32,768` ~ `32,767` | | -| `signed short int` | 2 bytes | `−32,768` ~ `32,767` | | -| `unsigned short int` | 2 bytes | `0` ~ `65,535` | | -| `long int` | 4 bytes | `-2,147,483,648` ~ `2,147,483,647` | | -| `signed long int` | 4 bytes | `-2,147,483,648` ~ `2,147,483,647` | | -| `unsigned long int` | 4 bytes | `0` ~ `4,294,967,295` | | -| `float` | 4 bytes | `3.4E-38` ~ `3.4E+38` | | -| `double` | 8 bytes | `1.7E-308` ~ `1.7E+308` | | -| `long double` | 10 bytes | `3.4E-4932` ~ `1.1E+4932` | | - - +| Data Type | Size Size | Range Range | Description Description | +| -------------------- | ---------------- | ---------------------------------- | ----------------------------------- | +| `char` | 1 byte | `−128` ~ `127` | single character/alphanumeric/ASCII | +| `signed char` | 1 byte | `−128` ~ `127` | - | +| `unsigned char` | 1 byte | `0` ~ `255` | - | +| `int` | `2` to `4` bytes | `−32,768` ~ `32,767` | store integers | +| `signed int` | 2 bytes | `−32,768` ~ `32,767` | | +| `unsigned int` | 2 bytes | `0` ~ `65,535` | | +| `short int` | 2 bytes | `−32,768` ~ `32,767` | | +| `signed short int` | 2 bytes | `−32,768` ~ `32,767` | | +| `unsigned short int` | 2 bytes | `0` ~ `65,535` | | +| `long int` | 4 bytes | `-2,147,483,648` ~ `2,147,483,647` | | +| `signed long int` | 4 bytes | `-2,147,483,648` ~ `2,147,483,647` | | +| `unsigned long int` | 4 bytes | `0` ~ `4,294,967,295` | | +| `float` | 4 bytes | `3.4E-38` ~ `3.4E+38` | | +| `double` | 8 bytes | `1.7E-308` ~ `1.7E+308` | | +| `long double` | 10 bytes | `3.4E-4932` ~ `1.1E+4932` | | ### Data types @@ -677,42 +614,36 @@ printf("%c\n", myLetter); printf("%lf\n", myDouble); ``` ----- - -Data Type | Description -:-| :- -`char` | character type -`short` | short integer -`int` | integer type -`long` | long integer -`float` | single-precision floating-point type -`double` | double-precision floating-point type -`void` | no type - +--- +| Data Type | Description | +| :-------- | :----------------------------------- | +| `char` | character type | +| `short` | short integer | +| `int` | integer type | +| `long` | long integer | +| `float` | single-precision floating-point type | +| `double` | double-precision floating-point type | +| `void` | no type | ### Basic format specifiers -| format specifier | data type | -| -----| -----| -| `%d` or `%i` | `int` integer | -| `%f` | `float` single-precision decimal type | -| `%lf` | `double` high precision floating point data or number | -| `%c` | `char` character | -| `%s` | for `strings` strings | - - +| format specifier | data type | +| ---------------- | ----------------------------------------------------- | +| `%d` or `%i` | `int` integer | +| `%f` | `float` single-precision decimal type | +| `%lf` | `double` high precision floating point data or number | +| `%c` | `char` character | +| `%s` | for `strings` strings | ### Basic format specifiers -| | short | int | long | -| ----| ----| ----| ----| -| Octal | `%ho` | `%o` | `%lo` | -| Decimal | `%hd` | `%d` | `%ld` | +| | short | int | long | +| ----------- | ------------ | ---------- | ------------ | +| Octal | `%ho` | `%o` | `%lo` | +| Decimal | `%hd` | `%d` | `%ld` | | Hexadecimal | `%hx` /`%hX` | `%x` /`%X` | `%lx` /`%lX` | - - ### Data format example ```c @@ -725,26 +656,23 @@ printf("%f\n", myFloatNum); printf("%c\n", myLetter); ``` -C Preprocessor ---- - - +## C Preprocessor ### Preprocessor Directives {.row-span-2} -command | description -----| ---- -`#define` | define a macro -`#include` | include a source code file -`#undef` | undefined macro -`#ifdef` | Returns true if the macro is defined -`#ifndef` | Returns true if the macro is not defined -`#if` | Compile the following code if the given condition is true -`#else` | Alternative to `#if` -`#elif` | If the `#if` condition is false, the current condition is `true` -`#endif` | End a `#if...#else` conditional compilation block -`#error` | Print an error message when standard error is encountered -`#pragma` | Issue special commands to the compiler using the standardized method +| command | description | +| ---------- | -------------------------------------------------------------------- | +| `#define` | define a macro | +| `#include` | include a source code file | +| `#undef` | undefined macro | +| `#ifdef` | Returns true if the macro is defined | +| `#ifndef` | Returns true if the macro is not defined | +| `#if` | Compile the following code if the given condition is true | +| `#else` | Alternative to `#if` | +| `#elif` | If the `#if` condition is false, the current condition is `true` | +| `#endif` | End a `#if...#else` conditional compilation block | +| `#error` | Print an error message when standard error is encountered | +| `#pragma` | Issue special commands to the compiler using the standardized method | ```c // replace all MAX_ARRAY_LENGTH with 20 @@ -757,23 +685,20 @@ command | description #define FILE_SIZE 42 // undefine and define to 42 ``` - - ### Predefined macros {.row-span-2} -macro | description -----| ---- -`__DATE__` | The current date, a character constant in the format "MMM DD YYYY" -`__TIME__` | The current time, a character constant in the format "HH:MM:SS" -`__FILE__` | This will contain the current filename, a string constant -`__LINE__` | This will contain the current line number, a decimal constant -`__STDC__` | Defined as `1` when the compiler compiles against the `ANSI` standard +| macro | description | +| ---------- | --------------------------------------------------------------------- | +| `__DATE__` | The current date, a character constant in the format "MMM DD YYYY" | +| `__TIME__` | The current time, a character constant in the format "HH:MM:SS" | +| `__FILE__` | This will contain the current filename, a string constant | +| `__LINE__` | This will contain the current line number, a decimal constant | +| `__STDC__` | Defined as `1` when the compiler compiles against the `ANSI` standard | + `ANSI C` defines a number of macros that you can use, but you cannot directly modify these predefined macros - - #### Predefined macro example ```c @@ -788,8 +713,6 @@ int main() { } ``` - - ### Macro continuation operator (\) A macro is usually written on a single line. @@ -801,8 +724,6 @@ A macro is usually written on a single line. If the macro is too long to fit on a single line, use the macro continuation operator `\` - - ### String Constantization Operator (#) ```c @@ -817,6 +738,7 @@ int main(void) { return 0; } ``` + When the above code is compiled and executed, it produces the following result: ``` @@ -825,8 +747,6 @@ Carole and Debra: We love you! When you need to convert a macro parameter to a string constant, use the string constant operator `#` - - ### tag paste operator (##) ```c @@ -842,8 +762,6 @@ int main(void) { } ``` - - ### defined() operator ```c @@ -860,8 +778,6 @@ int main(void) { } ``` - - ### Parameterized macros ```c @@ -889,10 +805,7 @@ int main(void) { } ``` -C Function ----- - - +## C Function ### Function declaration and definition {.row-span-2} @@ -912,11 +825,12 @@ void myFunction() { // declaration declaration } ``` ----- +--- + - `Declaration` declares the function name, return type and parameters _(if any)_ - `Definition` function body _(code to execute)_ ----- +--- ```c // function declaration @@ -933,8 +847,6 @@ void myFunction() {// Function definition } ``` - - ### Call function ```c @@ -953,8 +865,6 @@ int main() { // Output -> "Good evening!" ``` - - ### Function parameters ```c @@ -972,8 +882,6 @@ int main() { // Hello Jenny ``` - - ### Multiple parameters ```c @@ -990,8 +898,6 @@ int main() { // Hi Jenny you are 14 years old. ``` - - ### Return value {.row-span-2} ```c @@ -1025,8 +931,6 @@ int main() { // result = 8 (5 + 3) ``` - - ### Recursive example ```c @@ -1048,8 +952,6 @@ int sum(int k) { } ``` - - ### Mathematical functions ```c @@ -1063,7 +965,7 @@ void main(void) { } ``` ----- +--- - `abs(x)` absolute value - `acos(x)` arc cosine value @@ -1075,10 +977,7 @@ void main(void) { - `sin(x)` the sine of x - tangent of `tan(x)` angle -C Structures ---- - - +## C Structures ### Create structure @@ -1104,8 +1003,6 @@ int main() { } ``` - - ### Strings in the structure ```c{9} @@ -1127,8 +1024,6 @@ int main() { Assigning values ​​to strings using the `strcpy` function - - ### Accessing structure members {.row-span-2} ```c{11,12,16} @@ -1169,8 +1064,6 @@ s2.myNum = 20; s2.myLetter = 'C'; ``` - - ### Copy structure ```c{6} @@ -1184,8 +1077,6 @@ s2 = s1; In the example, the value of `s1` is copied to `s2` - - ### Modify value ```c{6,7} @@ -1202,49 +1093,42 @@ printf("%d %c %s", s1.myLetter); ``` -file processing ---- - - +## file processing ### File processing function -function | description Description -----| ---- -`fopen()` | `open` a new or existing file -`fprintf()` | write data to `file` -`fscanf()` | `read` data from a file -`fputc()` | write a character to `file` -`fgetc()` | `read` a character from a file -`fclose()` | `close` the file -`fseek()` | set the file pointer to `the given position` -`fputw()` | Write an integer `to` a file -`fgetw()` | `read` an integer from a file -`ftell()` | returns the current `position` -`rewind()` | set the file pointer to the beginning of the file +| function | description Description | +| ----------- | ------------------------------------------------- | +| `fopen()` | `open` a new or existing file | +| `fprintf()` | write data to `file` | +| `fscanf()` | `read` data from a file | +| `fputc()` | write a character to `file` | +| `fgetc()` | `read` a character from a file | +| `fclose()` | `close` the file | +| `fseek()` | set the file pointer to `the given position` | +| `fputw()` | Write an integer `to` a file | +| `fgetw()` | `read` an integer from a file | +| `ftell()` | returns the current `position` | +| `rewind()` | set the file pointer to the beginning of the file | There are many functions in the C library to `open`/`read`/`write`/`search` and `close` files - - ### Open mode parameter -Mode Mode | Description Description -----| ---- -`r` | Open a text file in `read` mode, allowing the file to be read -`w` | Open a text file in `write` mode, allowing writing to the file -`a` | Open a text file in `append` mode
If the file does not exist, a new one will be created -`r+` | Open a text file in `read-write` mode, allowing reading and writing of the file -`w+` | Open a text file in `read-write` mode, allowing reading and writing of the file -`a+` | Open a text file in `read-write` mode, allowing reading and writing of the file -`rb` | Open a binary file in `read` mode -`wb` | Open binary file in `write` mode -`ab` | Open a binary file in `append` mode -`rb+` | open binary file in `read-write` mode -`wb+` | Open binary file in `read-write` mode -`ab+` | open binary file in `read-write` mode - - +| Mode Mode | Description Description | +| --------- | -------------------------------------------------------------------------------------------- | +| `r` | Open a text file in `read` mode, allowing the file to be read | +| `w` | Open a text file in `write` mode, allowing writing to the file | +| `a` | Open a text file in `append` mode
If the file does not exist, a new one will be created | +| `r+` | Open a text file in `read-write` mode, allowing reading and writing of the file | +| `w+` | Open a text file in `read-write` mode, allowing reading and writing of the file | +| `a+` | Open a text file in `read-write` mode, allowing reading and writing of the file | +| `rb` | Open a binary file in `read` mode | +| `wb` | Open binary file in `write` mode | +| `ab` | Open a binary file in `append` mode | +| `rb+` | open binary file in `read-write` mode | +| `wb+` | Open binary file in `read-write` mode | +| `ab+` | open binary file in `read-write` mode | ### Open the file: fopen() @@ -1269,8 +1153,6 @@ void main() { After performing all operations on the file, the file must be closed with `fclose()` - - ### Write to file: fprintf() ```c{7} @@ -1286,8 +1168,6 @@ void main() { } ``` - - ### Read the file: fscanf() ```c{6} @@ -1306,8 +1186,6 @@ void main() { } ``` - - ### Write to file: fputc() ```c{6} @@ -1321,8 +1199,6 @@ void main() { } ``` - - ### Read the file: fgetc() ```c{8} @@ -1346,8 +1222,6 @@ void main() { } ``` - - ### Write to file: fputs() ```c {8} @@ -1367,8 +1241,6 @@ void main() { } ``` - - ### Read files: fgets() ```c {10} @@ -1389,8 +1261,6 @@ void main() { } ``` - - ### fseek() ```c{8} @@ -1412,8 +1282,6 @@ void main(void) { set the file pointer to the given position - - ### rewind() ```c{11} @@ -1445,8 +1313,6 @@ void main() { // Hello World! Hello World! ``` - - ### ftell() ```c{11}