CS133C Programming in C

Wednesday
Feb222012

Assignment 5

Read Allocated Memory: malloc() and free(), pages 480–485 (stop before Dynamic Memory Allocation and Variable-Length Arrays). Then read Chapter 13. Send me your questions by 5:00 Sunday, Feb. 26.

Friday
Feb172012

Lab 3 switch Statement Derivation

char input[] = "SSSWILTECH1\1\11W\1WALLMP1";

main()
{
  int i, c;
  for( i=2; (c=input[i])!='\0'; i++ ) {
    switch(c) {
      case 'a':   putchar('i'); continue;
      case '1':   break;
      case 1:     while( (c=input[++i])!='\1' && c!='\0' );
      case 9:     putchar('S');
      case 'E':
      case 'L':   continue;
      default:    putchar(c);
                  continue;
    }
    putchar(' ');
  }
  putchar('\n');
}

The explanation:

char input[] = "SSSWILTECH1\1\11W\1WALLMP1"
The character array input is initialized to the character string "SSS...MP1".
for( i=2; (c=input[2])!='\0';
c takes character values from input beginning at the third character.
switch('S')
The first time through the switch statement c='S'.
default: putchar('S');
The default case is taken since none of the cases match 'S'. S is printed/
continue
The continue statement forces the next iteration of the innermost enclosing loop; in this case, the for loop. Notice that the continue effectively is a branch to the reinitialization expression of the for.
for( ; c=input[3])!='\0'; i++) {
c gets the fourth character from input.
switch('W')
c='W'
default: putchar('W'); continue
As before, W is printed.
...
Similarly for i=4, c='I'.
switch('L') {
i=5, c='L'.
case 'L': continue
The 'L' case is taken, nothing is printed.
i=5, c='L'
Nothing is printed.
i=6, c='T'
T is printed.
i=7, c='E'
Nothing is printed.
i=8, c='C'
C is printed.
i=9, c='H'
H is printed.
switch('1') {
i=10, c='1'
case '1': break
The break statement forces an exit from the innermost enclosing loop or switch. In this case it effects a branch to the statement following the switch.
putchar(' ')
A space is printed.
for( ; (c=input[11])!='\0'; i++) {
Back to the top of the for loop.
switch('\1') {
The character constant '\n', where n is up to three octal digits, yields a character with the octal value n. For example, \0 yields the ASCII character null and \101 the character A.
case 1:
Case labels may be either character or integer constants. \1 matches since C automatically coerces char to int.
while( (c=input[++i])!='\1' && c!='\0' );
The exit condition for the while is either c=='\1' or end of string. Each time the while test is made, i is incremented by , thus the loop advances i through the characters of input until either the next '\1' character or the end of the string.

In the while loop:

i=12, c='\11';
Nothing is printed.
i=13, c='W';
Nothing is printed.
i=14, c='\1';
The while loop terminates.
case 9: putchar('S')
The statements from each case follow one another directly; there is no implied break between cases. Case 9 follows case 1. S is printed.
case 'E': case 'L': continue
Cases 'E' and 'L' follow case 9.
for( ; (c=input[15]); i++) {
Again, back to the top of the for loop.

In the for loop:

i=15, c='W';
W is printed.
i=16, c='A';
A is printed.
i=17, c='L';
Nothing is printed.
i=18, c='L';
Nothing is printed.
i=19, c='M';
M is printed.
i=20, c='P';
P is printed.
i=21, c='1';
A space is printed.
i=22, c='\0';
The for loop terminates.
putchar('\n')

If you think the switch about was hard to read, you may want to visit the web site for the The International Obfuscated C Code Contest. Here's a couple of examples of what you can find there...

This one-line program displays a 7-segment digital clock showing the time that the program was compiled.

main(_){_^448&&main(-~_);putchar(--_%64?32|-~7[__TIME__-_/8%8][">'txiZ^(~z?"-48]>>";;;====~$::199"[_*2&8|_/64]/(_&2?1:8)%8&1:10);}

The following program (along with some needed support files availalbe on the IOCCC web site is a complete 8080 emulator.

                               #include 
           #define n(o,p,e)=y=(z=a(e)%16 p x%16 p o,a(e)p x p o),h(
                                #define s 6[o]
             #define p z=l[d(9)]|l[d(9)+1]<<8,1<(9[o]+=2)||++8[o]
                                #define Q a(7)
           #define w 254>(9[o]-=2)||--8[o],l[d(9)]=z,l[1+d(9)]=z>>8
                               #define O )):((
                  #define b (y&1?~s:s)>>"\6\0\2\7"[y/2]&1?0:(
                               #define S )?(z-=
                    #define a(f)*((7&f)-6?&o[f&7]:&l[d(5)])
                               #define C S 5 S 3
                       #define D(E)x/8!=16+E&198+E*8!=x?
                             #define B(C)fclose((C))
                       #define q (c+=2,0[c-2]|1[c-2]<<8)
                          #define m x=64&x?*c++:a(x),
                         #define A(F)=fopen((F),"rb+")
                    unsigned char o[10],l[78114],*c=l,*k=l
                          #define d(e)o[e]+256*o[e-1]
#define h(l)s=l>>8&1|128&y|!(y&255)*64|16&z|2,y^=y>>4,y^=y<<2,y^=~y>>1,s|=y&4
+64506; e,V,v,u,x,y,z,Z; main(r,U)char**U;{

     { { { } } }       { { { } } }       { { { } } }       { { { } } }
    { { {   } } }     { { {   } } }     { { {   } } }     { { {   } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
    { { {   } } }    { { {     } } }    { { {   } } }    { { {     } } }
      { { ; } }      { { {     } } }      { { ; } }      { { {     } } }
    { { {   } } }    { { {     } } }    { { {   } } }    { { {     } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
   { { {     } } }   { { {     } } }   { { {     } } }   { { {     } } }
    { { {   } } }     { { {   } } }     { { {   } } }     { { {   } } }
     { { { } } }       { { { } } }       { { { } } }       { { { } } }

                                   for(v A((u A((e A((r-2?0:(V A(1[U])),"C")
),system("stty raw -echo min 0"),fread(l,78114,1,e),B(e),"B")),"A")); 118-(x
=*c++); (y=x/8%8,z=(x&199)-4 S 1 S 1 S 186 S 2 S 2 S 3 S 0,r=(y>5)*2+y,z=(x&
207)-1 S 2 S 6 S 2 S 182 S 4)?D(0)D(1)D(2)D(3)D(4)D(5)D(6)D(7)(z=x-2 C C C C
C C C C+129 S 6 S 4 S 6 S 8 S 8 S 6 S 2 S 2 S 12)?x/64-1?((0 O a(y)=a(x) O 9
[o]=a(5),8[o]=a(4) O 237==*c++?((int (*)())(2-*c++?fwrite:fread))(l+*k+1[k]*
256,128,1,(fseek(y=5[k]-1?u:v,((3[k]|4[k]<<8)<<7|2[k])<<7,Q=0),y)):0 O y=a(5
),z=a(4),a(5)=a(3),a(4)=a(2),a(3)=y,a(2)=z O c=l+d(5) O y=l[x=d(9)],z=l[++x]
,x[l]=a(4),l[--x]=a(5),a(5)=y,a(4)=z O 2-*c?Z||read(0,&Z,1),1&*c++?Q=Z,Z=0:(
Q=!!Z):(c++,Q=r=V?fgetc(V):-1,s=s&~1|r<0) O++c,write(1,&7[o],1) O z=c+2-l,w,
c=l+q O p,c=l+z O c=l+q O s^=1 O Q=q[l] O s|=1 O q[l]=Q O Q=~Q O a(5)=l[x=q]
,a(4)=l[++x] O s|=s&16|9159?Q+=96,1:0,y=Q,h(s<<8)
O l[x=q]=a(5),l[++x]=a(4) O x=Q%2,Q=Q/2+s%2*128,s=s&~1|x O Q=l[d(3)]O x=Q  /
128,Q=Q*2+s%2,s=s&~1|x O l[d(3)]=Q O s=s&~1|1&Q,Q=Q/2|Q<<7 O Q=l[d(1)]O s=~1
&s|Q>>7,Q=Q*2|Q>>7 O l[d(1)]=Q O m y n(0,-,7)y) O m z=0,y=Q|=x,h(y) O m z=0,
y=Q^=x,h(y) O m z=Q*2|2*x,y=Q&=x,h(y) O m Q n(s%2,-,7)y) O m Q n(0,-,7)y)  O
m Q n(s%2,+,7)y) O m Q n(0,+,7)y) O z=r-8?d(r+1):s|Q<<8,w O p,r-8?o[r+1]=z,r
[o]=z>>8:(s=~40&z|2,Q=z>>8) O r[o]--||--o[r-1]O a(5)=z=a(5)+r[o],a(4)=z=a(4)
+o[r-1]+z/256,s=~1&s|z>>8 O ++o[r+1]||r[o]++O o[r+1]=*c++,r[o]=*c++O z=c-l,w
,c=y*8+l O x=q,b z=c-l,w,c=l+x) O x=q,b c=l+x) O b p,c=l+z) O a(y)=*c++O r=y
,x=0,a(r)n(1,-,y)s<<8) O r=y,x=0,a(r)n(1,+,y)s<<8))));
system("stty cooked echo"); B((B((V?B(V):0,u)),v)); }
Wednesday
Feb152012

Assignment 4

Complete the following Programming Exercises starting on page 301 of your book: #2, #3, #8. Complete the following Programming Exercises staring on page 345 of your book: #6 and #7. You may do #8 on page 345 for extra credit.

Due Wednesday, Feb. 22.

Read Chapter 10 for class on Wednesday. E-mail me your questions no later than noon on Tuesday, Feb. 21.

You should expect a quiz on Wedneday.

Tuesday
Feb142012

Lab 3

Tuesday
Feb142012

FILE Sample Code

#include 
#include 

int main(int argc, char const *argv[])
{
    int ch;
    FILE *fp; // ignore the woman behind the curtain
    char fname[50];

    printf("Enter the name of the file: ");
    scanf("%s", fname);
    fp = fopen(fname, "r");
    if(!fp)
    {
        printf("Failed to open file %s.", fname);
        return -1;
    }

    while((ch = getc(fp)) != EOF)
        putchar(ch);

    fclose(fp);

    return 0;
}