Hidden implementation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Parkoff

    Hidden implementation?

    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


  • CBFalconer

    #2
    Re: Hidden implementation?

    Bryan Parkoff wrote:
    >
    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".
    Look up the use of 'static' in declaring file scope identifiers and
    functions.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home .att.net>
    Try the download section.

    ** Posted from http://www.teranews.com **

    Comment

    • bigcaterpillar@gmail.com

      #3
      Re: Hidden implementation?

      a is a global variable .
      you can use "extern int a " in main.c .

      Comment

      • Bryan Parkoff

        #4
        Re: Hidden implementation?

        >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".
        >
        Look up the use of 'static' in declaring file scope identifiers and
        functions.
        Do you suggest? Static keyword should be inside Test.h, but not Test.c.
        This way, you can declare global variable and global function after static
        keyword inside Test.h. Then, you place "b++" inside main() function. The
        "b++" inside main() function will not be able to modify another b variable
        inside Test.h because b variable in both main() function and Test.h have
        separate memory storage. Correct?

        Bryan Parkoff


        Comment

        • Thad Smith

          #5
          Re: Hidden implementation?

          Bryan Parkoff wrote:
          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".
          C doesn't work that way. If you invoke a function that has not been
          declared, a default prototype will be appointed to it. This is not an
          error in C. When the program is linked, the linker looks for a function
          with the specified name in both the specified modules and the libraries
          which you specify. In your case it finds show_line() in test.c.

          If you want to prevent the linker from satisfying the call to show_line(),
          declare show_line() as static, as Chuck suggests. That prevents it being
          called from any other module (unless you explicitly make a pointer to the
          function available externally).

          --
          Thad

          Comment

          • Peter Nilsson

            #6
            Re: Hidden implementation?

            Bryan Parkoff wrote:
            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()".
            I don't know exactly what this means, but see below.
            It should generate an error saying, "show_line( )
            is undeclared identifier".
            You are a victum of implicit function declarations
            and undefined behaviour.
            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.
            [If that's relevant to your quesiton, then don't post
            it to clc.]
            I make sure that C++ Compiler compiles
            C-style code instead of C++-style code.
            If you want to know what a C++ compiler will do
            with the code, ask a C++ group. C is not C++.
            Either you're using a C compiler for C code,
            or you're not.
            /* 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 */
            It isn't hidden. It has external linkage. If you
            want to hide it, use...

            static int a = 5;
            int get_a( void ) /* public function */
            {
            a += 2;
            return a;
            }
            >
            void show_line( void ) /* hidden function */
            Similarly this function has external linkage.

            static showline( 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 */
            But without the corrections above, if you precede this
            line with a declaration like...

            extern int a;

            ....your main _will_ be able to modify a. Hiding the identifier
            does not change the linkage.
            >
            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 */
            There is no declarator, so an implicit one is assumed...

            int showline();

            This doesn't match the definition you have, but it,
            unfortunately, _appears_ to work on your computer.
            /* C Compiler should fail to compile to give an error
            for show_line() */
            Not only is not diagnostic required, a C90 compiler that
            produced an error would be non-conforming.

            A C90 compiler _must_ assume an implicit declaration for
            a named function that has not previously been declared.

            A smart linker may have picked up the difference in types,
            but unfortunately, linkers are not required to be that
            smart.

            A C99 compiler though, would indeed need to issue
            a diagnostic in this case.

            C99 compilers aren't too common, but C90 compilers
            that warn of undeclared function use are common. You
            can even do what I do and technically break
            conformance by making the compiler _insist_ on
            prototypes being in scope for named function calls.
            return 0;
            }
            Look up the differences between scope and linkage.

            --
            Peter

            Comment

            • Flash Gordon

              #7
              Re: Hidden implementation?

              Bryan Parkoff wrote, On 19/05/08 03:12:
              >>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".
              >Look up the use of 'static' in declaring file scope identifiers and
              >functions.
              >
              Do you suggest?
              I suggest looking it up. I suspect you have not done so yet since you
              don't seem to understand it.
              Static keyword should be inside Test.h, but not Test.c.
              Completely backwards.
              This way, you can declare global variable and global function after static
              keyword inside Test.h. Then, you place "b++" inside main() function. The
              "b++" inside main() function will not be able to modify another b variable
              inside Test.h because b variable in both main() function and Test.h have
              separate memory storage. Correct?
              Now read up on what static does and think about what will happen if you
              apply it to variables and functions defined in Test.c
              --
              Flash Gordon

              Comment

              • Chris Dollin

                #8
                Re: Hidden implementation?

                Bryan Parkoff wrote:
                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".
                And then:
                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() */
                The compiler gave you the required diagnostic:
                'show_line' undefined; assuming extern returning int */
                The C standard doesn't distinguish "errors" and "warnings", so that's
                OK.

                The C standard also does not require detection of mismatching definition
                and use/assumption at link time, and doesn't specify the effect.

                Conclusions: (a) don't ignore warnings (b) if possible, tell the compiler
                to "fail" compilations with [the worst] warnings (c) fix all trivial
                warnings and as many as are cost-effective of the others; on a good day,
                that's all of them.

                --
                "See how we measure people / into zero." - October Project, /Be My Hero/

                Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                registered no: 690597 England Berks RG12 1HN

                Comment

                • CBFalconer

                  #9
                  Re: Hidden implementation?

                  Bryan Parkoff wrote:
                  >
                  >>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".
                  >>
                  >Look up the use of 'static' in declaring file scope identifiers and
                  >functions.
                  >
                  Do you suggest? Static keyword should be inside Test.h, but not
                  Test.c. This way, you can declare global variable and global function
                  after static keyword inside Test.h. Then, you place "b++" inside
                  main() function. The "b++" inside main() function will not be able
                  to modify another b variable inside Test.h because b variable in both
                  main() function and Test.h have separate memory storage. Correct?
                  No. test.h is only intended to allow other things to interface to
                  test.c. test.c uses it only o check that it is consistent with the
                  actual code. The static functions and objects in test.c never
                  appear in test.h.

                  Please don't strip attribution lines for any material you quote.
                  Those are the initial lines of the form "Joe wrote:|||". The count
                  of '?' before them, +1, ties them to the appropriate quotes.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.


                  ** Posted from http://www.teranews.com **

                  Comment

                  • pete

                    #10
                    Re: Hidden implementation?

                    Bryan Parkoff wrote:
                    Static keyword should be inside Test.h, but not Test.c
                    in Bizarro World.

                    IFYPFY.

                    --
                    pete

                    Comment

                    • Nick Keighley

                      #11
                      Re: Hidden implementation?

                      On 19 May, 02:37, "bigcaterpil... @gmail.com"
                      <bigcaterpil... @gmail.comwrote :
                      a is a global variable .
                      you can use "extern int a " in main.c .
                      you don't quote any context and what you say doesn't seem
                      relevent to the OP.

                      I'm not sure what you mean but I'm guessing you're wrong.

                      --
                      Nick Keighely

                      Comment

                      Working...