#### 2. In hello.c 1. printf("Hello, World"): ```C printf("hello, %s",answer); ``` * printf(): A function that prints the word between the round brackets. * "Hello, World": An argument with double quotes to input a specific sentence into the function. * Arguments "hello, %s",answer : In the double quotes, it is allowed to write infinite conversion specifications such us %s, %d and so on, and write inputs behind the first text with ",", which two argument. Consquently, the conversion specifications will be replaced by the inputs in order. |Conversion Specifications|%d |%s |%f |%c |%u|%o|%x|puting h before d,o,u,x|puting l before d,o,u,x|puting ll before d,o,u,x(C99 only)| |:--: |:--:|:--: |:--: |:--:|:--:|:--:|:--:|:--:|:--:|:--:| |**types** |int|string|float|charactor|unsigned int(decimal)|unsigned int(octal)|unsigned int(hexadecimal)|short int|long int|long long int| [More Information about Formatted I/O](https://hackmd.io/k2YMfBPsTW-r56pxNiztLA) * ※scanf(string,The address of the varibles,...): scanf() is a function that can receive what is typed in and store the value in specific address. ```c #include <stdio.h> int main(void) { int number1, number2; printf("Number1 and Number2:"); //Here we specify the inputs to be divided by a space. scanf("%d %d", &number1, &number2); printf("Number1 and Number2: %d %d\n", number1, number2); printf("Number1 and Number2:"); //Here we specify the inputs to be divided by a "-" scanf("%d-%d", &number1, &number2); printf("Number1 and Number2:%d-%d\n", number1, number2); return 0; } ``` ![](https://i.imgur.com/GDscJjs.png) 5. return 0: We can type echo $? in command box to see the return value. And we usually return 0 to represent the program runs correctly, or return 1 to represent the program runs incorrectly. #### 3. In Command Box 1. Hint Charactor: A charactor which is no need to typed and represent the initial of the command. ![](https://i.imgur.com/W6qzhIR.png) 2. How to run a written program? Just type ![](https://i.imgur.com/tZQsrna.png) * ./⇨File directory and . means the current directory. * hello⇨The file name 3. ~/ : ~ means the home directory.(./: . means the current directory) 4. ls : To see the file in the current directory. 5. rm file : To remove the file. 6. mv file1 file2 : To change the name of the file1 to file2 or move file1 to the directory named file2. * ※mv file .. : To move file to the folder above. 7. mkdir file/ : To create a folder. 8. cd file/ : To change the directory named file. * ※cd .. : To move the directory to its parent. 9. rmdir file/ : To remove the folder. 10. How to compile a file write gcc -o hello hello.c in the command box. 1. gcc: A compiler for C(g++ is for C++.) 2. -o: A parameter that can determine the name of the output file 3. hello: The name of the output file(machine code) 4. hello.c: The file that we want to compile ### Expressions 1. The Definition of Expression: Expressions are built from variables, constants, and operators. 2. Operators: 1. Properties of Operators: 1. Unary, Binary, and Ternary: Unary, Binary, Ternary are respectively represent there are one, two, and three operands. In compiler, if there are no Parentheses, unary operators will be executed first, then binary operators will be executed and trinary operators are the last. 2. Right Associative and Left Associative: An operator is left associative if it groups from left to right, and an operator is right associative if it groups from right to left. ``` // Left Associative i-j-k is equivalent to (i-j)-k // Right Associative -+i is equivalent to -(+i) ``` 3. The behavior of % and / with negative sign: 1. Implementation-defined in C89 2. i%j has the same sign of i in C99 2. Types of Operators: 1. Arithmetic Operators: * +, -, *, / -- Binary, Left Associative * Remainder: % -- Binary, Left Associative * Unary Plus(Positive): + -- Unary, Right Associative * Unary Minus(Negative): - -- Unary, Right Associative 2. Relational Operators 3. Logical Operators 4. Assignment Operators: 1. Simple Assignment: = The effect fo the assignment v=e is to evaluate the expression e and copy the value to v. 1. If v and e don't have the same type, the value of e os converted to the type of v. 2. = is right associative. 3. = needs a L value * ※L Value and R Value: L value have a confirmed, specific address in the momery which a can thus be change. On the contrast, R Value don't have specific address such as a statement (1+1), so it can only be put on the right side of "=". * In many programming languages, assignment is a statement; in C, assignment is an operator. 2. Compound Assignment: 1. v+=e, v-=e, v*=e, v/=e, v%=e These operators means to do addition, subtraction, multiplication, division and remainder and assign the value to v. 6. Increment and Decrement Operators: ++ and -- 1. Prefix Operators: ++i and --i will add 1 to i or subtract i by 1 early. 2. Postifix Operators: i++ and i-- will add 1 to i or subtract i by 1 last. 7. Conditional Operators: ```c expression1:expression2?expression3 ``` Expression1 will be evaluated first. 1. If the value is not zero, then expression2 will next be evaluated, and the outcomeing value is the value of the overall expression. 2. If the value is zero, the value of expression3 is the value of the overall expression. ### Data Types |long int|unsigned long|int|unsigned int|short int|unsigned short int|float|double|char|bool| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |4 bytes|4 bytes|4 bytes|4 bytes|2 bytes|2 bytes|4 bytes|8 bytes|1 bytes|1 bytes| * Ranges 1. Signed and Unsigned: In signed types, the leftmost bit is 0 if the number is positive or zeor, 1 if it's negative. 2. int in different bit computer ![](https://i.imgur.com/ZR0eLOJ.png) ![](https://i.imgur.com/2SfqukI.png) ![](https://i.imgur.com/B9nvLSM.png) * Integers, Floating Types, Characters, and Boolean Types * Integers: * C allows integer constants to be written in decimal(base 10), octal(base 8), or hexadecimal(base 16). 1. Octal constants contain only 0 to 7, and must begin with 0. 2. Hexadecimal constants contain 0 to 9 and a to f, and always begin with 0x * To force the compiler to treat a constant as float long int or unsigned types, just follow it with f(F), l(L) or u(U), and l and u may be used in combination like lu(LU) or ul(UL). Also in C99, a constant followed with LL or ll may be a long long int. * Integer Overflow: When the arithmetic operations are performed on integers, its possible that the result will be too large to represent. The behavior when integer overflow occurs depend on whether the operands are signed or unsigned. 1. Signed Integers: The behavior is undefined. 2. Unsigned Integers: The result is $2^n$, where n is the number of bits used to store. * Floating Types: C provides three floating types: float, double long double 1. Precision: IEEE floating point standard provides single precision(32 bits) and double precision(64 bits). In single precision format, the exponent is 8 bits long while the fraction occupies 23 bits long. So the maximum value is approximately $3.4*10^{38}$, with a precision of about 6 digits. 2. The default floating type is double. * Character Types: 1. Character constants are enclosed in single quotes, not double quotes. 2. Character types actually have int types rather than char types, and values of characters often depend on ASCII. Therefore, char can do addition, substaction and comparison, ( but these might make the program unportable). 3. Escape Sequences: ![](https://i.imgur.com/FukO5eL.png) * Boolean Types: It just contain 0 and 1 which also mean false and true. It a boolean variable is assigned as a non-zero constant, then the resultant value will be 1. * ※<stdbool.h> supplies a macro, bool, so that it can be used to define a variable. It also supplies macros called true and false which stand for 1 and 0 respectively. * Type Conversion 1. For an arithmetic operation, the operands should have the same sizes. So, when operands with different types are mixed in expressions, the compiler should arrange for them to be the same types. * char or short int ⇨ int(unsigned int in some case) * int ⇨ unsigned int ⇨ long int ⇨ unsigned long int * Integer ⇨ Floating types * float ⇨ double ⇨ long double 2. Casting: To change the variable manually, just write ``` (type)variable; ``` * Type Definition: 1. Write ```c #define defined_type_name type ``` before int main() to define a macro 2. Write ```c typedef type defined_type_name ``` * Size of Types: The operator sizeof(type) will get the size of the type. In the parentheses, there can be constants, variables, and expressions, and when applied expressions, the parentheses can be omitted. ### Selection Statement * ※Statement Catagories 1. Expression Statements 2. Selection Statements: if and switch 3. Iteration Statements: while do and for 4. Jump Statements:break, continue, goto, and return 5. Compound Statements: {statement} 6. Null Statements 1. Logical Expression: 1. Rational Operators:<, >, <=, and >= 1. These operators yield a boolean type integer, 1(true) or 0(false). 2. The procedure of these operators is lower than arithmetic operators. ``` i+j<k-1 is equivalent to (i+j)<(k-1) ``` 3. These operators are left associative ``` i<j<k is equivalent to (i<j)<k ``` 2. Equality Operators: ==(equal to) and !=(not equal to) 1. These operators also yield an integer, 1 or 0. 2. The procedure of these operators is lower than rational operators. 3. These operators are left associative 3. Logical Operators: !(logical negative), &&(logical and), ||(logical or) 1. ! is unary, while && and || are binary. 2. These operators also 1 or 0. 3. These operators treat any nonzero operands as 1 and zero operands as 0. 4. ! is right associative; && and || are left associative. 4. 2. Statements 1. The If Statement: ```c if (expression) statement else if (expression) statement else statement ``` 2. The Switch Statement: ```c switch(expression) case constant-expression: statements ... case constant-expression: statements default: statements ``` 1. If there are several cases that pointing to a statement, it can be written like ```c switch(expression) case constant-expression: case constant-expression: ... case constant-expression: statements ``` 2. There are normally a break statement at the end of each statements to break out of the switch statement, otherwise, control will flow into the next case. ### Loops(Iteration Statements) 1. Controlling Expression: In C every loop has a controlling expression. Every time the loof body is evaluated, the expression is evaluated If the expression is true, the loop continue to execute. 2. Iteration Statements: 1. While: ```c while(expression) statement ``` The expression is evaluated first, if it's true, then the loop body is executed, after then, the expression will be evaluated again. The process continues until the expression is false. * ※while(true) may execute forever unless the body contains a statement such as break, continue, or goto or calls a function that causes the program to terminate. 2. do-while: ```c do statement while(expression) ``` The loop body is executed first, then the expression is evaluated. If the expression is nonzero, the loop body is executed again. 3. for: ```c for(expr1;expr2;expr3) statement ``` 1. The procedure: 1. expr1 is an initialization step that's performed only ones, before the loop begins to execute. 2. expr2 controls loop termination. 3. expr3 is performed at the end of each loop iteration. 2. Properties: 1. Expressions are optional, and if expr2 is omitted, it defaults to be true. 2. In C99 expr1 can be replaced by a declaration, this make the variable available only in the loop. 3. expr1 can declare more than one variables. 4. First and third expression can be replaced by a comma expression like ``` expr1,expr2 ``` . expr1 should have side effect, otherwise, it serves no purpose. 3. break, continue, and goto 1. break: break can control the loop to terminate, and it only transfers control out of the innermost enclosing while, do, for, or switch. 2. continue: continue transfers control to a point just before the end of the loop body. 3. goto: goto is capable of jumping to any statement in a function, provided that the statement has a label, where the label is just a identifier placed at the beginning of a statement. ```c //label idenetifier:statement //goto goto identifier ``` * ※goto is rarely used in C. ### Function 1. Declaration: ```c type function_name(type1, type2...) ``` 1. A function must be declaration before it be called(defining of a function can also be seen as a declartion). So, the following program is valid ```c type function_name(type1, type2...) int main() { function_name(v1, v2...); } type function_name(type1 v_name1, type2 v_name2...) { statement } ``` 3. The declared function is called the prototype of the function. 2. Defining of a Function: ```c type function_name(type1 v_name1, type2 v_name2...) { statement } ``` 1. type: It stands for the return type of the function. If there is no return value, you can write void as a type. 2. statement: In the body, there must be a ruturn statement to terminate the function, it the function type it void, it can be written like ```c return; ``` 3. arguments: In C arguments are passed by value. When a function is called, each argument is evaluated and its value assigned to the corresponding parameter. Since the parameter contains a copy of the argument's value, any changes made to the parameter during the execution of the function don't affect the argument. * ※Array Argument * When a function parameter is a one-dimension array, the length of the array can be left unspecified. ```c type function_name(int name[]) ``` But if it needs the length of the one-dimension array, you should input an extra argument which represents the length of the array. ```c type function_name(int name[], int n) ``` * If a function is a multidimentional array, only the length of the first dimension may be omitted. 3. A function should be defined before being called, in this way, we don't need to write the arguments' names and the statements. ```c type function_name(argument1,argument2...) ```