Const vs #define – When do we really use them and why?

Thread Starter

skyr6546

Joined Mar 22, 2019
73
I do not understand main difference between constant and define and I am having difficulty to understand specific use

define keyword
C:
#include <stdio.h>
 
#define x  20

int main(void)
{
   printf("x = %d \n", x);
    
   return 0;
}
constant keyword tell to compiler that value of variable can't be change
C:
#include <stdio.h>
 
int main(void)
{
    int x = 20;
    
   printf("x = %d \n", x);
    
   return 0;
}
somebody can explain When we should really use constant and define keyword in code ?
 

djsfantasi

Joined Apr 11, 2010
9,156
There’s been a lot of discussion on that very question. I've seen arguments for both usage. When making your choice remember that #define is an alias and can be used for more than values.
 

WBahn

Joined Mar 31, 2012
29,976
The #define is essentially text search and replace within your code before the compiler sees it.

The const keyword tells the compiler to look for attempts to change the value in a variable. But there are many kinds of variables. Consider the function

char* strcpy(char* dest, const char* src);

In this declaration we are telling the compiler that the char* that src points to is a constant, meaning that it isn't to be changed. That makes sense since we don't want our string copy function to change the source string. Any attempt to do so will be caught by the compiler and result in an error.

There's no way that a #define macro could accomplish this for us.

These are to HELP the compiler keep us honest. There are ways around them if we really want to do so, but that's on us.
 

MrAl

Joined Jun 17, 2014
11,389
Hi,

I think the main difference is that #define can change a previously defined constant while an actual constant can not be changed.
Also with #define you can use #ifdef to find out if an object has already been defined or not. You can also use it in a conditional statement something like:
#if defined(Version)
k=2;
#else
k=1;
 

Analog Ground

Joined Apr 24, 2019
460
With all respect, #define and const are completely different. #define is used by the C preprocessor to replace a name or "token" in the source code with some text in the source code. The C preprocessor can also perform tests on the token to alter the source code. The C compiler never sees it. const is used by the C compiler to specify characteristics of a variable and modify how storage is allocated for the variable. Yes, one important thing is telling the compiler the variable will not be changed. However, in the embedded world, it often leads to the compiler and linker putting the variable in non-volatile memory like flash memory (along with program code) and not in RAM. This can be important when there is lots of Flash memory but not so much RAM. I am just not getting why they are compared at all. Again with respect, any equivalency seems superficial. Feel free to flame away.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
Hi,

I think the main difference is that #define can change a previously defined constant while an actual constant can not be changed.
Also with #define you can use #ifdef to find out if an object has already been defined or not. You can also use it in a conditional statement something like:
#if defined(Version)
k=2;
#else
k=1;
I wouldn't call that the main difference at all -- they are completely separate critters that have a small intersection of situations in which either can be used to achieve the same apparent objective. But the majority of the uses of one are not possible using the other.
 

WBahn

Joined Mar 31, 2012
29,976
With all respect, #define and const are completely different. #define is used by the C preprocessor to replace a name or "token" in the source code with some text in the source code. The C preprocessor can also perform tests on the token to alter the source code. The C compiler never sees it. const is used by the C compiler to specify characteristics of a variable and modify how storage is allocated for the variable. Yes, one important thing is telling the compiler the variable will not be changed. However, in the embedded world, it often leads to the compiler and linker putting the variable in non-volatile memory like flash memory (along with program code) and not in RAM. This can be important when there is lots of Flash memory but not so much RAM. I am just not getting why they are compared at all. Again with respect, any equivalency seems superficial. Feel free to flame away.
I completely agree. I think the equivalency discussions are very superficial but reflect the fact that most people at the time that they are likely to ask the question have only been introduced to each of them in the context of the very small intersection of uses that I described in the last post. So it's a reasonable question given that limited exposure. Since a huge fraction of C programmers never move very far beyond that small intersection, it's also not surprising that so many folks think that the two have more in common than they have in contrast.
 

MrAl

Joined Jun 17, 2014
11,389
Hello,

Well i for one was under the impression that the context of the discussion was under the assumption that we were talking about the differences when declaring CONSTANT''s, not just a generally broader scope chat.
 

WBahn

Joined Mar 31, 2012
29,976
Hello,

Well i for one was under the impression that the context of the discussion was under the assumption that we were talking about the differences when declaring CONSTANT''s, not just a generally broader scope chat.
Perhaps the TS can clarify, but it looked to me like they were asking what the main difference between #define and const was. It may well be that they were not yet aware that they are used for far more things than making values constant in programs, but that doesn't mean that they only want to know the difference in that very limited context.
 

Analog Ground

Joined Apr 24, 2019
460
After thinking more about this thread, perhaps one issue is a failure to present "C" as a "tool chain" instead of just a language. This concept is even more important in embedded programming. The issue is more pronounced since IDEs and board support packages hide so much "under the covers" by hiding steps or blending steps together. Off the top of my head, here is the simplest description I can think of for an embedded programming tool chain. There is only a one way flow. Optional or sometimes skipped elements are in brackets, "[ ]". In my head, I see the vague outline of a flow chart showing how all this interacts.

Editor -> [Make] -> Preprocessor - > Compiler -> [Assembler] -> [Optimizer] -> Linker - > [Loader] -> [Debugger] -> Programmer
 

MrAl

Joined Jun 17, 2014
11,389
Perhaps the TS can clarify, but it looked to me like they were asking what the main difference between #define and const was. It may well be that they were not yet aware that they are used for far more things than making values constant in programs, but that doesn't mean that they only want to know the difference in that very limited context.
Yeah i got the context from his two examples. I thought it was obvious that there were other uses too as i pointed out in my reply.
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
@Analog Ground I was trying to understand particular use of const and define.
Take a look at this two examples
Code:
#include<stdio.h>

#define DelayTime  3

int main ()
{
    int i;
   
    for (i = 0; i <DelayTime; i++ )
        printf ("Delay : %d \n", i);
   
    return 0;
}
Delay : 0
Delay : 1
Delay : 2

Code:
#include<stdio.h>

int main ()
{
    int i;
    const int DelayTime = 3;
   
    for (i = 0; i <DelayTime; i++ )
        printf ("Delay : %d \n", i);
   
    return 0;
}
Delay : 0
Delay : 1
Delay : 2

We can achieve delay in two ways I was trying to figure out when we should really use define and when we should really const in programming
 

Analog Ground

Joined Apr 24, 2019
460
@Analog Ground I was trying to understand particular use of const and define.
Take a look at this two examples
Code:
#include<stdio.h>

#define DelayTime  3

int main ()
{
    int i;
  
    for (i = 0; i <DelayTime; i++ )
        printf ("Delay : %d \n", i);
  
    return 0;
}
Delay : 0
Delay : 1
Delay : 2

Code:
#include<stdio.h>

int main ()
{
    int i;
    const int DelayTime = 3;
  
    for (i = 0; i <DelayTime; i++ )
        printf ("Delay : %d \n", i);
  
    return 0;
}
Delay : 0
Delay : 1
Delay : 2

We can achieve delay in two ways I was trying to figure out when we should really use define and when we should really const in programming
Thank you for clarifying your question. In your case, the first example is what you want. The idea is to give a value to the DelayTime parameter somewhere in your program where it is easy to find and easy to change without looking through the whole program. It also gives the value a name which means something instead of just a number. Usually a #define of this type is in the beginning of the program (like you did) or in a "header file" which can hold things like this. Your use is an example of making the program easier to read and easier to change. The "const" keyword is used for other purposes which have been described in other replies. const is very technical and has nothing to do with making the program easier to read and easier to change.
 

MrChips

Joined Oct 2, 2009
30,706
#define is used for text substitution.
Instead of writing
int DaysOfWeek = 7;

use
#define DaysOfWeek 7

For simple projects, put #define statements at the start of the code.
For everything else, put #define statements in a header file, commonly named constant.h.

const is a different usage altogether as already discussed above.
 
Top