Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 1730 April 2 nd, 2014. C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled;

Similar presentations


Presentation on theme: "CSCI 1730 April 2 nd, 2014. C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled;"— Presentation transcript:

1 CSCI 1730 April 2 nd, 2014

2 C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled; x=4; y=7; a='H'; f=-3.4; d=54.123456789; e=54.123456789; printf("%d %c %f %lf\n",x,a,e,d); printf("%d %c %.9f %.9lf\n",x,a,e,d); }

3 C review – arithmetic /* A review of the basic arithmetic operators in C. */ #include int main() { int x,y; int r1,r2,r3,r4,r5; x=4; y=7; r1=x+y; r2=x-y; r3=x/y; r4=x*y; printf("%d %d %d %d\n",r1,r2,r3,r4); r3++; r4--; r5=r4%r1; printf("%d %d %d\n",r3,r4,r5); }

4 C review – loops #include /* A review of the loop types in C. */ int main() { int i,x; x=0; for (i=0; i<4; i++) { x=x+i; printf("%d\n",x); } while (i<7) { x=x+i; i++; printf("%d\n",x); } do { x=x+i; i++; printf("%d\n",x); } while (i<9); }

5 C review – blocks /* A review of conditionals and blocks in C. */ #include int main() { int i,x; x=0; for (i=0; i<5; i++) { if (i%2 == 0 || i == 1) x=x+i; else x=x-i; printf("%d\n",x); }

6 C review – flow control /* A review of flow control statements in C. */ #include int main() { int i,x; x=0; for (i=0; i<5; i++) { if (i%2 == 0) continue; x=x-i; if (i%4 == 0) break; printf("%d\n",x); }

7 ASCII /* This program shows the dual interpretations of char and ** unsigned char data types. */ #include main() { char a; unsigned char b; a='A'; b='B'; printf("%c %c %d %d\n",a,b,a,b); a=183; b=255; printf("%d %d\n",a,b); }

8 sizeof() operator /* This program demonstrates the sizeof() operator. */ #include main() { int i; char c; double d; printf("%d %d %d %d\n",sizeof(i),sizeof(c),sizeof(d),sizeof(float)); }

9 Bitwise NOT /* This program demonstrates the bitwise not operator. */ #include main() { unsigned char a; a=17; a=~a; printf("%d\n",a); }

10 Bitwise AND /* This program demonstrates the bitwise and operator. */ #include main() { unsigned char a,b; a=17; b=22; a=a & b; printf("%d\n",a); }

11 Bitwise OR /* This program demonstrates the bitwise or operator. */ #include main() { unsigned char a,b; a=17; b=22; a=a | b; printf("%d\n",a); }

12 Bit operators (variables and constants) /* This program demonstrates using the bitwise operators ** with variables and constants. */ #include main() { char x,y; x=7; y=6; x=x&y; y=x|16; printf("%d %d\n",x,y); }

13 Left/Right shift /* This program demonstrates the bitwise shift operators, ** left-shift and right-shift. */ #include main() { unsigned char a,b; a=17; a=a << 2; b=64; b=b >> 3; printf("%d %d\n",a,b); }

14 Bitwise right-shift (negative) /* This program demonstrates right-shifting a negative integer ** so that the shifted-in bits are 1 instead of 0 */ #include main() { char a,b; a=17; a=a >> 2; b=-65; b=b >> 2; printf("%d %d\n",a,b); }

15 Bitmasks /* This program demonstrates setting a bit, clearing a bit, and ** reading a bit. */ #include main() { char a; int i; a=17; a=a | (1 << 3); /* set 3rd bit */ printf("%d\n",a); a=a & (~(1<<4)); /* clear 4th bit */ printf("%d\n",a); for (i=7; i>=0; i--) printf("%d ",(a&(1 > i); /* read i'th bit */ printf("\n"); }

16 Memory map 1 /* Draw the memory map for this code (pg 65) */ #include main() { char a,b,c; a=7; b=-13; c=0; }

17 Memory map 2 /* Draw the memory map for this code (pg 65) */ #include main() { char a; int b; float c; double d; a=7; b=-13; c=0.1; d=42.5; }

18 Memory map 3 /* Draw the memory map for this code. Showing the bit patterns ** emphasizes the differences in storage of "6" (pg 66) */ #include main() { char a; short int b; char c; a=6; b=13; c='6'; }

19 Memory map 4 /* Draw the memory map for this code. It can emphasize the ** value of a loop counter after the loop is done (pg 67) */ #include main() { int i,n; n=0; for (i=1; i<=4; i++) n=n+i; }

20 Memory map 5 /* Draw the memory map for this code. It can emphasize an ** unitialized variable (pgs 67-68) */ #include main() { int i,sum; printf("%d\n",sum); for (i=1; i<=10; i++) if (i%2 == 0) sum=sum+i; printf("%d\n",sum); }

21

22 File Types in Unix regular file - can be text, binary, can be executable directory file - "folder type" file FIFO file - special pipe device file, allows unrelated processes to communiate block device file - represents physical device that transmit data a block at a time character device file - represents physical device that doesn't necessarily transmit data a block at a time symbolic link file - contains a pathname that references another file (some versions of UNIX)

23 creating and removing files directory created using mkdir, removed using rmdir mkdir /usr/tmp/junkdir rmdir /usr/tmp/junkdir device files created using mknod (need to be superuser) mknod /dev/cdsk c 115 5 mknod /dev/bdsk b 287 101

24 Creating and removing files FIFO created using mkfifo mkfifo /usr/tmp/fifo.mine to make a link, use ln : ln -s /usr/jose/original /usr/mary/slink then, if you type more /usr/mary/slink you get the contents of /usr/jose/original... the link is followed (like a pointer)

25 File attributes file type access permission - for owner, group, other hard link count uid - user id of the file owner (linked to user via password file) gid - group id of the file owner file size - in bytes last access time last modify time last change time – time file permission, uid, gid, or hard link count changed inode number - sytem inode number of the file file system id major and minor device numbers

26 File attributes You can see many of the file attributes by typing: ls -l -rw-r--r--. 1 eileen users 199 Apr 2 04:41 arithmetic.c -rw-r--r--. 1 eileen users 142 Apr 2 04:41 ascii.c -rw-r--r--. 1 eileen users 205 Apr 2 04:41 basic_types.c -rw-r--r--. 1 eileen users 91 Apr 2 04:41 bit.c -rw-r--r--. 1 eileen users 253 Apr 2 04:41 bitmask.c -rw-r--r--. 1 eileen users 155 Apr 2 04:41 blocks.c -rw-r--r--. 1 eileen users 6021 Apr 1 08:41 buggy.c -rw-r--r--. 1 eileen users 6178 Apr 1 08:33 commented.c

27 File attributes used by the kernel in managing files uid and gid are compared against those of a process attempting to access the file to determine which access permissions apply The make utility looks at the last modification time Not all fields make sense for every file: size doesn't apply to device files device number doesn't apply to regular files

28 Some file attributes can’t be changed inode number file type file system id major and minor device number

29 Changing file attributes -- UNIX command ---- System call ---- Attributes changed -- chmod access permissions last change time chown uid last change time chgrpchown gid last change time chmod access permissions last change time touchutime last access time modification time lnlink increase hard link count rmunlink decrease hard link count


Download ppt "CSCI 1730 April 2 nd, 2014. C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled;"

Similar presentations


Ads by Google