Thread: When we should really use constant and define

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    32

    When we should really use constant and define

    code with define keyword
    Code:
    #include <stdio.h>
    
    #define x  20
    
    
    int main(void)
    {
       printf("x = %d \n", x);
       
       return 0;
    }
    x = 20

    code with constant keyword
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x = 20;
    	
       printf("x = %d \n", x);
       
       return 0;
    }
    x = 20

    I don't see the difference between constant and define keyword in code

    somebody can explain When we should really use constant and define keyword in code ?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,882
    Use constant whenever it works.
    But, use define where it does not work.

    You need to use define when it is an array dimension; that is the first place that constants do not work in C that I run across on a normal daily use.

    Note: There is also enum types.

    EDIT: Your code does *not* use a constant!!!

    Tim S.
    Last edited by stahta01; 11-29-2019 at 12:11 AM.
    "...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
    Join Date
    Nov 2019
    Posts
    32
    Quote Originally Posted by stahta01 View Post
    EDIT: Your code does *not* use a constant!!!

    Tim S.
    My mistake

    Code:
    #include <stdio.h> 
    int main(void)
    {
       const int x = 20;
         
       printf("x = %d \n", x);
        
       return 0;
    }
    Quote Originally Posted by stahta01 View Post
    Use constant whenever it works.
    But, use define where it does not work.
    .
    Thanks Tim S you gave one example of array. I have still doubt I am looking good reason to use constant / define

    My question is which one should we really need ?
    Last edited by skyr6546; 11-29-2019 at 12:26 AM.

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Best rule of thumb: if the job can be done without it, use something else. Most useful thing about a #define is conditional compilation anyway...
    The power of null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character arrays - constant values in a constant expression
    By hewlettuser in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2017, 04:00 AM
  2. Replies: 3
    Last Post: 05-17-2016, 06:35 AM
  3. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM
  4. # define - Table constant
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 04-24-2003, 03:29 PM
  5. #define CONSTANT - how to change it's value?
    By gogo in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 11:34 AM

Tags for this Thread