home *** CD-ROM | disk | FTP | other *** search
- /*
- Demonstrates many of the capabilities of C compiler
-
- typedefs, structs, function pointers, nested structs, initializers,
- enums, unions, local static variables etc
- */
-
- union
- {
- int x;
-
- struct astrtype
- {
- int x1 : 13, x2 : 2;
- int x3 : 7, x4 : 6;
-
- int b;
- }
- y, *yp;
- } uni, *unip;
-
- union { int a; char b; } seven = {7};
-
- struct astrtypeo { int a; char *s; int b; } astr = { 1, "astr", 2 };
-
- enum { red, blue } colour = blue;
-
- double doud=2.3232;
- long woper=765432L;
-
- int m1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } },
-
- m2[2][3][2] = { { {1,2},{3,4},{5,6} },
- { {7,8},{9,1},{2,3} } },
- m3[3] = { 1,2,3 };
-
- char ra[]="this is a string", *rb=&((ra+2)[3]);
-
- struct
- {
- int x1 : 13, x2 : 2;
- int x3 : 7, x4 : 6;
-
- int b;
- }
- *azyp, azy = { 1, 2, 3, 4, 5 };
-
- char uu[5][4][3]; char uu[][4][3];
- int yyt=4?2:1+7;
-
- char tt[][10]={"this","is","a","word"},
- day1[][10]={"sunday","monday","et","cetera","etc"},
- ff[]="another string", *day2[]={"tuesday","wednesday"};
-
- int zza[] = { 1, 2, 3 }, zzb[] = { 4, 5, 6 }, zzc[] = {7, 8, 9},
- aa[] = { 32 }, qq=96+1, *hh=&qq,
- *zzpa[] = { zza, zzb, zzc },
- **qqp[] = { zzpa, zzpa+1, &(zzpa[1]) };
-
- typedef void vfun();
- vfun main,arsub,filefunc,scopefun;
- void printnum(int n), subrun(int a,int b);
- int subbi(), n, subtwo(int a,int b);
- long lfun();
-
- int tax[] = { 1, 2, 'a' }, tox[5] = { 4, 5, 'b', 6, 7 };
- char cax[] = { 1, 2, 'a' }, cox[5] = { 4, 5, 'b', 6, 7 };
-
- unsigned zw=0xface;
- char zm[]="abc", zn[]={'a','b','c','\0'};
-
- struct mak { int a; char b; } yone, *ytwo, globar[5];
- struct sec { long alpha, *beta[4], *gamma; char b; int *x, y, z[2]; } *s1;
-
- typedef int alphat, betat[5], gammat;
- alphat alfa;
- betat betertee;
-
- struct mak macone, *mactwo, macthree[3];
-
- typedef int *upperbongo;
-
- typedef int *FPTRA();
-
- typedef int (*FPTR)();
-
- union mate { int a; char b; long *c; char d; };
- int ;
-
- struct mystruct
- {
- struct mak myitem;
- long longvar;
- int intvar;
- };
-
- #define suminus(j,a) j+a-2
-
- char yyz[]="yyzb", *cat="whiskas", ssbuf[20]="hello world";
-
- int *giarp, arri[10], *giary[5];
- long *glurp, arrl[5], *blurb[10];
-
- char cvary='a';
- int ivar=42, gvar=45;
-
-
- #include <io.h>
- #include <stdio.h>
- #include <string.h>
- #include <system.h>
-
- int asub(z,x,y) char z; int x,y; { printf("%c",z); return x+y; }
-
- void main()
- {
- int dgvar=gvar, gvar=7;
-
- void *varptr, *varray[10];
- int (*funpt)();
- FPTR funptrr;
- void (*subp)(int p1, int p2);
- int (*subq)(int p1, int p2);
-
- double xx=3.141592, yy=1.234567;
-
- static int stak1, *stak2[20];
- static struct mak makk[2], *makk2, *makk3, *makk4;
-
- int sum, value, test, iar[10], *iarray[10];
- unsigned char flag, *apt, car[5];
- long *lap, lar[4], lvar1, lvar2;
-
- struct mak indie, *indip, *idip[3], idi[4], indthree;
- struct mystruct anstruct;
-
- enum numer { zero, one, two, free } numba;
-
- typedef struct astag { int x,y; struct astag *z; } astype;
- astype asinst;
-
- struct pal
- {
- struct mal
- {
- int a;
- char b;
- }
- palne;
-
- struct
- {
- int w1, w2;
- long lw;
- }
- mfing;
-
- union mate matey, *pmate;
- long *longvar;
- int intvar;
- };
-
- static int svar=24;
- static char sch='b';
-
- double ddza,ddzb;
-
- union {
- struct { unsigned int l1; unsigned int l2;
- unsigned int l3; unsigned int l4; } lx;
-
- double b; }
- ab;
-
- static double pipi=3.1415926536;
- double duba=1.2345-1.1111, dubb=duba*1.1111;
- int mrakx,mraky;
-
- long wopa=234567L;
- static long wopra=134567L;
-
- int mama=34,Mama=43,Ma_ma=42;
-
- int mack=5;
-
- struct pal palthing;
- struct mal mal1[2];
-
- struct astruct
- {
- char name[10];
- int x,y;
- }
- namei[10] = { { "Myname", 4, 5 },
- { "2name", 2, 3 } };
-
- struct bstruct
- {
- char x[10];
- struct astruct abstruct;
- }
- acount[10] = { { "first", { "Myname", 9, 5 } },
- { "second", { "Yourname", 2, 7 } } };
-
- struct { int t[3][2][4]; long h; } ilstruct[2][4], *ilsptr;
-
- int (*fptr)(char *fmt,...), i,j,k;
- int uu[9][3];
- char dstr[5]="aabb";
-
- int *xt=uu[2];
- int sival=subbi();
-
- int ux[3][4]={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
- int uy[3][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
-
- char *day[] = { "Mon", "Tues", "Wed" };
- char s[5][10] = { "cat", "dog", "bird" };
-
- static int qa[]={1,2,3}, qb[]={4,5,6}, qc[]={7,8,9};
- static int *pa[]={qa,qb,qc};
-
- struct
- {
- int x1 : 13, x2 : 2;
- int x3 : 7, x4 : 6;
-
- int b;
- }
- *zyp, zy = { 1, 2, 3, 4, 5 };
-
- printf("%d ",asub('x',1,2));
- printf("%d%d%d%d%d",zy.x1,zy.x2,zy.x3,zy.x4,zy.b);
- printf("%d%d%d%d%d ",azy.x1,azy.x2,azy.x3,azy.x4,azy.b);
-
- uni.x=6; printf("%d ",uni.y.x2);
- uni.y.x1=5; uni.y.x2=1; uni.y.x3=23; uni.y.x4=11;
-
- printf("%d %d %d %d ",uni.y.x1,uni.y.x2,uni.y.x3,uni.y.x4);
- printf("%d %d ",uni.y.x1+uni.y.x2,uni.y.x3+uni.y.x4);
- printf("%d %d ",uni.y.x1-uni.y.x2,uni.y.x3-uni.y.x4);
-
- printf("%d %d ",uni.y.x1*uni.y.x2,uni.y.x3*uni.y.x4);
- printf("%d %d ",uni.y.x1/uni.y.x2,uni.y.x3/uni.y.x4);
-
- uni.y.x1+=7; printf("%d ",uni.y.x1);
- uni.y.x1*=2; printf("%d ",uni.y.x1);
- uni.y.x1/=3; printf("%d ",uni.y.x1);
- uni.y.x1%=3; printf("%d ",uni.y.x1);
-
- printf("%d ",++uni.y.x2); uni.y.x2++; printf("%d ",uni.y.x2++);
- printf("%d ",uni.y.x2); ++uni.y.x2; printf("%d ",uni.y.x2);
- printf("%d ",++uni.y.x2);
-
- zy.x3=28; zyp=&zy;
- printf("-> %d %d\n",zy.x3,zyp->x3);
-
- for(i=0; i<3; i++)
- for(j=0; j<4; j++)
- printf("%d%d",ux[i][j],uy[i][j]);
-
- for(i=0; i<3; i++)
- for(j=0; j<3; j++)
- printf("%d",pa[i][j]);
- printf("%d %d ",dgvar,gvar);
-
- scopefun();
-
- printf("%d%d%d%d%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",
- sizeof(astr),sizeof(m1),sizeof(m2),sizeof(m3),sizeof(ra),sizeof(rb),
- sizeof(tt),sizeof(day1),sizeof(ff),sizeof(day2),
- sizeof(zza),sizeof(zzb),sizeof(zzc),sizeof(qq),sizeof(aa),sizeof(hh),
- sizeof(zzpa),sizeof(qqp),sizeof(tax));
-
- printf("%d %d %d %d %d %d %d %d %d %d %d %d\n",
- sizeof(tox),sizeof(cax),sizeof(cox),sizeof(zw),
- sizeof(zm),sizeof(zn),sizeof(yyz),sizeof(cat),
- sizeof(ssbuf),sizeof(cvary),sizeof(ivar),seven.a);
-
- printf("%c%d %d%d%s %d %d ",*rb,sival,astr.a,astr.b,astr.s,
- sizeof(m2[1]),sizeof(tt[1]));
- for(i=0; i<4; i++) printf("%s ",tt[i]);
-
- for(i=0; i<2; i++)
- for(j=0; j<3; j++)
- for(k=0; k<2; k++)
- printf("%d ",m2[i][j][k]);
- printf("\n");
-
- ilstruct[1][3].t[1][1][2]=4; ilsptr=&ilstruct[1][3];
- printf("%d%s%d ",ilsptr->t[1][1][2],dstr,*hh);
-
- printf("%d %d %d %d %d %d %d %d %d %d %d %d X ",zza[0],zza[1],zza[2],
- sizeof(char),sizeof(char),*aa,zzb[0],zzb[1],zzb[2],zzc[0],zzc[1],zzc[2]);
-
- for(i=0; i<3; i++)
- for(j=0; j<2; j++)
- for(k=0; k<3; k++)
- printf("%d ",qqp[i][j][k]);
-
- printf("\n%d %d %d %s %s %s mack=%d 5 ivar=%d 42 cvary=%x a(%x) ",
- tax[0],tax[1],tax[2],ssbuf,yyz,cat,mack,ivar,cvary,'a');
-
- printf("%d%d%d%d",yy<xx,xx<yy,yy==xx,xx!=yy);
- printf("%d%d%d%d",yy<=xx,xx<=yy,yy>xx,xx>yy);
- printf("%d%d\n",yy>=xx,xx>=yy);
-
- printf("%d %x tox %d %d %d %d %d sv=%d 24 sc=%x b(%x)\n",
- zw,zw,tox[0],tox[1],tox[2],tox[3],tox[4],svar,sch,'b');
-
- cat=ssbuf;
- printf("%s%s %s cax %d %d %d cox %d %d %d %d %d\n",zm,zn,
- cat,cax[0],cax[1],cax[2],cox[0],cox[1],cox[2],cox[3],cox[4]);
-
- (fptr=&printf,*fptr)("hello");
-
- filefunc();
-
- funptrr=&subbi; printf("%d ",(*funptrr)());
- subrun(12,3+subtwo(4,1+subtwo(3,4))); printf("\n");
-
- subp=&subrun; (*subp)(12,3+subtwo(4,7));
- subq=&subtwo; (*subp)(12,3+(*subq)(4,7));
-
- betertee[3]=2; alfa=betertee[3]; makk4=1+makk;
- makk[1].a=37; makk2=&makk[1]; makk3=makk+1; stak1=3; stak2[0]=&stak1;
- printf("%d%d %d=%d=%d ",stak1,*stak2[0],makk2->a,makk3->a,makk4->a);
-
- printf("%d %d %d ",sizeof(struct pal),sizeof(numba),sizeof(palthing));
- printf("%d %d %d ",sizeof(struct pal *), sizeof(void *), sizeof(mal1));
- printf("%d %d%d\n",sizeof(mal1[1]),sizeof(&mal1[1]),alfa);
-
- arsub(); printf("%ld ",lfun());
-
- somewere:
-
- palthing.palne.b=65;
- anstruct.myitem.b=65; indip=&(anstruct.myitem);
- palthing.mfing.w2=89; mal1[1].b=65;
- printf("%d %d",palthing.mfing.w2,palthing.palne.b);
- printf("%c%c%c",mal1[1].b,indip->b,"tunes"[1]);
-
- if(stak1==3) { stak1=4; goto somewere; }
-
- indie.a=4; globar[2].b='y'; idip[2]=&idi[2]; idi[2].a=42;
- printf("%d %c %d ",indie.a,globar[2].b,idi[2].a);
-
- indip=&idi[2]; indip->a=43;
- printf("%d %c %d %d ",indie.a,globar[2].b,idi[2].a,indip->a);
-
- for(i=0; i<3; i++) printf("%s ",s[i]);
- for(i=0; i<3; i++) printf("%s ",day[i]);
- printf("\n");
-
- sum=suminus(1,2+3);
- do printf("%d ",sum--); while(sum);
-
- numba=free; printf("numba=%d ",numba);
-
- printnum(2*3+7);
- sum=3; sum+=5; printnum(sum); sum<<=1; printnum(sum);
- sum>>=2; printnum(sum); sum%=3; printnum(sum);
- putchar((2>1)?'y':'n'); putchar((2>3)?'n':'y');
-
- /* local stack and heap arrays */
-
- iarray[9]=&iar[4]; *iarray[9]=7;
- apt=&car[2]; *apt=1; iar[0]=3; *iar=2;
-
- lvar1=(long)42; lvar2=lvar1+(long)12;
- varray[3]=varptr; iar[5]=1; apt=malloc(20); apt[3]='b'; car[0]='c';
- lap=malloc(20); lap[3]=2; lar[2]=3;
-
- printf("%d%d%d%d %ld%ld ",iar[0],*iar,iar[4],car[2],lap[3],lar[2]);
- printf("%d",iar[5]); printf("%c%c%ld ",apt[3],car[0],lvar2);
-
- /* global static and heap arrays */
-
- giary[2]=&iar[4]; *giary[2]=6; printf("%d",iar[4]);
- cat=malloc(10); cat[3]='d'; printf("%c",cat[3]);
- giarp=malloc(10); giarp[2]=4; arri[2]=5;
- printf("%d%d",giarp[2],arri[2]); glurp=malloc(20); glurp[0]=6;
- arrl[1]=7; printf("%ld%ld\t",glurp[0],arrl[1]);
-
- subrun(12,3+subtwo(4,7));
- printf("\n");
-
- if((1>2) && 3) printf(" no");
- if((2>1) && 3) printf(" yes");
-
- if(1>2) printf("n"); else printf("y");
- if(2>1) printf("y"); else printf("n");
-
- test=3*4+1; printf("%d",test);
-
- for(sum=0, value=1; value<11; value=value+1) sum=sum+value;
- printf("sum to ten %d ",sum);
-
- for(value=1; value<=4; value++)
- switch(value)
- {
- case 1: printf("one ");
- break;
-
- default: printf("def ");
- break;
-
- case 4: printf("four ");
- case 2: printf("even ");
- break;
- }
-
- value=3;
-
- lopp:
- printf("%d",value--);
- if(value) goto lopp;
- printf("one " "two " "three");
-
- for(value=5; value>0; value--)
- {
- if(value%2==0)
- continue;
- printf("%d",value);
- }
-
- value=1; putchar('\n');
-
- while(value<100)
- {
- value=value+1;
- test=2;
- flag=0;
-
- while(test<value)
- { if(value%test==0) flag=1; test=test+1; }
- if(flag==0) { printnum(value); putchar(32); }
- }
- putchar('\n');
-
- printf("\"\'");
- printf("%d %d %d ",mama,Mama,Ma_ma);
- printf("%lx %lx %ld %ld %ld ",wopa,woper,wopra,wopa,woper);
- printf("%g %g %g %g\n",doud,pipi,duba,dubb);
-
- ddza=1.2345;
- ddzb=1.1111;
-
- ab.b=1.2345;
-
- printf("%d%d%d%d%d%d ",ddza!=ddzb,ddza==ddzb,ddza<ddzb,
- ddza>ddzb,ddza<=ddzb,ddza>=ddzb);
-
- printf("%g %g ",ddza+ddzb,ddza-ddzb);
- printf("%g %g ",ddza*ddzb,ddza/ddzb);
- mrakx=L'\377'; mraky='ab'; printf("%d %d ",mrakx,mraky);
-
- printf(L"%g ",ab.b);
- printf("%4x %4x ",ab.lx.l1,ab.lx.l2);
- printf("%4x %4x\n",ab.lx.l3,ab.lx.l4);
- }
-
-
- void printnum(int n)
- {
- static char *ptr;
-
- ptr=malloc(10); ptr=ptr+9; *ptr=0;
-
- if(n==0) { putchar('0'); return; }
- if(n<0) { putchar('-'); n= -n; }
-
- while(n>0)
- {
- *--ptr=(n%10)+'0';
- n=n/10;
- }
-
- while(*ptr) putchar(*ptr++);
- }
-
- void subrun(int p1, int p2)
- {
- int i=4;
- printf("sr %d,%d ",p1,p2);
- while(--i) { if(i==2) continue; printf("%d",i); } i=4;
- do { if(i==2) continue; printf("%d",i); } while(--i);
- }
-
- int subtwo(int p1, int p2)
- {
- printf("s2 %d,%d ",p1,p2);
-
- {
- int atint;
- static int stint=6;
-
- printf("*%d",stint);
- stint=7;
- }
-
- return p2;
- }
-
- int subbi()
- {
- return 423;
- }
-
- void arsub()
- {
- char f[12][11];
- static int arint;
- int i,j, b[5][7];
-
- arint=3;
- goto arlab;
- printf("disaster!!\n");
-
- arlab:
- for(i=0; i<5; i++)
- for(j=0; j<7; j++)
- b[i][j]=i+j;
-
- for(i=0; i<5; i++)
- for(j=0; j<7; j++)
- printf("%d ",b[i][j]);
-
- f[3][4]='a'; printf(" %c\n",f[3][4]);
- }
-
- long lfun()
- {
- long lvar;
-
- lvar=(long)54;
- return lvar;
- }
-
- void filefunc()
- {
- char buf[20];
- int fd;
-
- strcpy(buf,"something");
- printf("fd=%d ",fd=create("tfile")); if(fd==-1) exit(0);
- printf("w=%d ",write(fd,buf,9));
- printf("cc=%d ",close(fd));
-
- strcpy(buf,"any thing");
- printf("o=%d ",fd=open("tfile",0));
- read(fd,buf,20);
- printf("cl=%d %s ",close(fd),buf);
- }
-
-
- void scopefun()
- {
- int out1, out2, out3=2;
-
- out1=3; out2=4;
-
- {
- static int out3=7;
- int out4, out1;
-
- out1=4;
-
- {
- int out1,out2;
-
- out1=5; out2=2;
- printf("%d %d %d ",out2,out1,out3);
- }
-
- printf("%d ",out1,out2);
- }
-
- printf("%d %d %d\n",out1,out2,out3);
- }
-