This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

can constants defined in a file be used in other file without defining ?

i have defined

#define key0  0x35 
in main file and now i want to use key0 in one of included file in this project
but on compilation i get error for this.
Does keil have any provision where i can use defined constants
of one file in other file without redefining them ?

Parents
  • Create your own header file, for example "keys.h"
    and include it in any file which use
    defines of the header file.

    /* header file */
    #if !defined(KEYS_H_HDR)
    #define KEYS_H_HDR

    #define key0 0x35
    ...

    #endif

    /* in source file use (for example) ... */
    #include "keys.h"

    Vaclav

Reply
  • Create your own header file, for example "keys.h"
    and include it in any file which use
    defines of the header file.

    /* header file */
    #if !defined(KEYS_H_HDR)
    #define KEYS_H_HDR

    #define key0 0x35
    ...

    #endif

    /* in source file use (for example) ... */
    #include "keys.h"

    Vaclav

Children
  • Thanks Vaclav & Andrew
    I am new to programming in C,book to which i had reffered did not carry detail about
    this.

    Is it neccessary that header file created
    by us should be kept in "inc" folder.

    doubt!! --> KEYS_H_HDR
    (#ifndef KEYS_H_HDR
    #define KEYS_H_HDR )
    Does this should be same as header file name ?

  • "book to which i had reffered did not carry detail about this."

    In that case, it does not qualify as a "good" book on 'C' - this is fundamental stuff.

    "Is it neccessary that header file created by us should be kept in "inc" folder."

    No.
    As with any application trying to find a file, there are certain default locations where the compiler will look, and a certain order in which it will try the alternatives - or you can explicitly state the file's location by includinging a full or relative path.
    You need to read the Compiler Manual for details. For C51, see the INCDIR directive.

  • Arjun asked:
    > doubt!! --> KEYS_H_HDR
    > (#ifndef KEYS_H_HDR
    > #define KEYS_H_HDR )
    > Does this should be same as header file > name ?

    The label name is arbitrary, but it keeps things neat to make it the same as the header filename.
    This is a very common technique to avoid "multiple definition" errors if the same header file is referred to twice. (The first reference will "define" KEYS_H_HDR, so a second reference will "comment out" the entire contents of the header file.

    If your C textbook does not refer to header files, then get another one. This is a fundamental part of the C language.