I'm making this program and keeps telling me these errors:
35 expected `;' before ':' token
35 expected primary-expression before ':' token
35 expected `;' before ':' token
17 label `ins' used but not defined

I can't seem to find what the error is, it may only be a simple error or something wrong with my goto syntax, I posted it here, hoping to find advice since this is only the second time I'm trying something with the goto syntax

I made the rest of the code comments since they contain the same errors

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
  FILE *input, *output;
  char a[5000];
  int x=1,y;
  input = fopen("output4.txt","r");
  output = fopen("output5.txt","w");
  while(!feof(input))
  {
    x++;
    fscanf(input,"%c",&a[x]);
    if(a[x]=='\n')
    {
      if(a[0]=='D' && a[1]=='E' && a[2]=='C') goto ins;
      else if(a[0]=='A' && a[1]=='D' && a[2]=='D') goto ins;
      //else if(a[0]=='A' && a[1]=='D' && a[2]=='D' && a[3]=='C')goto ins2;
      else if(a[0]=='A' && a[1]=='N' && a[2]=='L') goto ins;
      else if(a[0]=='X' && a[1]=='R' && a[2]=='L') goto ins;
      else if(a[0]=='M' && a[1]=='O' && a[2]=='V') goto ins;
      //else if(a[0]=='C' && a[1]=='J' && a[2]=='N' && a[3]=='E')goto ins2;
      //else if(a[0]=='A' && a[1]=='C' && a[2]=='A' && a[3]=='L' && a[4]=='L')goto ins3;
      //else if(a[0]=='d' && a[1]=='i' && a[2]=='r' && a[3]=='e' && a[4]=='c' && a[5]=='t')goto byte;
      //else if(a[0]=='#' && a[1]=='i' && a[2]=='m' && a[3]=='m' && a[4]=='e' && a[5]=='d')goto data;
      //else if(a[0]=='a' && a[1]=='d' && a[2]=='d' && a[3]=='r' && a[4]=='1' && a[5]=='1')goto memadd;
      //else if(a[0]=='o' && a[1]=='f' && a[2]=='f' && a[3]=='s' && a[4]=='e' && a[5]=='t')goto rel;
      //else if(a[0]=='b' && a[1]=='i' && a[2]=='t')goto bit;
      //else if(a[0]=='@' && a[1]=='R' && a[2]=='0' || a[2]=='1')goto memadd;
      //else if(a[0]=='R' && a[1]=='1' || a[1]=='6' || a[1]=='0' || a[1]=='5' || a[1]=='4' || a[1]=='3')goto reg;
      //else if(a[0]=='A')goto acc;
      else if(a[x]==10){continue; x=1;}
      x=1;
    }
    goto ins: fprintf(output,"%c%c%c\t\t<ins>\n",a[0],a[1],a[2]); x=0;
    /*goto ins2: 
         fprintf(output,"%c%c%c%c\t\t<ins>\n",a[0],a[1],a[2],a[3]); x=0;
    goto ins2: 
         fprintf(output,"%c%c%c%c%c\t\t<ins>\n",a[0],a[1],a[2],a[3],a[4]); x=0;
    goto byte: 
         fprintf(output,"%c%c%c%c%c%c\t\t<byte>\n",a[0],a[1],a[2],a[3],a[4],a[5]); x=0;
    goto data: 
         fprintf(output,"%c%c%c%c%c%c\t\t<data>\n",a[0],a[1],a[2],a[3],a[4],a[5]); x=0;
    goto addr: 
         fprintf(output,"%c%c%c%c%c%c\t\t<addr>\n",a[0],a[1],a[2],a[3],a[4],a[5]); x=0;
    goto rel: 
         fprintf(output,"%c%c%c%c%c%c\t\t<rel>\n",a[0],a[1],a[2],a[3],a[4],a[5]); x=0;
    goto bit: 
         fprintf(output,"%c%c%c\t\t<bit>\n",a[0],a[1],a[2]); x=0;
    goto memadd: 
         fprintf(output,"%c%c%c\t\t<memadd>\n",a[0],a[1],a[2]); x=0;
    goto reg: 
         fprintf(output,"%c%c\t\t<reg>\n",a[0],a[1]); x=0;
    goto acc: 
         fprintf(output,"%c\t\t<acc>\n",a[0]); x=0;*/
  }
  fclose(input);
  fclose(output);
  return 0;
}

Dani AI

Generated

As pointed out, the compiler errors come from mixing up labels and goto syntax. A label is an identifier followed by a colon (placed where you want execution to continue); a goto jumps to the label name and ends with a semicolon. In your posted code the line that reads like a combined goto and label is illegal — the colon belongs to the label, not to the goto — so the compiler complains about : and then says the label is undefined.

Beyond that immediate syntax error there are several logic and safety problems that will cause bugs even after the goto syntax is fixed:

  • x++ is executed before you store the character, so your indexing is off by one. Start x at 0 and increment after storing, or use a[x++] = ch;.
  • continue; x = 1; will never reset x because code after continue is skipped.
  • while (!feof(input)) plus fscanf is fragile; always check input functions' return values to detect EOF or errors.
  • The fixed-size buffer a[5000] can overflow if you don't guard x.
  • conio.h is nonstandard; prefer standard I/O for portability.

A simpler, safer pattern is to read lines and test the first few characters (no gotos needed). Example:

#include <stdio.h>
#include <string.h>

int main(void) {
    FILE *in = fopen("output4.txt","r");
    FILE *out = fopen("output5.txt","w");
    char line[512];
    if (!in || !out) return 1;
    while (fgets(line, sizeof line, in)) {
        if (strncmp(line, "DEC", 3) == 0 ||
            strncmp(line, "ADD", 3) == 0 ||
            strncmp(line, "ANL", 3) == 0 ||
            strncmp(line, "XRL", 3) == 0 ||
            strncmp(line, "MOV", 3) == 0) {
            fprintf(out, "%.3s\t\t<ins>\n", line);
        }
    }
    fclose(in); fclose(out);
    return 0;
}

For a minimal fix to your compiler errors: put the label (for example ins:) on its own line where you want execution to continue and keep goto ins; (with a semicolon) where you need the jump. After that, fix the index, input-checking, and unreachable code noted above.

Recommended Answers

All 2 Replies

Go to statements go like this:

/* create the label */
    ins:
/* then call the goto */
    goto ins;

Substitute every goto <label>: that you have for <label>: or goto <label>; depending on if is a call or a label.
BTW. What a fine example of why goto shouldn't be used. You have a "messed up" code that doesn't even work, but because there's so many jumps with goto I am not inclined to find out why it doesn't work.

You need the proper input file to make it work, but thanks for what you said. I can't believe I made such an obvious mistake in using the goto statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.