Symbolic constants in C -


Conscious of symbolic constants using macros, mathematical constants and const objects Macros are global in the area and can not be confined to the local area, which is a major disadvantage. Calculation constant can not be used in situations other than integer, the mathematical constant can not be float or displayed in the long run. Construction objects can be local scope, can be displayed in different datatypes. But declares an "int consultant" or "cant ace" in C, the value constant and int buffer [A] is not allowed in C. But C ++ AF buffer [A] is allowed because it is called "Constant A" compiler constant only.

Despite most of these mentioned losses, generally prefer to define the indicative constants as a mathematical constant compared to the truncated objects.

I could not understand the statement given below that the Contents tells the cause of performance penalties. What is the reason for this? Please help me understand ..

The problem with the cut objects is that they can be imposed a performance penalty, which prevents the calculator constant. An object has been declared with

const More accurate, its name is not continuous expression). The keyword const is not "continuous", it means "read only". Then given:

  const int answer = 42; Printf ("Answer is% d \ n", answer);   

The evaluation of the printf call in the principal requires the value of answer before accumulation printf function

But in reality, for any compiler, what you pay for it (even if it is free) will optimize the context of answer , so that printf phone results In the same machine code,

  is printf ("Answer% d \ n", 42);   

(GCC does it with -O1 or better. If you do not specify -O ... The code actually brings the value of the object - but if you do not ask for optimization, then you are saying to the compiler that you do not care much about performance.)

(Actually a Cunning compiler can generate code ("Answer is 42"),

.)

The real difference is Is that no The Answer can not be used in those contexts that require a constant expression. For example, case answer: ... will be legal in C ++, but in C is illegal.

Note that int arr [answer]; In fact, at least in C99, is legal, which allows variable-length arrays. It would be equally legal if you wrote

  const int answer = rand ()% 100 + 1;   

But VLA can only have a period of automatic storage, so they can not be declared in the scope of the file or with the static keyword.

enum move:

  enum {answer = 42; }   

This is a static expression answers , but it is limited to types of values ​​ int (C calculation constants always type Are integer ). Some people can argue that this is the abuse of the enum feature, but I have nothing to worry about.

Therefore may be const int answer = 42; For display penalties for , for #define answer 42 , but in practice this is just going to restrict the references in which you can use it.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -