找到你要的答案

Q:What is the difference between global variables and #define in c

Q:什么是在C中定义全局变量和#之间的差异

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.

有2个机会的原因是什么:

  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.

answer1: 回答1:

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

那么,全局变量可以从任何地方编辑。

基本上,在低级别,变量存储在RAM内存中,并在启动应用程序后创建,它总是在RAM中有一个地址。定义只是宏,编译器只会在编译步骤中替换其定义名称及其值。

#定义不能被编辑,它只是一个宏。和#定义不仅仅是价值观,你可以定义几乎一切你想要的,例如:

// 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;

并且,在大多数情况下,定义用于设置一些OS特定的或硬件特定的值。这是一个非常强大的东西,因为它给你机会在编译步骤中动态地改变事物。例如:

// 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
answer2: 回答2:

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.

考虑这个小例子

#define num 5
int number = 5;

Num是一个宏观和数量是一个全局变量。

一个重要的区别是,民不存储在存储器中,数字是5的替代品,但数量使用内存。

同时,宏的预处理器指令,其值不能被改变的变量。

所以,没有做

num = 6;

后来在代码。你将不得不使用# undef取消定义和重新定义的值改变。

answer3: 回答3:

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.

全局变量可以从任何地方访问和编辑。#定义常量不能被编辑,只是读。

实例:

  • 我们用#定义错误666定义一个程序员预编译时常数的误差对整个程序。

  • 我们使用一个全局变量来计算函数的多少个运算,这个值也可以被其他函数读取。

有没有点使误差作为一个全局变量,因为它不可编辑,你可以不使用#定义X为反。

answer4: 回答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.

#定义的代码上宣布,这意味着该类的声明之前。它用来定义(如名字所说)一个常量,它可以被读取但不被改变。

全局变量可以在代码上全局访问,同时更改。

c  global-variables  c-preprocessor