我编译时遇到了一些奇怪的错误

时间:2013-03-03 03:06:46

标签: c compiler-errors

这是我第一次使用stackoverflow,所以我希望我这样做。我在其中一个课程中遇到一个程序时遇到了一些麻烦,我已经完成了其他两个课程。这个是问题3,如果你go to this pdf并向下滚动问题3,将会有我用来完成这个程序的信息(但是我必须说它非常简单。)无论如何,我得到了当我编译时,这些非常奇怪的错误,有没有人有任何理由为什么我的代码将无法编译/将无法正常工作?程序的要点是读取样本dna输入并将其与其他2个进行比较。我已经制作了一个示例文件,就像它说in the instructions一样。感谢您的时间!

我得到了什么错误:

     dna.c: In function ‘main’:
dna.c:12: error: expected ‘]’ before ‘;’ token
dna.c:13: error: expected ‘]’ before ‘;’ token
dna.c:14: error: expected ‘]’ before ‘;’ token
dna.c:15: error: expected ‘]’ before ‘;’ token
dna.c:21: error: ‘first’ undeclared (first use in this function)
dna.c:21: error: (Each undeclared identifier is reported only once
dna.c:21: error: for each function it appears in.)
dna.c:22: error: ‘second’ undeclared (first use in this function)   
dna.c:23: error: ‘third’ undeclared (first use in this function)
dna.c:25: error: ‘last’ undeclared (first use in this function)
dna.c: In function ‘read_DNA’:
dna.c:43: error: expected ‘)’ before ‘;’ token
dna.c:43: error: too few arguments to function ‘fgets’
dna.c:45: error: expected expression before ‘;’ token
dna.c: In function ‘print_DNA’:
dna.c:76: error: expected ‘)’ before ‘;’ token

这是我的代码

#include <stdio.h>
#define MAX_IN_LENGTH 241;
#define OUT_LENGTH 60;
FILE *input;
FILE *output;
int read_DNA(char sequence[]);
double compare_DNA(char seq1[], char seq2[], char seq3[], int n);
void print_DNA(char seq1[], char seq2[], char seq3[], int n);

int main()
{
  char first[MAX_IN_LENGTH];
  char second[MAX_IN_LENGTH];
  char third[MAX_IN_LENGTH];
  int last[MAX_IN_LENGTH];
  int i;
  int length;
  double percent;
  input = fopen("dna_input.dat", "r");
  output = fopen("dna_output.dat", "w");
  length =read_DNA(first);
  length =read_DNA(second);
  length =read_DNA(third);
  fprintf(output,"Comparison between sequence # 1 and sequence #2:\n");
  percent=compare_DNA(first, second, last,length);
  print_DNA(first, second, last, length);
  fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
  fprintf(output,"Comparison between sequence # 1 and sequence #3:\n");
  print_DNA(first, third, last,length);
  fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
  fprintf(output,"Comparison between sequence # 2 and sequence #3:\n");
  percent=compare_DNA(second,third,last,length);
  print_DNA(second, third, last, length);
  fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
  fclose(input);
  fclose(output);
  return 0;
}


int read_DNA(char sequence[])
{
  fgets(sequence, MAX_IN_LENGTH, input);
  int i;
  for(i=0; i<MAX_IN_LENGTH; i++)
  {
    if(sequence[i]=='\0')
      return i;

  }
  return MAX_IN_LENGTH;
}

double compare_DNA(char seq1[], char seq2[], char seq3[], int n)
{
  int i,count=0;
  for(i=0;i<n-1;i++)
  {  
    if(seq1[i]==seq2[i])
    {
      seq3[i]=seq1[i];
       count++;
    }
    else
      seq3[i]=' ';
  }
  return (double)count/(n-1);
}


void print_DNA(char seq1[], char seq2[], char seq3[], int n)
{
  int i=0;
  int lines, j, start, stop;
  lines =n/OUT_LENGTH;
  if( n%OUT_LENGTH!=0)
    lines++;
  for(j=0;j<lines;j++)
  {
    start=j*OUT_LENGTH;
    stop=(j+1)*OUT_LENGTH;
    if(stop>=n)
      stop=n-1;
    for(i=start;i<stop;i++)
      fprintf(output,"%c",seq1[i]);
    fprintf(output,"\n");
    for(i=start;i<stop;i++)    
    {
      fprintf(output,"%c",seq3[i]);
      if(seq3[i]==' ')
        fprintf(output,"");
    }
    fprintf(output,"\n");   
    for(i=start;i<stop;i++)   
    {
      fprintf(output,"%c",seq2[i]);}
    fprintf(output,"\n");
  }  
}

2 个答案:

答案 0 :(得分:4)

请勿在您的#define语句中添加分号。

答案 1 :(得分:3)

因为#define 预处理程序指令 而且它的工作方式如下

#define foo bar将所有foo替换为bar,前提是foo没有引号(如"foo"之类的函数printf没有被替换)。 换行符 字符分隔两个预处理程序指令, NOT 分号。

很自然你的分号也被替换了,所以,你的数组声明现在变成了

char first [241;];由于编译器首先看到分号,因此假定您通过不关闭方括号而产生错误。

此错误未声明您的first, second, third变量,因此会出现错误。

OUT_LENGTH

的情况也是如此