Answer SL34

No, the compiler will complain that the identifiers height and width in the source file mainRect.c cannot be found.

The variables height and width have internal linkage and file scope for the file rect.c. main() cannot see them.


/* --- rect.h --- */
void setWidth( int w );
void setHeight( int h );
int getArea();

/* --- rect.c --- */
static int width = 0;
static int height = 0;

void setWidth( int w )
{
  width = w;
}

void setHeight( int h )
{
  height = h;
}

int getArea()
{
  return height*width;
}

/* --- mainRect.c --- */
#include <stdio.h>
#include "rect.h"

void main()
{
  height =  4;
  width = 3;

  printf("Area: %d\n", getArea() );
}

This is like trying to access the private instance variables of an object.



Back to Puzzle Home