13

Whats the reason why there are 2 opportunities :

  1. global variables
  2. symbolic constants with #define ?

So I know what #define does but I don't know what the difference in use is. Which situation do I must have thus I'm able to decide myself upon the right opportunitie? What is one of the opportunities able to do what the other one doesn't ? I hope that I could clarify what I mean.

6
  • 1
    Global variables can be accessed and edited from everywhere. #define constants can't be edited, just read. It has to do with thread safety and reentry of functions.
    – shinzou
    May 2, 2015 at 10:00
  • "I know what #define does" Sure? ;-) However, reading this gcc.gnu.org/onlinedocs/cpp might help you get enlightened.
    – alk
    May 2, 2015 at 10:02
  • 1
    A global variable is a variable, you can change it's value unless you make it a const,#define is fixed value. It cannot be changed at all. May 2, 2015 at 10:03
  • @GAURAVVERMA: It's not even necessarily a "value".
    – alk
    May 2, 2015 at 10:03
  • #define is handled by the preprocessor. It is just plain text substitution in a certain sense. In cases of errors where your #define directive is involved the compiler won't be able to figure it out, and you'll end up with "cryptic" error messages. Preprocessor directives have their place, but somtimes it's probably better to just stick to "const globals
    – Beko
    May 2, 2015 at 10:05

4 Answers 4

12

Well, global variables can be edited from everywhere.

Basically, in the low level, a variable is stored in RAM memory and created after launching your application, it always has an address in RAM. Defines are just macros, your compiler will just replace your define names with its values in the compilation step.

#define can't be edited, it's just a macros. And #define is not just about values, you can define almost everything that you want, for example:

// Defining a constant
#define PI 3.14

// Defining an expression
#define MIN(x,y) ((x)<(y))?(x):(y)

// Defining a piece of code
#define STOP_TIMER \
    clock_gettime(CLOCK_REALTIME, &__t1); \
    __lasttime = \
    (double) (__t1.tv_sec - __t0.tv_sec) + \
    (double) (__t1.tv_nsec - __t0.tv_nsec) / 1000000000.0;

And, in most situations, defines are used to set some OS-specific or hardware-specific values. It's a really powerful thing, because it gives you the opportunity to change things dynamically in the compilation step. For example:

// Example with OS
#ifdef __linux__
    #define WELCOME_STRING "welcome to Linux!"
#else
    #define WELCOME_STRING "welcome to Windows!"
#endif

// Example with hardware
#if __x86_64__ || __ppc64__
    #define USING_64BIT
#else
    #define USING_NOT64BIT
#endif
3
  • Therefore the only difference between a constant global variable and a macro is the memory (const global variable is stored in memory and a macro don't), isn't it ?
    – Clock_9
    May 2, 2015 at 10:29
  • @Clock_9 Yeah that's one of the differences, but not the only. You can't store a piece of code in the ordinal variable, and you can't do some system-specific stuffs with variables. May 2, 2015 at 10:41
  • 1
    @Clock_9 As for defines: if you define something like #define PRINT_THE_HELLO_DEFINES printf("hello defines!\n"); and void main(){ PRINT_THE_HELLO_DEFINES }, then your code will be replaced like that: void main(){ printf("hello defines!\n"); }. So, defines are about replacing stuffs. May 2, 2015 at 10:42
9

Consider this small example

#define num 5
int number = 5;

num is a macro and number is a global variable.

One important difference is that num is not stored in the memory, num is just the substitute for 5, but number uses memory.

Also, macro's are preprocessor directives, their values cannot be changed like variables.

So, no doing

num = 6;

later in the code. You will have to use #undef to undefine it and define it again to change the value.

1
  • 1
    Macros can be redefined (after they are undefined using the #undef preprocessor directive).
    – Peter
    May 2, 2015 at 11:12
2

Global variables can be accessed and edited from everywhere. #define constants can't be edited, just read.

Examples:

  • We use #define ERROR 666 to define a programmer pre compile time constant for an error for the whole program.

  • We use a global variable for a count of how many operations a function did and this value can be read by other functions as well.

There's no point to make the error as a global variable since it shouldn't be edited and you can't use the #define x as a counter.

-4

#define is declared on top of the code, it means before the declaration of the class. And it serves as to define (as the name says) a constant, that can be read but not changed.

A global variable can be accessed globally on the code, and at same time changed.

2
  • 6
    There are no classes in C.
    – alk
    May 2, 2015 at 10:10
  • 1
    (Don't know why this comment had been deleted) A pre-processor directive like #define can be put on each line of a file which is going to be fed to the C pre-processor.
    – alk
    May 2, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.