Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs288 Intensive Programming in Linux

Similar presentations


Presentation on theme: "Cs288 Intensive Programming in Linux"— Presentation transcript:

1 Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: Class 6 6-9-15

2 Topics today Homework revu More C Time for questions

3 text cfy

4 Submit only .c files (or .h if I ask for header files too.)
Insure that your code compliles on the afs Linux using the default options (no –gcc) Write a hello world program – done in class last week Do problem 1-4 from text Do problem 1-8 from text

5 First program /* This is a comment */ #include <stdio.h> /* preproc d*/ main() { printf("hello world\n"); } 6-9-15

6 Problem 1-4 Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.

7

8 Problem 1-8: ite a program to count blanks, tabs, and newlines.
./public_html/x/exer1-8A.c

9

10 Some more Basics of C 6-9-15

11 Conditional Operator ? : exp1 ? exp2 : exp3
If exp1 is non-zero value of expression is exp2 otherwise it is exp ? 2 : 3 cfy

12

13

14 Operator Precedence left to right
* / % + - cfy

15 Some Basics /* Comments */ { blocks } Parameters ( ) # preprocessor directive Statements end with ; Loops: for while Conditionals if then 6-9-15

16 $ cat header. h #define ROOT 1 $ cat main1. c /. This is a multi-line
$ cat header.h #define ROOT 1 $ cat main1.c /* * This is a multi-line * comment block. */ #include <srdio.h> #include "header.h" /* This a one line comment */ main(){ int x=ROOT; /* this is an embeeded comment */ #ifdef ROOT printf("%d",x); #else printf("not root\n"); #endif }

17 $ cc main1.c $ ./a.out 1$

18 Basic printf formats %i or %d int %c char %f float %s string
cfy

19 Width and precision %d print as decimal integer
at least 6 characters wide %f print as floating point %6f print as floating point, at least 6 characters wide %.2f print as floating point, 2 characters after decimal point cfy

20 I/O C=getchar(); putchar(C); cfy

21 for statement for(init;condition;increment) { statement(s); } for(i=0;i<5;i++) printf(“%d\n”,i); cfy

22 Symbolic Contants #define name replacement text #define LIMIT 30
cfy

23 Escape sequences \a 07 Alarm (Beep, Bell) \b 08 Backspace
\f 0C Formfeed \n 0A Newline (Line Feed) \r 0D Carriage Return \t 09 Horizontal Tab \v 0B Vertical Tab \\ 5C Backslash \‘ 27 Single quotation mark \“ 22 Double quotation mark \? 3F Question mark \nnn any The character whose numerical value is given by nnn interpreted as an octal number \xhh any The character whose numerical value is given by hh interpreted as a hexadecimal number 6-9-15

24 if statement if ( expression) statement else if ( expression ) else if ( expression ) cfy

25 arrays int myarray[10]; cfy

26 functions int function ( int param1, char param2); … int function ( int param1, char param2) { int local1, local2; /* call by value */ retval=function2 (local1); return(10); } cfy

27 argv, argc #include <stdio.h> int main (int argc, char *argv[]) { printf(“%s\n”,argv[0]); return 0; } cfy

28 compiler cc file.c –o file cfy

29 More basic C More on arrays:
Array initialization during declaration: int num[5]; num[0]=1; int num[5]={1,2,3,4,5}; cfy

30 Multi-dimensional arrays
int a[2][3]; int a[2][3]={{1,2,3},{4,5,6}}; cfy

31 Multi-dimensional arrays cont
#include <stdio.h> main() { int a[2][3]={{1,2,3},{4,5,6}}; int i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) printf("a[%d][%d]=%d\n",i,j,a[i][j]); } cfy

32 Multi-dimensional arrays cont
afsconnect1-58 >: ./a.out a[0][0]=1 a[0][1]=2 a[0][2]=3 a[1][0]=4 a[1][1]=5 a[1][2]=6 cfy

33 Strings are just arrays of characters.
#include <stdio.h> main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; printf("name1=%s\n",name1); printf("name2=%s\n",name2); } >: ./a.out name1=NJIT name2=NJIT cfy

34 Strings (cont). main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; char *name3="NJIT"; printf("name1=%s\n",name1); printf("name2=%s\n",name2); printf("name3=%s\n",name3); } afsconnect1-67 public_html >: ./a.out name1=NJIT name2=NJIT name3=NJIT cfy

35 Strings (cont). Header file string.h Some examples: strcmp String Compare strcat String Concatenation memcpy Copy Memory Block strlen String Length cfy

36 General form: Strcat(string1,string2) Appends string2 to string1;
Strings (cont). General form: Strcat(string1,string2) Appends string2 to string1; cfy

37 Addresses! & used to get the address of a variable main() { int i=666;
printf("i= %d\n",i); printf("&i= %d\n",&i); } afsconnect1-72 public_html >: ./a.out i= 666 &i= cfy

38 More C keywords switch break, continue, goto sizeof Storage classes: auto register extern static cfy

39 switch( Grade ) { case 'A' : printf( "Excellent\n" ); case 'B' : printf( "Good\n" ); case 'C' : printf( "OK\n" ); case 'D' : printf( "Mmmmm....\n" ); case 'F' : printf( "You must do better than this\n" ); default : printf( "What is your grade anyway?\n" ); } cfy

40 label: while (true) { if(a) break; if(b) continue; if(c) goto label; }
cfy

41 printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n",
main() { int i=666; char array[15]; double n; printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n", sizeof(array)); printf("sizeof n= %d\n", sizeof(n)); } >: ./a.out sizeof i= 4 sizeof array= 15 sizeof n= 8 cfy

42 static Has number of implications:
Local static variables initialized only once and retain their value. Guaranteed to be automatically initialized to zero if not otherwise initialized. Global static variable inaccessible outside the file. Can be applied to functions. cfy

43 Structures! struct <name> { element1; element2; … } cfy

44 Structures example struct class { char name[30]; int number; char instructor[25]; int students[35]; }; cfy

45 Structures element dereferencing
structure-variable.stucture-member; e.g.: x=class.number; cfy

46 Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int grades[10]; }g[35]; cfy

47 Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; cfy

48 sizeof can be applied to unions and structures
Structures can be nested. cfy

49 Pointers General form: datatype *ptrname; int x=666, y=555; Int *p; p = &x; y = *p; cfy

50 Some cc command line options
Compiling multiple C files. Linking multiple objects.

51 $ cat main. c main(){ func(); } $ cat sub. c func(){} $ cc main
$ cat main.c main(){ func(); } $ cat sub.c func(){} $ cc main.c /tmp/ccRFdyU8.o: In function `main': main.c:(.text+0xa): undefined reference to `func' collect2: error: ld returned 1 exit status $ cc sub.c /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' $ cc main.c sub.c $ cc -c main.c $ cc -c sub.c $ cc main.o sub.o $ cc main.c sub.o $

52 $ cat main. c #include "tmp. h" main(){ func(); } $ cc -c main. c main
$ cat main.c #include "tmp.h" main(){ func(); } $ cc -c main.c main.c:1:17: fatal error: tmp.h: No such file or directory ^ compilation terminated. $ ls -l /tmp/tmp.h -rw-rw-r--. 1 integration integration 0 Mar 3 09:23 /tmp/tmp.h $ cc -c -I /tmp main.c $

53 Homework Do exercise 1-19 from the text.
Write a program which reads stdin and converts any local case characters to upper case. Redo problem 4 from class 2 as a C program. cfy

54 problem 4 Write a C program, sort.c, which sorts a list of command line parameters in ascending order. For example, your command will look something like:  $ sort and type enter. Your program will return: cfy


Download ppt "Cs288 Intensive Programming in Linux"

Similar presentations


Ads by Google