csci 1730 april 2 nd, 2014. c review – 4 data types /* a review of the basic data types in c. */...

29
A “review” of C CSCI 1730 April 2 nd , 2014

Upload: ashlie-mccormick

Post on 24-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

A “review” of CCSCI 1730April 2nd, 2014

C review – 4 data types

/* A review of the basic data types in C. */

#include <stdio.h>

int main(){int x,y;char a;float f,e;double d;

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);}

C review – arithmetic

/* A review of the basic arithmetic operators in C. */

#include <stdio.h>

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);}

C review – loops#include <stdio.h>

/* 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);}

C review – blocks

/* A review of conditionals and blocks in C. */

#include <stdio.h>

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); }}

C review – flow control

/* A review of flow control statements in C. */

#include <stdio.h>

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); }}

ASCII

/* This program shows the dual interpretations of char and** unsigned char data types. */

#include <stdio.h>

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);}

sizeof() operator

/* This program demonstrates the sizeof() operator. */

#include <stdio.h>

main(){int i;char c;double d;

printf("%d %d %d %d\n",sizeof(i),sizeof(c),sizeof(d),sizeof(float));}

Bitwise NOT

/* This program demonstrates the bitwise not operator. */

#include <stdio.h>

main(){unsigned char a;

a=17;a=~a;printf("%d\n",a);}

Bitwise AND

/* This program demonstrates the bitwise and operator. */

#include <stdio.h>

main(){unsigned char a,b;

a=17;b=22;a=a & b;printf("%d\n",a);}

Bitwise OR

/* This program demonstrates the bitwise or operator. */

#include <stdio.h>

main(){unsigned char a,b;

a=17;b=22;a=a | b;printf("%d\n",a);}

Bit operators (variables and constants)

/* This program demonstrates using the bitwise operators** with variables and constants. */

#include <stdio.h>

main(){char x,y;

x=7;y=6;x=x&y;y=x|16;printf("%d %d\n",x,y);}

Left/Right shift

/* This program demonstrates the bitwise shift operators,** left-shift and right-shift. */

#include <stdio.h>

main(){unsigned char a,b;

a=17;a=a << 2;b=64;b=b >> 3;printf("%d %d\n",a,b);}

Bitwise right-shift (negative)

/* This program demonstrates right-shifting a negative integer** so that the shifted-in bits are 1 instead of 0 */

#include <stdio.h>

main(){char a,b;

a=17;a=a >> 2;b=-65;b=b >> 2;printf("%d %d\n",a,b);}

Bitmasks

/* This program demonstrates setting a bit, clearing a bit, and** reading a bit. */

#include <stdio.h>

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)) >> i); /* read i'th bit */printf("\n");}

Memory map 1

/* Draw the memory map for this code (pg 65) */

#include <stdio.h>

main(){char a,b,c;

a=7;b=-13;c=0;}

Memory map 2

/* Draw the memory map for this code (pg 65) */

#include <stdio.h>

main(){char a;int b;float c;double d;

a=7;b=-13;c=0.1;d=42.5;}

Memory map 3

/* Draw the memory map for this code. Showing the bit patterns** emphasizes the differences in storage of "6" (pg 66) */

#include <stdio.h>

main(){char a;short int b;char c;

a=6;b=13;c='6';}

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 <stdio.h>

main(){int i,n;

n=0;for (i=1; i<=4; i++) n=n+i;}

Memory map 5

/* Draw the memory map for this code. It can emphasize an** unitialized variable (pgs 67-68) */

#include <stdio.h>

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);}

Files and Directories

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)

creating and removing filesdirectory 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

Creating and removing filesFIFO 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)

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

File attributesYou 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

File attributesused 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 filesdevice number doesn't apply to regular files

Some file attributes can’t be changedinode numberfile typefile system idmajor and minor device number

Changing file attributes-- UNIX command --

-- System call -- -- Attributes changed --

chmod chmod access permissionslast change time

chown chown uidlast change time

chgrp chown gidlast change time

chmod chmod access permissionslast change time

touch utime last access timemodification time

ln link increase hard link count

rm unlink decrease hard link count