Hi,
I've read that extra braces won't have an effect on compiling code, but when I ran a piece of code through my compiler in Ubuntu with an extra set of braces (it's a step toward completing an example from K&R, 1.17), the program seemed to repeat itself several times over.
I've experimented removing them and adding them and it always has the same effect of making the code run several times. Any idea what could be happening?
EDIT: here is the code
A - Working normally
Code:
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXLINE 100
#define MAXWORD 100
main()
{
int i, x, c, b, nline, nword, state, pass;
int wdcount[MAXWORD];
state = OUT;
i = x = nline = nword = 0;
while ((c = getchar())!= EOF) {
if (c != '\n'){
if (c != '\t' && c != ' ' && state == OUT){
++nword;
state = IN;
}
else if (c != '\t' && c != ' ' && state == IN){
;
}
else if (c == '\t' || c == ' ' && state == IN){
state = OUT;
}
else if (c == '\t' || c == ' ' && state == IN){
;
}
}
else if (c == '\n'){
wdcount[i] = nword;
nword=0;
++i;
state = OUT;
}
}
for (x = 0; x <i;++x){
printf("%d\n", wdcount[x]);
}
printf("Yankee\n");
}
B - Extra Braces before if, at end of printf;
Code:
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXLINE 100
#define MAXWORD 100
main()
{
int i, x, c, b, nline, nword, state, pass;
int wdcount[MAXWORD];
state = OUT;
i = x = nline = nword = 0;
while ((c = getchar())!= EOF) {
{if (c != '\n'){
if (c != '\t' && c != ' ' && state == OUT){
++nword;
state = IN;
}
else if (c != '\t' && c != ' ' && state == IN){
;
}
else if (c == '\t' || c == ' ' && state == IN){
state = OUT;
}
else if (c == '\t' || c == ' ' && state == IN){
;
}
}
else if (c == '\n'){
wdcount[i] = nword;
nword=0;
++i;
state = OUT;
}
}
for (x = 0; x <i;++x){
printf("%d\n", wdcount[x]);
}
printf("Yankee\n");}
}