Click to See Complete Forum and Search --> : Initialization of const int within scope


dude_1967
January 28th, 2003, 07:46 AM
Hello,

Is a fundamental data type with the const qualifier within a function scope initialized at run-time?

I think that it is clearly specified for C++ and that there is, indeed, a run-time initialization of such a variable. However, is this also the case for plain ANSI C?

See the example below written in old-fashioned pure ANSI C. Is n_rand initialized at run-time? Is this specified in the ANSI/ISO specification or is it a language extension? I can not find any mention of this language usage in the ANSI C specification.

Thanks a lot for any help clarifying this issue.
Chris.



#include <stdlib.h>
#include <stdio.h>

static const int rand100(void)
{
// Return a random integer ranging from 0...99
return (int) ((100.0 * (double) rand()) / RAND_MAX);
}

int main(int argc, char* argv[])
{
int i;
for(i = 0; i < 100; i++)
{
// Is this a run-time initialization?
const int n_rand = rand100();

fprintf(stdout, "%i\n", n_rand);
}
return 1;
}

vinodp
January 28th, 2003, 09:21 AM
I think const is recent addition to C std.
& the C std says..

The syntax and semantics of const were adapted from C++;

HTH
Vinod

KevinHall
January 28th, 2003, 10:10 AM
In the code you wrote, n_rand is initialized at run time. The "const" just means that (a) you promise not to modify it's value (through some clever typecasting and assignment) and (b) the compiler can make some optimizations concerning n_rand.

You will notice though that you declare n_rand within a for loop, which means that each time through the for loop it gets intantiated and thus initialized. Most compilers will probably allocate the memory at the beginning of main(), but you are forcing n_rand to be initialized 100 times.

- Kevin

dude_1967
January 28th, 2003, 11:18 AM
Thanks for the comments.
I agree.

By the way, in the languages C++ or C does the combination of qualifiers "register const" make any sense? I would imagine yes if one wanted to hint to the compiler to put a non-modifiable POD-type within a CPU register.

Sincerely, Chris.

:)

AnthonyMai
January 28th, 2003, 04:01 PM
static const int rand100(void)
{
// Return a random integer ranging from 0...99
return (int) ((100.0 * (double) rand()) / RAND_MAX);
}


The const above does NOT mean that the function promises to always return a constant. What it means is that the returned integer is a "const int", i.e., you can't modify it.

Such constness has no practical meaning whatsoever. Even if a function returns a regular, none-const integer. I don't see how you can modify the return value, after the function returns. There is no way to do that.

The only thing you can do with an integer returned from a function is either to immediately make a copy into another variable, or immediately make a copy into a parameter used in another function call. In either usage the returned integer is copied and then right away you lose access to the original return value, and you only have a copy at hand.