Cs 288 Intensive Programming in Linux Instructor C

  • Slides: 54
Download presentation
Cs 288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher. f. yurkoski@njit. edu

Cs 288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher. f. yurkoski@njit. edu Section web site: https: //web. njit. edu/~yurkowsk/cs 288. html Class 6 6 -9 -15 1

Topics today • Homework revu • More C • Time for questions

Topics today • Homework revu • More C • Time for questions

text 6 -9 -15 cfy 3

text 6 -9 -15 cfy 3

 • Submit only. c files (or. h if I ask for header files

• 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

First program /* This is a comment */ #include <stdio. h> /* preproc d*/

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

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

Problem 1 -4 Exercise 1 -4. Write a program to print the corresponding Celsius to Fahrenheit table. • https: //web. njit. edu/~yurkowsk/x/problem 1 -4. c

Problem 1 -8: ite a program to count blanks, tabs, and newlines. • .

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

Some more Basics of C 6 -9 -15 10

Some more Basics of C 6 -9 -15 10

Conditional Operator • ? : exp 1 ? exp 2 : exp 3 If

Conditional Operator • ? : exp 1 ? exp 2 : exp 3 If exp 1 is non-zero value of expression is exp 2 otherwise it is exp 3 1 ? 2 : 3 6 -9 -15 cfy 26

Operator Precedence left to right • * / % • + - 6 -9

Operator Precedence left to right • * / % • + - 6 -9 -15 cfy 29

Some Basics /* Comments */ { blocks } Parameters ( ) # preprocessor directive

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

$ cat header. h #define ROOT 1 $ cat main 1. c /* *

$ cat header. h #define ROOT 1 $ cat main 1. 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 rootn"); #endif }

$ cc main 1. c $. /a. out 1$

$ cc main 1. c $. /a. out 1$

Basic printf formats • %i or %d • %c • %f • %s 6

Basic printf formats • %i or %d • %c • %f • %s 6 -9 -15 cfy int char float string 33

Width and precision • %d • %6 d print as decimal integer, at least

Width and precision • %d • %6 d print as decimal integer, at least 6 characters wide • %f print as floating point • %6 f print as floating point, at least 6 characters wide • %. 2 f print as floating point, 2 characters after decimal point 6 -9 -15 cfy 34

I/O • C=getchar(); • putchar(C); 6 -9 -15 cfy 35

I/O • C=getchar(); • putchar(C); 6 -9 -15 cfy 35

for statement for(init; condition; increment) { statement(s); } for(i=0; i<5; i++) { printf(“%dn”, i);

for statement for(init; condition; increment) { statement(s); } for(i=0; i<5; i++) { printf(“%dn”, i); } 6 -9 -15 cfy 36

Symbolic Contants • #define name replacement text • #define LIMIT 30 6 -9 -15

Symbolic Contants • #define name replacement text • #define LIMIT 30 6 -9 -15 cfy 37

Escape sequences • a 07 Alarm (Beep, Bell) b 08 Backspace f 0 C

Escape sequences • a 07 Alarm (Beep, Bell) b 08 Backspace f 0 C Formfeed n 0 A Newline (Line Feed) r 0 D Carriage Return t 09 Horizontal Tab v 0 B Vertical Tab \ 5 C Backslash ‘ 27 Single quotation mark “ 22 Double quotation mark ? 3 F 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 38

if statement if ( expression) statement else statement if ( expression ) statement else

if statement if ( expression) statement else statement if ( expression ) statement else if ( expression ) statement 6 -9 -15 cfy 39

arrays int myarray[10]; 6 -9 -15 cfy 40

arrays int myarray[10]; 6 -9 -15 cfy 40

functions int function ( int param 1, char param 2); … int function (

functions int function ( int param 1, char param 2); … int function ( int param 1, char param 2) { int local 1, local 2; /* call by value */ retval=function 2 (local 1); return(10); } 6 -9 -15 cfy 41

argv, argc #include <stdio. h> int main (int argc, char *argv[]) { printf(“%sn”, argv[0]);

argv, argc #include <stdio. h> int main (int argc, char *argv[]) { printf(“%sn”, argv[0]); return 0; } 6 -9 -15 cfy 42

compiler • cc file. c –o file 6 -9 -15 cfy 43

compiler • cc file. c –o file 6 -9 -15 cfy 43

More basic C • More on arrays: Array initialization during declaration: int num[5]; num[0]=1;

More basic C • More on arrays: Array initialization during declaration: int num[5]; num[0]=1; int num[5]={1, 2, 3, 4, 5}; 6 -9 -15 cfy 44

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

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

Multi-dimensional arrays cont #include <stdio. h> main() { int a[2][3]={{1, 2, 3}, {4, 5,

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]=%dn", i, j, a[i][j]); } 6 -9 -15 cfy 46

Multi-dimensional arrays cont afsconnect 1 -58 >: . /a. out a[0][0]=1 a[0][1]=2 a[0][2]=3 a[1][0]=4

Multi-dimensional arrays cont afsconnect 1 -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 6 -9 -15 cfy 47

Strings are just arrays of characters. #include <stdio. h> main() { char name 1[25]="NJIT";

Strings are just arrays of characters. #include <stdio. h> main() { char name 1[25]="NJIT"; char name 2[25]={'N', 'J', 'I', 'T', ''}; printf("name 1=%sn", name 1); printf("name 2=%sn", name 2); } >: . /a. out name 1=NJIT name 2=NJIT 6 -9 -15 cfy 48

Strings (cont). main() { char name 1[25]="NJIT"; char name 2[25]={'N', 'J', 'I', 'T', '�'};

Strings (cont). main() { char name 1[25]="NJIT"; char name 2[25]={'N', 'J', 'I', 'T', ''}; char *name 3="NJIT"; printf("name 1=%sn", name 1); printf("name 2=%sn", name 2); printf("name 3=%sn", name 3); } afsconnect 1 -67 public_html >: . /a. out name 1=NJIT name 2=NJIT name 3=NJIT 6 -9 -15 cfy 49

Strings (cont). Header file string. h Some examples: strcmp String Compare strcat String Concatenation

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

Strings (cont). General form: Strcat(string 1, string 2) Appends string 2 to string 1;

Strings (cont). General form: Strcat(string 1, string 2) Appends string 2 to string 1; 6 -9 -15 cfy 51

Addresses! & used to get the address of a variable main() { int i=666;

Addresses! & used to get the address of a variable main() { int i=666; printf("i= %dn", i); printf("&i= %dn", &i); } afsconnect 1 -72 public_html >: i= 666 &i= 1814055836 6 -9 -15 cfy . /a. out 52

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

More C keywords switch break, continue, goto sizeof Storage classes: auto register extern static 6 -9 -15 cfy 53

switch( Grade ) { case 'A' : printf( "Excellentn" ); case 'B' : printf(

switch( Grade ) { case 'A' : printf( "Excellentn" ); case 'B' : printf( "Goodn" ); case 'C' : printf( "OKn" ); case 'D' : printf( "Mmmmm. . n" ); case 'F' : printf( "You must do better than thisn" ); default : printf( "What is your grade anyway? n" ); } 6 -9 -15 cfy 54

label: while (true) { if(a) break; if(b) continue; if(c) goto label; } 6 -9

label: while (true) { if(a) break; if(b) continue; if(c) goto label; } 6 -9 -15 cfy 55

main() { int i=666; char array[15]; double n; printf("sizeof i= %dn", sizeof(i)); printf("sizeof array=

main() { int i=666; char array[15]; double n; printf("sizeof i= %dn", sizeof(i)); printf("sizeof array= %dn", sizeof(array)); printf("sizeof n= %dn", sizeof(n)); 6 -9 -15 cfy } >: . /a. out sizeof i= 4 sizeof array= 15 sizeof n= 8 56

static Has number of implications: • Local static variables initialized only once and retain

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. 6 -9 -15 cfy 57

Structures! struct <name> { element 1; element 2; … } 6 -9 -15 cfy

Structures! struct <name> { element 1; element 2; … } 6 -9 -15 cfy 58

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

Structures example struct class { char name[30]; int number; char instructor[25]; int students[35]; }; 6 -9 -15 cfy 59

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

Structures element dereferencing structure-variable. stucture-member; e. g. : x=class. number; 6 -9 -15 cfy 60

Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int

Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int grades[10]; }g[35]; 6 -9 -15 cfy 61

Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; 6 -9

Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; 6 -9 -15 cfy 62

 • sizeof can be applied to unions and structures • Structures can be

• sizeof can be applied to unions and structures • Structures can be nested. 6 -9 -15 cfy 63

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

Pointers General form: datatype *ptrname; int x=666, y=555; Int *p; p = &x; y = *p; 6 -9 -15 cfy 64

Some cc command line options • -I • -L • -c • -o •

Some cc command line options • -I • -L • -c • -o • Compiling multiple C files. • Linking multiple objects.

$ cat main. c main(){ func(); } $ cat sub. c func(){} $ cc

$ cat main. c main(){ func(); } $ cat sub. c func(){} $ cc main. c /tmp/cc. RFdy. U 8. o: In function `main': main. c: (. text+0 xa): undefined reference to `func' collect 2: error: ld returned 1 exit status $ cc sub. c /usr/lib/gcc/x 86_64 -redhat-linux/4. 8. 5/. . /lib 64/crt 1. o: In function `_start': (. text+0 x 20): undefined reference to `main' collect 2: error: ld returned 1 exit status $ cc $ main. c sub. c cc -c main. c $ cc -c sub. c $ cc main. o sub. o $ cc $ main. c sub. o

$ cat main. c #include "tmp. h" main(){ func(); } $ cc -c main.

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

Homework • Do exercise 1 -19 from the text. • Do exercise 1 -21

Homework • Do exercise 1 -19 from the text. • Do exercise 1 -21 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. 6 -9 -15 cfy 68

problem 4 Write a C program, sort. c, which sorts a list of command

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 7 2 3 9 -1 and type enter. Your program will return: -1 2 3 7 9 6 -9 -15 cfy 69