Thread: #define for constants?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    #define for constants?

    I have a bunch of constants:

    Code:
    const int SCREEN_WIDTH = 500;
    const int SCREEN_HEIGHT = 500;
    
    const int MAZE_WIDTH = 21;
    const int MAZE_HEIGHT = 21;
    
    const int TILE_SIZE = 50;

    Which are required by a couple of my source files.
    How do I share them with my source files?

    Do I make a headerfile with #define statements eg. #define SCREEN_WIDTH 500

    Or do I make a headerfile with them defined as static variables?

    Or do I make a headerfile with them declared as extern variables and define them in another source file?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In, C you define them in a header file and include that header where they are needed.

    NOTE: const int does NOT work in all places in C; you need to define them for some places to compile.

    One place the "const int" does not work well is array dimensions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    You DECLARE extern global variables in the header file.
    You DEFINE the same variables and initialize the const variables in One .c file, and then use them in any .c file that #includes the header file.

    Variables are usually given lower case names. "tile_size" rather than "TILE_SIZE". #defines are traditionally all upper case names.

    We try to avoid global variables, and define them as local in main() or some other function and pass them to a function called.

    Yes, you could use #defines in the header file instead of const variables:
    Code:
    #define TILE_SIZE  50
    // ...
    Please look at the following:

    foo.h
    Code:
    #ifndef FOO_H
    #define FOO_H
    
    // extern DECLARATION only, defined in a .c file
    
    extern const int SCREEN_WIDTH;
    extern const int SCREEN_HEIGHT;
    
    extern const int MAZE_WIDTH;
    extern const int MAZE_HEIGHT;
    
    extern const int TILE_SIZE;
    
    // extern function declarations/prototypes
    
    #endif // FOO_H
    foo.c
    Code:
    #include <stdio.h>
    #include "foo.h"
    
    // Const variable DEFINITIONS
    
    const int SCREEN_WIDTH = 500;
    const int SCREEN_HEIGHT = 500;
    
    const int MAZE_WIDTH = 21;
    const int MAZE_HEIGHT = 21;
    
    const int TILE_SIZE = 50;
    
    int main(void)
    {
       printf("TILE_SIZE: %d\n", TILE_SIZE);
    
       // ...
    
       return 0;
    }
    Last edited by rstanley; 08-12-2021 at 01:54 PM.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    A subtle difference is that when you use a #define the value is 'baked' into your machine code as a constant, and as such the compiler can optimize much better.

    With an "external const int" it is an single actual value stored somewhere in actual memory, will be loaded from memory. In source file that doesn't have access to the constant value the compiler can make no assumptions of what the value could be and has to optimize for the general case.

    So:

    Code:
    // in the .h file
    xxtern constant SCREEN_WIDTH;
    
    // in the .c file
    int foo(int a) {
       return a*SCREEN_WIDTH;
    }
    vs

    Code:
    // in the .h file
    #define SCREEN_WIDTH 4
    
    // in the .c file
    int foo(int a) {
       return a*SCREEN_WIDTH;
    }
    The resulting machine code will be a lot cleaner/faster with #define, as it will not need to access memory can optimize the multiply operation.

    I'm not saying that "#define" is better than "const int" (it isn't), but each definitely has uses and maybe a few seconds should be spent thinking which is best for your use-case.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Since #define doesn't allocate any memory as const int does, why don't we use #define for all our global constants?

    When to and when not use #define?

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Variables are usually given lower case names. "tile_size" rather than "TILE_SIZE". #defines are traditionally all upper case names.
    global constants too?

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    Quote Originally Posted by Nwb View Post
    Since #define doesn't allocate any memory as const int does, why don't we use #define for all our global constants?

    When to and when not use #define?
    For integer constants you will nearly always have to use a #define for a constant that is involved in calculating the size of a structure or array, because the size of a structure needs to be calculated at compile time.

    You will nearly always use a #define to replace a 'magic number', so it has a symbolic name. For example NULL, EOF, and error codes like EBADF and ENOMEM.

    Most other times a "const int" or an "enum" is a btter idea.

  8. #8
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by hamster_nz View Post
    A subtle difference is that when you use a #define the value is 'baked' into your machine code as a constant, and as such the compiler can optimize much better.

    With an "external const int" it is an single actual value stored somewhere in actual memory, will be loaded from memory. In source file that doesn't have access to the constant value the compiler can make no assumptions of what the value could be and has to optimize for the general case.

    So:

    Code:
    // in the .h file
    xxtern constant SCREEN_WIDTH;
    
    // in the .c file
    int foo(int a) {
       return a*SCREEN_WIDTH;
    }
    vs

    Code:
    // in the .h file
    #define SCREEN_WIDTH 4
    
    // in the .c file
    int foo(int a) {
       return a*SCREEN_WIDTH;
    }
    The resulting machine code will be a lot cleaner/faster with #define, as it will not need to access memory can optimize the multiply operation.

    I'm not saying that "#define" is better than "const int" (it isn't), but each definitely has uses and maybe a few seconds should be spent thinking which is best for your use-case.
    what can I get away with and what should I not use .h file for because it bad programming habit,unreadable code
    so if I want a library in separate .c file with .h file
    how do I do when I want different versions of constants ,depending on what I use library to,compile time conditionals?
    it has kinda following
    #define WKING unicode number
    #define WQUEEN unicode number
    ...
    but maybe I want to use it with other constants,like ascii "K","Q","Kn","Bi","Rk","pw" for testrun in console mode
    you tell me you can C,why dont you C your own bugs?

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  2. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  3. define constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 03:17 PM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. Constants or #define
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2002, 08:56 AM

Tags for this Thread