sarthak
sarthak

Reputation: 794

Compilation Error in C code

I have the following C code, which I am running in 16-bit mode using bcc:

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;
}

When I compile it using the following command:

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

I get the following error:

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

How can I remove these?

Upvotes: 0

Views: 771

Answers (1)

alk
alk

Reputation: 70883

You probably are using a pre C99 compiler. Or you are at least telling your compiler to behave like this be providing the option -ansi, which might make the compiler stick to the C89 standard.

The C89 standard does not allow variable definitions somewhere else but at beginning of a block.

To get around this for the 1st error modify this:

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); 
  ...

to look like this:

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);
  ...

For the 2nd error change:

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

for example like this:

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

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

Upvotes: 4

Related Questions