C代码中的编译错误

Compilation Error in C code

我有以下 C 代码,我是 运行 使用 bcc 的 16 位模式:

int div(int num, int den);
int mod(int num, int den);

void readSector(char* buffer, int sector)
{
 int rel_sec = mod(sector,18) + 1;
 int head = div(sector,18);
 head = mod(head,2);
 int track = div(sector,36); 
 interrupt(0x13,0x201,buffer,track*0x100+rel_sec,head*0x100);
}

int div(int num, int den)
{
 int i = 0;
 while(num - den*i >= 0)
   i++;
 i--;
 return i;
}

int mod(int num, int den)
{
 int i = 0;
 while(num -den*i >= 0)
   i++;
 i--;
 int x = num - den*i;
 return x;
}

当我使用以下命令编译它时:

bcc -ansi -c -o readSector.o readSector.c

我收到以下错误:

readSector.c:9.4: error: bad expression
readSector.c:9.10: error: need ';'
readSector.c:9.12: error: track undeclared
readSector.c:28.4: error: bad expression
readSector.c:28.6: error: need ';'
readSector.c:28.8: error: x undeclared

我怎样才能删除这些?

您可能正在使用 C99 之前的编译器。或者你至少告诉你的编译器像这样提供选项 -ansi,这可能会使编译器坚持 C89 标准。

C89 标准不允许在除块开头以外的其他地方定义变量。

要解决第一个错误,请修改:

void readSector(char* buffer, int sector)
{
  int rel_sec = mod(sector,18) + 1;
  int head = div(sector,18);
  head = mod(head,2);
  int track = div(sector,36); 
  ...

看起来像这样:

void readSector(char* buffer, int sector)
{
  int rel_sec = mod(sector,18) + 1;
  int head = div(sector,18);
  int track = div(sector,36); 
  head = mod(head,2);
  ...

对于第二次错误更改:

int mod(int num, int den) 
{
  int i = 0;
  while(num -den*i >= 0)
    i++;
  i--;
  int x = num - den*i;
  return x;
}

例如这样的:

int mod(int num, int den) 
{
  int i = 0;
  while(num -den*i >= 0)
    i++;
  i--;

  {
    int x = num - den*i;
    return x;
  }
}