Problems with basics..

Discussion in 'C Programming' started by iCoder, Mar 24, 2007.

  1. iCoder

    iCoder Guest

    hi All,
    This is my first post.. I am trying to understand how #include
    works... I wrote a simple multi file project like this in C::B.


    ///// MyFunc.h ///////////

    #ifndef MYFUNC_H_INCLUDED
    #define MYFUNC_H_INCLUDED

    int Val;
    void PrintVal();

    #endif // MYFUNC_H_INCLUDED


    ////////////// MyFunc.c ///////////////
    #include <stdio.h>

    #include "myfunc.h"

    void PrintVal()
    {
    printf("%d",Val);
    }



    /////////////////// main.c //////////////////
    #include <stdio.h>
    #include <stdlib.h>

    #include "myfunc.h"

    int main()
    {
    Val = 30;
    PrintVal();
    return 0;
    }



    I "think" #include "xyz.pqr" dumbly replaces the line with contents of
    file xyz.pqr.. Then it compiles and links.
    With this "assumption" there should be a 'int Val' local to both
    MyFunc.c and main.c. So whatever modifications i do for 'Val' should
    not be reflected in MyFunc.c and PrintVal should print garbage. To my
    surprise it prints the value of 'Val' with exact value i have
    modified.
    Also tell me how "extern" works exactly.. For me, in the above
    program, i should declare void PrintVal() as extern i suppose so that
    main.c understands it properly.


    Thanks in advance.
     
    iCoder, Mar 24, 2007
    #1
    1. Advertising

  2. "iCoder" <> schrieb im Newsbeitrag
    news:...
    > hi All,
    > This is my first post.. I am trying to understand how #include
    > works... I wrote a simple multi file project like this in C::B.
    >
    >
    > ///// MyFunc.h ///////////
    >
    > #ifndef MYFUNC_H_INCLUDED
    > #define MYFUNC_H_INCLUDED
    >
    > int Val;
    > void PrintVal();
    >
    > #endif // MYFUNC_H_INCLUDED
    >
    >
    > ////////////// MyFunc.c ///////////////
    > #include <stdio.h>
    >
    > #include "myfunc.h"
    >
    > void PrintVal()
    > {
    > printf("%d",Val);
    > }
    >
    >
    >
    > /////////////////// main.c //////////////////
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #include "myfunc.h"
    >
    > int main()
    > {
    > Val = 30;
    > PrintVal();
    > return 0;
    > }
    >
    >
    >
    > I "think" #include "xyz.pqr" dumbly replaces the line with contents of
    > file xyz.pqr.. Then it compiles and links.
    > With this "assumption" there should be a 'int Val' local to both
    > MyFunc.c and main.c. So whatever modifications i do for 'Val' should

    No, you should have ended up with 2 global ints called Val and your Linker
    should have warned about it.

    > not be reflected in MyFunc.c and PrintVal should print garbage. To my
    > surprise it prints the value of 'Val' with exact value i have
    > modified.
    > Also tell me how "extern" works exactly.. For me, in the above
    > program, i should declare void PrintVal() as extern i suppose so that
    > main.c understands it properly.


    extern in Val; in the header
    int Val; in either of the the c files but not in both.

    You should never ever define a variable in a header, only declare it there
    using that extern specifier (telling the compiler: "there is an int called
    Val somewhere, trust me") and make sure it is defined exaclty once in some c
    file. If it is not defined, the linker will complain, if it is defined more
    than once the linker may warn.

    Bye, Jojo
     
    Joachim Schmitz, Mar 24, 2007
    #2
    1. Advertising

  3. iCoder

    Flash Gordon Guest

    Joachim Schmitz wrote, On 24/03/07 14:04:
    > "iCoder" <> schrieb im Newsbeitrag
    > news:...
    >> hi All,
    >> This is my first post.. I am trying to understand how #include
    >> works... I wrote a simple multi file project like this in C::B.
    >>
    >> ///// MyFunc.h ///////////
    >>
    >> #ifndef MYFUNC_H_INCLUDED
    >> #define MYFUNC_H_INCLUDED
    >>
    >> int Val;
    >> void PrintVal();
    >>
    >> #endif // MYFUNC_H_INCLUDED
    >>
    >> ////////////// MyFunc.c ///////////////
    >> #include <stdio.h>
    >>
    >> #include "myfunc.h"
    >>
    >> void PrintVal()
    >> {
    >> printf("%d",Val);
    >> }
    >>
    >> /////////////////// main.c //////////////////
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> #include "myfunc.h"
    >>
    >> int main()
    >> {
    >> Val = 30;
    >> PrintVal();
    >> return 0;
    >> }
    >>
    >> I "think" #include "xyz.pqr" dumbly replaces the line with contents of
    >> file xyz.pqr.. Then it compiles and links.
    >> With this "assumption" there should be a 'int Val' local to both
    >> MyFunc.c and main.c. So whatever modifications i do for 'Val' should

    > No, you should have ended up with 2 global ints called Val and your Linker
    > should have warned about it.


    There is no requirement for the linker to warn about it since it is
    undefined behaviour. A common extension is for the linker to merge such
    objects into a single object when no initialisation is specified. Some
    systems will produce an error and some can be made to produce an error,
    but not all systems can be made to error on it.

    >> not be reflected in MyFunc.c and PrintVal should print garbage. To my
    >> surprise it prints the value of 'Val' with exact value i have
    >> modified.
    >> Also tell me how "extern" works exactly.. For me, in the above
    >> program, i should declare void PrintVal() as extern i suppose so that
    >> main.c understands it properly.

    >
    > extern in Val; in the header
    > int Val; in either of the the c files but not in both.
    >
    > You should never ever define a variable in a header, only declare it there
    > using that extern specifier (telling the compiler: "there is an int called
    > Val somewhere, trust me") and make sure it is defined exaclty once in some c
    > file.


    This is good advice.

    > If it is not defined, the linker will complain, if it is defined more
    > than once the linker may warn.


    Note the may, there are common systems that do and common systems that
    by default do not.
    --
    Flash Gordon
     
    Flash Gordon, Mar 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Illingworth

    Book on VHDL basics and HDL based design

    Brendan Illingworth, Jan 13, 2006, in forum: VHDL
    Replies:
    2
    Views:
    2,116
    Ralf Hildebrandt
    Jan 13, 2006
  2. Jacob Yang [MSFT]

    Re: Starting basics

    Jacob Yang [MSFT], Oct 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    325
  3. Brian Shannon

    Basics of Javascript

    Brian Shannon, Jun 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    380
    clintonG
    Jun 25, 2004
  4. Simon Harris

    Class Basics (Slightly OT?)

    Simon Harris, May 8, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    376
    Web Team @ Borough of Poole
    May 11, 2005
  5. Andrew Chalk

    Basics of Forma Layout

    Andrew Chalk, Dec 16, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    450
    Joe King
    Dec 17, 2005
Loading...

Share This Page