# Data ## Variable Can define by yourself by any , but can't have any other symbol except `_` . Also , there each name can only be used one time . ```clike int hahaha -> valid int hah_aha ->valid int hah!hah -> invalid int @sdfas ->invalid ``` ## Type ### Interger * Interger number (整數) : 0 , 1 , 2 , 3 * Memory Cost : 4 bytes * Declare ```clike int a = 0 ; ``` ### Character * Character (字母) : a , b , c , d * Memory Coset : 1 bytes * To assign a character use `''` to include the character * Declare ```clike char a = 'a' ; ``` ### Float * Float number (小數) * Memory cost : 4 * Declare ```clike float a = 3.14159 ; ``` ### Double * Bigger float number * Memory cost : 8 * Declare ```clike double a = 3.14159 ; ``` ## Operation The data has four basic operation in C . * **addition** Add two value together , it is valid for `int` , `char` , `double` and `float` . Example : ```clike int x = 1 ; int y = 2 ; int z = x + y ; ``` ```clike x = 1 y = 2 z = 3 ``` The shorter format for `a = a + 1` is `a++` , so as `a --` . How the `char` store the character ? * **substraction** Do the substract between two given value or variables , it is valid for `int` , `char` , `double` and `float` . Example : ```clike int x = 1 ; int y = 2 ; int z = y - x ; ``` ```clike x = 1 y = 2 z = 1 ``` * **mutiplication** Mutiply two given value or variables , and is valid for types ,too . Example : ```clike int x = 2 ; int y = 2 ; int z = x * y ; ``` ```clike x = 2 y = 2 z = 4 ``` * **division** There are two operator for division . If there are two nubers a and b and `a = nb + r` , `r < b` 1 . `/` The operation `a/b` takes the integer part of simple division which is the quotient(商)(n) Example : ```clike= int x = 5 ; int y = 2 ; int z = x / y ; ``` ```clike x = 5 y = 2 z = 2 // 5 = 2*2 + 1 // 7 = 3 *2 +1 ``` 2 . `%` The operations `a%b` takes the remainder(餘數)( r ) . Example : ```clike= int x = 5 ; int y = 2 ; int z = x % y ; ``` ```clike x = 5 y = 2 z = 1 // 5 = 2*2 + 1 ``` ### Division of float and double The only valid division in `float` and `double` is `/` . `%` is an invalid operation . Example : ```clike float a = 2.5 ; float b = 2 ; float c = a/b ; ``` ```clike a = 2.5 b = 2 c = 1.250000 ``` ## ASCII Code Behind the `char` , it actually stores a number which is the index of a symbol , so the computation between `char` is avaliable . [ASCII CODE](https://zh.wikipedia.org/wiki/ASCII) Example ```clike= #include <stdio.h> int main(int argc, char const *argv[]) { char a = 65 ; printf("%c\n",a); a = a + 1 ; printf("%c\n",a); return 0; } ``` Compile : ```clike $ gcc ascii.c -o ascii $ ./ascii ``` Output : ```clike A B ``` ## Storage ### Bit / Bytes Each bits in a storage has two state , `1` or `0` , so we can use this advantage to store the data . * Decimal : the most common way to represent a data ```clike 123 = 123 11100 = 11100 ``` ```clike 123 = (1 * 100) + (2 * 10) + (3 * 1) 11100 = (1 * 10000) + (1 * 1000) + (1 * 100) + (0 * 10) + (0 * 1) ``` * Binary : the way to store data in computer ```clike 1111010 = 123(decimal) 11100 = 28(decimal) ``` ```clike= 1111010 = (1 * 64) + (1 * 32) + (1 * 16) + (1 * 8) + (0 * 4) + (1 * 2) + (0 * 1) 11100 = (1 * 16) + (1 * 8) + (1 * 4) + (0 * 2) + (0 * 1) ``` ```clike= 二進位制 十進位制 1 1 10 2 = 2 ^1^ 100 4 = 2 ^2^ 110 6 111 7 1000 8 = 2 ^3^ 1001 9 1010 10 ``` Byte equals to 8 bits ![need to remake](https://i.imgur.com/CQXxe4K.png) ### Range Because each type has it limited storage , so we get the upper and lower limit for each type . * Interger - 32 bits ```clike -2,147,483,648 ~ 2,147,483,647 ``` * Character - 8 bits ```clike -128 ~ 127 ``` * Float - 32 bits ```clike 1.2E-38 ~ 3.4E+38 ``` * Double - 64 bits ```clike 2.3E-308 ~ 1.7E+308 ``` ## Arrary A continuous memory with specificed size , can be called by index . * **Type** can be assign as any type , ex : char , int , float * **Declare / Call** ```clike int a[3] = {0,0,0} ; a[0] = 0 ; ``` Note that in declaration , `a[n]` is not valid , n has to be a constant value . * **Order of index** count by 0 ~ n-1 , ex: ```clike int a[3] ; a[0] = 1 ; a[1] = 1 ; a[2] = 1 ; ``` * **Code Example** ```clike= #include <stdio.h> int main() { int a[3] = {1,2,3} ; printf("a[0] is %d\n",a[0] ); printf("a[1] is %d\n",a[1] ); printf("a[2] is %d\n",a[2] ); return 0; } ``` Execution ```clike $ gcc example.c -o example $ ./example ``` Result ```clike a[0] is 1 a[1] is 2 a[2] is 3 ``` ### String Actually it is an array of a `char` type . Example : `program` ```graphviz Graph{ p r o g " r" a m "\\0" } ``` ```clike char * a = "program" ; ``` ```clike char a[8] = "program" ; ``` ```clike char a[8] ; a[0] = 'p' ; a[1] = 'r' ; a[2] = 'o' ; a[3] = 'g' ; a[4] = 'r' ; a[5] = 'a' ; a[6] = 'm' ; a[7] = '\0' ; ``` The three way above can reach the goal to generate a string .