Are constants, defined by using the keyword const , external linkage or internal linkage?
Can constants be modified using pointers?
What about constants defined using by #define?

Recommended Answers

All 6 Replies

This is an example.

#define randomize(srand)(time(NULL))
#define random(num)(rand() % (num))

#define max(x,y)((x) > (y) ?(x) : (y))
#define red 12

Are constants, defined by using the keyword const , external linkage or internal linkage?
Can constants be modified using pointers?
What about constants defined using by #define?

Can constants be modified? It really depends on what you mean by modified. Does that imply the use of memory management functions?

const-qualified variables aren't the same as constants.

int const i = 5;

5 is a constant. i is a variable that happens to be const-qualified, which means you can't modify it directly. Constants can never be modified, because at runtime they usually don't exist. const-qualified variables, well, it depends.

const is mostly a way of documenting variables that you intend not to modify. Consider

size_t strlen(const char *s);

The word const here indicates that the function strlen will not try to modify the referent of s (here an array of char).

An important thing to realize about const is that it's applied to variables, not objects. Observe:

int i = 0;
int *p = &i;
int const *q = &i;

Although p and q both refer to the same object, only p can safely be used to modify it. Any attempt to modify *q, which is const, causes undefined behavior (I think -- don't have my C99 at hand).

Furthermore, const-ness is not part of the value of an expression, but merely a property applied (at compile time) to certain names. The purpose of const is to allow the programmer to state his intent not to modify a certain variable within a certain scope, and for the compiler to assume that variable is not being modified, so that optimization can take place.

Your question about linkage is rather puzzling, because linkage is determined by the scope of the declaration and has nothing to do with const-ness.

in C, the keyword const doesn't exist, but it does in C++.

#define is used often in C to declare a name for a value that doesn't ever change throughout the execution of the program. This makes it easier to change values that are used over and over again.

in C, the keyword const doesn't exist, but it does in C++.

#define is used often in C to declare a name for a value that doesn't ever change throughout the execution of the program. This makes it easier to change values that are used over and over again.

Please read this link

http://tigcc.ticalc.org/doc/keywords.html

thanx al..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.