Lab 2 The Unix environment Using vi C

Lab 2: The Unix environment, Using vi, C programming SITE, u. Ottawa 1

Practical tools n Editor n n Compiler n n Vi (emac) gcc Makefile make Debugging n gdb 2

Using C/Unix/Linux at SITE $ ssh linux 3

Vi Editor n n A screen-based editor used by Unix users An experience with vi n n n n n Starting the editor Cutting and Pasting/Deleting text Inserting New Text Moving the Cursor Within the File Moving the Cursor Around the Screen Replacing Text Searching for Text or Characters Saving and Quitting http: //www. eng. hawaii. edu/Tutor/vi. html#intro http: //www. brezeale. com/technical_notes/vim_notes. shtml 4

Vi Basic Commands n n n n n n mkdir <dirname> - create a directory cp <filename> <target> - copy a file rm <filename> - delete a file vi <filename> - open a file pressing the <Esc> key turns off the Insert mode : q – quit with saving : x – quit without saving : i – insert text before cursor, until <Esc> hit : a – append text before cursor, until <Esc> hit $ - move cursor to the end of the current line : w – move cursor to the beginning of next word 0 – move cursor to the start of current line : o – open and put text in a new line after the cursor y 1 – copy current character Yn – copy n characters yy – copy current line p - paste characters x – delete single character under cursor dd – delete the current line u – undo the last action : w – save a file /<string> - search the string forward ? <string> - search the string backward 5

Avi Exercise n Create and edit test. c file #include <stdio. h> main() { printf(“Hello world, …n”); } 6

C programming/Unix #include <stdio. h> main() { printf(“hello, wolrd…n”); } $ mkdir csi 3130 $cd csi 3130 $ vi test. c $cc test. c $a. out 7

Compiler n GNU Compiler Collection n A compiler system produced by the GNU Project supporting various programming languages includes front ends for C (gcc), C++ (g++), Java (gcj), and Fortran (g. Fortran) http: //pages. cs. wisc. edu/~beechung/ref/gccintro. html 8

GCC n Compile C code to an executable n n gcc -o <executable-name> <. c files> Reporting all warning messages n gcc -Wall -o <executable-name> <. c files> 9

GCC (Cont’d) n An example of compiling two. c files into an executable program n n gcc -Wall -o assign 1 prob 1 main. c list. c An example of compiling two files and then link them separately n n n gcc -Wall -o main. o -c main. c gcc -Wall -o list. o -c list. c gcc -Wall -o assign 1 prob 1 main. o list. o 10

Make command n make automates the process of compiling a program n Makefile n n $ make clean 11

Debugging n gdb is GNU Debugger n n A debugger for several languages Inspecting what the program is doing at a certain point during execution Errors like segmentation faults may be easier to find with the help of gdb http: //ftp. gnu. org/old-gnu/Manuals/gdb 5. 1. 1/html_node/gdb_toc. html 12
![GDB n enable built-in debugging support n n n gcc [other flags] -g <source GDB n enable built-in debugging support n n n gcc [other flags] -g <source](http://slidetodoc.com/presentation_image_h2/5f025a2da6f1e67501a8d97bda84bb79/image-13.jpg)
GDB n enable built-in debugging support n n n gcc [other flags] -g <source files> -o <output file> generating an expanded symbol table for use with gdb Example: gcc -Wall -Werror -ansi - pedantic-errors -g prog 1. c -o prog 1. x 13

GDB (Cont’d) n enter gdb n printing line from a source file n running a program n n n break <c file name>: <line number> break <c file name>: < functionname> set a breakpoint with condition n n run break at a particular function n n list breakpoint at a line number n n quit break <function or file name> if <condition> deleting breakpoints n delete n proceed onto the next breakpoint n Single step n n continue step 14

debugging exercise n Reverse a string #include <string. h> #include <stdio. h> char * reverse(char s[]) { int c, i, j; For(i =0, j=strlen(s)-1; i< j; i++, j--) { c = s[i]; S[i] =s[j]; S[j]=c; } } Main() { printf(“%s n”, reverse(“database”)); } 15

GDB debugging exercise Program failed, why? ? ? Load the program into GDB 16

GDB debugging exercise Here failed? What is the value of i 17

GDB debugging exercise Track your program step by step, inspect the variables. . Try the basic GDB commands here! 18
![Correct code #include <string. h> #include <stdio. h> char * reverse(char s[]) { int Correct code #include <string. h> #include <stdio. h> char * reverse(char s[]) { int](http://slidetodoc.com/presentation_image_h2/5f025a2da6f1e67501a8d97bda84bb79/image-19.jpg)
Correct code #include <string. h> #include <stdio. h> char * reverse(char s[]) { int c, i, j; for(i =0, j=strlen(s)-1; i< j; i++, j--) { c = s[i]; s[i] =s[j]; s[j]=c; } return s; } int main() { char my. Str[9]="database"; printf("%s n", reverse(my. Str)); return 0; } 19

From Java to C 20

Types, Operators and Expressions n n n Data type char, int, float, double, short int, long int Constants #define PI 3. 1415926 Typedef: assigning alternative names to existing typedef int my. Integer; Relational and logical operators > >= < <= == != Increment and decrement operators b = a++; b = ++a; b = a--; b = --a; 21

Control Flow n n n The semicolon is a statement terminator If-Else, Else-If if (expression) statement 1 else statement 2 if (expression) statement 1 else if (expression) statement 2 else statement 3 For loop you need to define the counter variable before the loop for(expr 1; expr 2; expr 3) statement While-Do expr 1; While(expr 2) statement expr 3; Do-While do statement While(expression); 22

Control Flow (Cont’d) n Switch switch (expression){ case const-expr : statements default: statements } n Conditional expressions (expr 1 ? expr 2 : expr 3) if(a>b) z=a; z = (a > b) ? a : b ; else z=b; 23

Function declaration n Return value n Default return value of a function in C is int Java n The use of function may appear earlier than its definition C program n Should be declared somewhere earlier than their invocations int power(int b, int e) { int r = 1; while (e-- > 0) r *= b; return r; } main() {. . . /* call power() here */ } int power(int b, int e); … /* call power() here */ … int power(int b, int e) { int r = 1; while (e-- > 0) r *= b; return r; } 24

Input and Output n n printf(“the int value is %d”, var); string value is %s”, var); charactr and double values are %c and %e”, var); double value is %e”, var); n scanf(“enter n Do not forget including <stdio. h> n n n an int: %d”, var); a string: %s”, var); a character and a double: %c %e”, var); a string: %s”, var); 25

Variable scope int global. Var = 34; my. Fun() { int x =1; } Global; outside of the function Local 26

Some details about C n No boolean type in C No explicit boolean type n #define TRUE 1 #define FALSE if (a){ … } n No function overloading in C Two functions cannot have the same n n Variables must be declared at the top of a basic block (for some c systems) The following may not pass the compiler n { } n Lack of exceptions n n int a; printf("Hello worldn"); char b; Illegal activity may not be told (e. g. access memory that hasn’t been allocated in some way) No automated garbage collection in C n you must free the memory 27

Standard library n <stdlib. h> n n <stdio. h> n n Mathematical functions <ctype. h> n n String handling <math. h> n n Input/output <string. h> n n dynamic memory allocation Characters http: //www. utas. edu. au/infosys/info/documentation/C/CStd. Lib. html 28

Some common errors n n if (a=1){ some stuff } == Boolean evaluation = Variable assignment operator void myfunc(int a) { /*. . . */ } void myfunc(float b) { /*. . . */ } error: myfunc already defined 29

Exercise n Write a program that n n n Accepts an input “x” (A value to progress up to) Starting at 0 and 1, outputs the Fibonacci sequence up to “x” permutations. There should be two functions, for clarity. n n (main) Accepts the input and calls the fibonacci function. (fibonacci) One that accepts the value of x and outputs the sequence. 30
![Fibonacci in Java import java. io class Fibonacci { public static void main(String args[]) Fibonacci in Java import java. io class Fibonacci { public static void main(String args[])](http://slidetodoc.com/presentation_image_h2/5f025a2da6f1e67501a8d97bda84bb79/image-31.jpg)
Fibonacci in Java import java. io class Fibonacci { public static void main(String args[]) { System. out. println("How many numbers of the sequence would you like? "); Input. Stream. Reader sr = new Input. Stream. Reader(System. in); Buffered. Reader br = new Buffered. Reader(sr); try { String input = br. read. Line(); int n = Integer. value. Of(input). int. Value(); fibonacci(n); } catch (Number. Format. Exception e){System. out. println("That is not an integer. Please enter an integer value"); } catch (IOException e) { System. out. println("I did not recieve an input"); } } public static void fibonacci(int n){ int a=0, b=1; for (int i=0; i<n; i++){ System. out. println(a); a=a+b; b=a-b; } } } 31

Fibonacci in C #include <stdio. h> int main () { int n; printf("n. How many numbers of the sequence would you like? n"); scanf("%d", &n); fibonacci(n); return 0; } int fibonacci(int n) { int a = 0; int b = 1; int sum; int i; for (i=0; i<n; i++) { printf("%dn", a); sum = a + b; a = b; b = sum; } return 0; } 32
- Slides: 32