Thread: shared variables and header files

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    2

    shared variables and header files

    Hello,

    I have been programming in Pascal for the last 35 years an d decided to switch to C/C++. Having created a lot of handy functions I decided to translate them in C as a starter.
    So far I created three files: tools.c, tools.h and test.c. I think they don't need any explanation.

    In Pascal you have booleans so I created the constants TRUE and FALSE:
    Code:
    unsigned char   FALSE = 1,
                         TRUE = 0;
    Knowing that I would need them in other programs I put the above in the header file. And that worked fine.

    A bit later I needed TRUE and FALSE in tools.c and then the problems started:
    - If I left TRUE and FALSE only in tools.h, tools.c cannot find them.
    - If I declare TRUE and FALSE only in tools.c, test.c cannot find them.
    - If I declare them both in tools.c and tools.h, the compiler complains both have been declared twice.
    - The same error if I add '#include "tools.h"' into tools.c instead of the declaration.

    I stumbled on "shared variables" and that "extern" should be used but that didn't help me either (or I did understand things wrong).

    Remark: I could let things work by using 0 and 1 instead of TRUE and FALSE but I will need other shared variables in the future as well so why not asking now using this example.

    Thank you for any help!

    Kind regards, Ruud Baltissen

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,620
    Why not just use stdbool ?

    By the way with your current setup, you should be able to #include tools.h in both tools.c and test.c (don't forget about using include guards).

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,313
    Going by the names, they seem to be constants not variables. We would normally define those with defines:
    Code:
    #define BOOL  unsigned char
    #define FALSE 0
    #define TRUE  1
    or possibly as an enum:
    Code:
    typedef enum {FALSE, TRUE} BOOL;
    Note that we would normally define FALSE as 0 and TRUE as 1.

    And as jim said, in modern C (C99 and later) you can include stdbool.h which defines bool, false, and true, something like:
    Code:
    #define bool  _Bool  // _Bool is a builtin type in C99 and later.
    #define true  1
    #define false 0
    _Bool (or bool if you include stdbool.h) has the magical property of only ever containing 0 or 1. If you assign 0 to it, it has the value 0. If you assign any non-zero value to it, its value is 1. This is not the same as an unsigned char.
    Code:
    #include <stdbool.h>
    bool b = 42;            // b contains 1
    unsigned char c = 42;   // c contains 42
    b *= 2;                 // b still contains 1
    c *= 2;                 // c contains 84
    _Bool is probably stored as an unsigned char under the hood, but the compiler adds extra code to operations on it to ensure that it only contains 0 or 1.
    Sign at the Sichuan Tobacco Project Hope Primary School:
    "Work hard for society. Tobacco can help you become an achiever!"

  4. #4
    Registered User
    Join Date
    Dec 2021
    Posts
    2
    Hello Jim,

    I didn't know about these "include guards", used them and that worked like a charm. Thank you!

    Hello John,

    Used this #define and gave TRUE and FALSE the correct value. Thank you as well!

    Kind regards, Ruud Baltissen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variables in 1 header, accessible to different c files
    By dayalsoap in forum C Programming
    Replies: 3
    Last Post: 07-10-2012, 01:42 PM
  2. pthread and shared variables
    By ojaro in forum C Programming
    Replies: 7
    Last Post: 08-14-2010, 11:38 PM
  3. Shared Variables Question.
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 02:33 PM
  4. Static variables and header files
    By drrngrvy in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2006, 01:27 PM
  5. Variables in header files
    By Overlord in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2006, 06:43 AM

Tags for this Thread