- Feb 28, 2008
- 1,264
- 0
- 71
I am trying to learn C from the second edition of The C Programming Language and on two programs they won't compile, I am using GCC and code::blocks
the first is 1.5.4 Word Counting
#include <stdio.h>
#define IN 1
#define OUT 0
main ()
{
int c,nl,nw,nc,state;
state = OUT;
nl=nw=nc=0;
while ((c=getchar())!=EOF){
++nc;
if (c== '\n')
++nl;
if (c ==' ':: c=='\n'::c=='\t')
state=OUT;
else if(state==OUT){
state=IN;
++nw;
}
}
printf("%d %d %d \n",nl,nw,nc);
}
the compiler tells me that there is an expected ')' before ':' token on line 13
also on 1.6 Arrays
#include<stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite=nother=0;
for (i=0;i<10;++i)
ndigit=0;
while((c=getchar())!EOF)
if(c>='0'&&c<='9')
++ndigit[c-'0'];
else if(c==' ':: c=='\n'::c=='\t')
++nwhite;
else ++nother;
printf ("digits =");
for (i=0; i<10;++i)
printf(" %d",ndigit);
printf(", white space =%d, other=%d\n",nwhite,nother);
}
On line 9 expected')' before '!' token then the same error as before on line 12.
Am I doing something wrong or is my compiler at fault? I have tried retyping these from blank files and still do not get results.
I am very frustrated and even though the code makes sense to me I am not exactly inspired with confidence from these errors.
Code::Blocks is version 12.11
Ia it CB or my compiler and if it is the compiler what can I do about it?
Thanks
the first is 1.5.4 Word Counting
#include <stdio.h>
#define IN 1
#define OUT 0
main ()
{
int c,nl,nw,nc,state;
state = OUT;
nl=nw=nc=0;
while ((c=getchar())!=EOF){
++nc;
if (c== '\n')
++nl;
if (c ==' ':: c=='\n'::c=='\t')
state=OUT;
else if(state==OUT){
state=IN;
++nw;
}
}
printf("%d %d %d \n",nl,nw,nc);
}
the compiler tells me that there is an expected ')' before ':' token on line 13
also on 1.6 Arrays
#include<stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite=nother=0;
for (i=0;i<10;++i)
ndigit=0;
while((c=getchar())!EOF)
if(c>='0'&&c<='9')
++ndigit[c-'0'];
else if(c==' ':: c=='\n'::c=='\t')
++nwhite;
else ++nother;
printf ("digits =");
for (i=0; i<10;++i)
printf(" %d",ndigit);
printf(", white space =%d, other=%d\n",nwhite,nother);
}
On line 9 expected')' before '!' token then the same error as before on line 12.
Am I doing something wrong or is my compiler at fault? I have tried retyping these from blank files and still do not get results.
I am very frustrated and even though the code makes sense to me I am not exactly inspired with confidence from these errors.
Code::Blocks is version 12.11
Ia it CB or my compiler and if it is the compiler what can I do about it?
Thanks
Last edited: