I want this header file to be reused for client programmer. First of
all, I divide my C code into Test.h and Test.c. Test.h is the interface.
Test.c is the implementation. I place global variable "a" inside Test.c.
The global variable "a" is hidden from the client programmer to access. I
tried to hide function "show_line( )" inside Test.c. Why did C Compiler
compile function "show_line( )" inside the function "main()". It should
generate an error saying, "show_line( ) is undeclared identifier".
After I complete designing Test.h and Test.c, then Test.h is available
to the client programmer only. The client programmer does not require to
look at Test.c. Take a look at my example code below.
I use Microsoft Visual C++ 2008. I make sure that C++ Compiler compiles
C-style code instead of C++-style code.
/* Test.h */
#ifndef TEST_H
#define TEST_H
int get_a( void ); /* public function */
void look( void ); /* public function */
#endif /* TEST_H */
/* Test.c */
#include <stdio.h>
#include "Test.h"
int a = 5; /* hidden global variable */
int get_a( void ) /* public function */
{
a += 2;
return a;
}
void show_line( void ) /* hidden function */
{
printf( "-->%d\n", a );
}
void look( void ) /* public function */
{
show_line();
}
/* Main.c */
#include <stdio.h>
#include "Test.h"
int main( void )
{
int z;
// a++; /* Compile error C2065: 'a' : undeclared identifier */
/* global variable "a" is hidden as undeclared identifier */
look(); /* print 5 */
z = get_a(); /* add z and 2 */
look(); /* print 7 */
show_line(); /* Compile warning C4013: 'show_line' undefined; assuming
extern returning int */
/* C Compiler should fail to compile to give an error for show_line() */
return 0;
}
--
Yours Truly,
Bryan Parkoff
all, I divide my C code into Test.h and Test.c. Test.h is the interface.
Test.c is the implementation. I place global variable "a" inside Test.c.
The global variable "a" is hidden from the client programmer to access. I
tried to hide function "show_line( )" inside Test.c. Why did C Compiler
compile function "show_line( )" inside the function "main()". It should
generate an error saying, "show_line( ) is undeclared identifier".
After I complete designing Test.h and Test.c, then Test.h is available
to the client programmer only. The client programmer does not require to
look at Test.c. Take a look at my example code below.
I use Microsoft Visual C++ 2008. I make sure that C++ Compiler compiles
C-style code instead of C++-style code.
/* Test.h */
#ifndef TEST_H
#define TEST_H
int get_a( void ); /* public function */
void look( void ); /* public function */
#endif /* TEST_H */
/* Test.c */
#include <stdio.h>
#include "Test.h"
int a = 5; /* hidden global variable */
int get_a( void ) /* public function */
{
a += 2;
return a;
}
void show_line( void ) /* hidden function */
{
printf( "-->%d\n", a );
}
void look( void ) /* public function */
{
show_line();
}
/* Main.c */
#include <stdio.h>
#include "Test.h"
int main( void )
{
int z;
// a++; /* Compile error C2065: 'a' : undeclared identifier */
/* global variable "a" is hidden as undeclared identifier */
look(); /* print 5 */
z = get_a(); /* add z and 2 */
look(); /* print 7 */
show_line(); /* Compile warning C4013: 'show_line' undefined; assuming
extern returning int */
/* C Compiler should fail to compile to give an error for show_line() */
return 0;
}
--
Yours Truly,
Bryan Parkoff
Comment