1 / 32

Lab 2: The Unix environment, Using vi, C programming SITE, uOttawa

Lab 2: The Unix environment, Using vi, C programming SITE, uOttawa. Practical tools. Editor Vi (emac) Compiler gcc Makefile make Debugging gdb. Using C/Unix/Linux at SITE. $ ssh linux. Vi Editor. A screen-based editor used by Unix users An experience with vi Starting the editor

skyler
Télécharger la présentation

Lab 2: The Unix environment, Using vi, C programming SITE, uOttawa

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. Vi Editor • A screen-based editor used by Unix users • An experience with vi • 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

  5. Vi Basic Commands • 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 • y1 – 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

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

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

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

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

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

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

  12. Debugging • gdb is GNU Debugger • 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

  13. GDB • enable built-in debugging support • 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 prog1.c -o prog1.x

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

  15. debugging exercise • 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”)); }

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

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

  18. GDB debugging exercise Track your program step by step, inspect the variables.. Try the basic GDB commands here!

  19. 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 myStr[9]="database"; printf("%s \n",reverse(myStr)); return 0; }

  20. From Java to C

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

  22. Control Flow • The semicolon is a statement terminator • If-Else, Else-If if (expression) if (expression) statement1 statement1 else else if (expression) statement2 statement2 else statement3 • For loop you need to define the counter variable before the loop for(expr1; expr2; expr3) statement • While-Do expr1; While(expr2) statement expr3; • Do-While do statement While(expression);

  23. Control Flow (Cont’d) • Switch switch (expression){ case const-expr : statements case const-expr : statements default: statements } • Conditional expressions (expr1 ? expr2 : expr3) if(a>b) z=a; z = (a > b) ? a : b ; else z=b;

  24. Function declaration • Return value • Default return value of a function in C is int • Java • The use of function may appear earlier than its definition • C program • 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; }

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

  26. Variable scope int globalVar = 34; myFun() { int x =1; } Global; outside of the function Local

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

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

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

  30. Exercise • Write a program that • 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. • (main) Accepts the input and calls the fibonacci function. • (fibonacci) One that accepts the value of x and outputs the sequence.

  31. 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?"); InputStreamReader sr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(sr); try { String input = br.readLine(); int n = Integer.valueOf(input).intValue(); fibonacci(n); } catch (NumberFormatException 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; } } }

  32. Fibonacci in C #include <stdio.h> int main () { int n; printf("\nHow 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("%d\n",a); sum = a + b; a = b; b = sum; } return 0; }

More Related