##
Introduction to C
###
for those who already know Java
### What is C? - Developed by Dennis Ritchie at Bell Labs. - First public released in 1927. - The book: **Ritchie, Dennis M., and Brian W. Kernighan. The C programming language. Englewood Cliffs: Prentice Hall, 1988.**
### How to learn C (now that you already know Java)? - [C for Java Programmers](https://www.cs.rochester.edu/u/ferguson/csc/c/c-for-java-programmers.pdf) - [C Programming vs. Java Programming](https://introcs.cs.princeton.edu/java/faq/c2java.html)
### Before you start ... - C is much less supportive for programmers than Java. - Easier to make mistake, and harder to fix.
### But ... - C requires less memory resources than Java. - C, in many cases, runs faster than Java. - Knowing C will make you a better Java programmer.
### Similarity (or mostly similar) - Values, types, literals, expressions - Variables - Control flow (if, switch, while, for, do-while) - Call-return: parameters, arguments, return values - Arrays (mostly) - Primitive and reference types - Typecasts - Library
### Differences - No classes or objects - Arrays are simpler: - No boundary checking - No knowledge of arrays' own size - Strings are very limited - No collections, exceptions, or generics - No memory management - Pointer!!!
### How Java works ![java](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/java.png)
### How C works ![c](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/c.png)
### Hands-on - Start virtualbox - Start the *csc-331* VM
### Setup directory - Confirm that you are in your `home` directory by typing `pwd` and confirm that you are in `/home/student`. - Create a new directory named `intro-c`, and then change into that directory. ``` $ mkdir intro-c $ cd intro-c ```
![c](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-0.PNG)
### Editor: nano - If you know `vim` or `emacs`, feel free to use them. - Use `nano` for text editing on Linux (assuming you are still in `intro-c` directory from the previous slide: ``` $ nano hello.c ```
![nano](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-1.PNG)
![hello](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-2.PNG)
### Save and Quit from nano - To save and quit, first press `Ctrl-C` to ask to quit - You will be asked whether to save recent changes (**Y**) or not (**N**) - If you enter **Y**, you will next be asked for the file name to write, with the default choice is the current filename. If you don't plan to save your changes to a new file, go a head and press `Enter`.
### What's in the code?
### Compile and Run: Simple Compilation - Make sure that you are still inside `/home/student/intro-c` using `pwd`. ``` $ pwd $ ls $ gcc hello.c $ ls $ ./a.out ```
![hello-simple](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-3.PNG)
### Compile and Run: Specify output files ``` $ ls $ gcc -o hello hello.c $ ls $ ./hello ```
![hello-specific](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-4.PNG)
### Compile and Run: I want to see everything ``` $ ls $ rm hello $ ls $ gcc -save-temps -o hello hello.c $ ls $ ./hello ```
![hello-all](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/hello-5.PNG)
### Compile and Run: What are those? - `hello.i`: generated by pre-processor - `hello.s`: generated by compiler - `hello.o`: generated by assembler - `hello`: executable, generated by linker
![c](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/c-hello.png)
### What are in those files? - `hello.i`: ``` $ more hello.i ``` - You can use the `Space` button to move forward, and `q` to quit.
### What are in those files? - `hello.s`: ``` $ more hello.s ```
### What are in those files? - `hello.o`: we cannot use `more` since this is not a text file. ``` $ xxd -b hello.o | more ```
### What are in those files? - `hello`: we cannot use `more` since this is not a text file. ``` $ xxd -b hello | more ```
###
Variables, Addresses, and Pointers
- In Java, you cannot do anything to a variable other than get or set its value. - In C, you can retrieve the address of the location in memory where the variable is stored. - The operator **&** (reference of) represents the memory address of a variable.
![pointer-0](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/pointer-0.PNG)
### Pointer definition - Pointer is a variable that points to a memory location. - A pointer is denoted by a * character. - The type of pointer must be the same as that of the value being stored in the memory location (that the pointer points to).
![pointer-1](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/pointer-1.PNG)
![pointer-2](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/pointer-2.PNG)
###
Pass by Value and Pass by References
- Parameters are passed to functions. - Parameters can be value variables or pointer variables. - What is the difference?
![pointer-3](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/pointer-3.PNG)
![pointer-4](https://www.cs.wcupa.edu/lngo/assets/images/csc-331/intro_c/pointer-4.PNG)
In Java, do you pass by value or pass by reference?