Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would the initialization value be computed at compile time or runtime?

if i have a function that uses the rand() function as its initialization value, would that value be found when the program compiles, or when the function is run?

say:

int function(int init = rand()){
  return init;
}

if it is found at compile time, how can i get the initialization to be dynamic? i guess i would use NULL as the initialization value, but how would i tell the difference between NULL and init = 0?

like image 200
marg Avatar asked Aug 11 '11 01:08

marg


People also ask

What is compile time and runtime initialization?

CompileTime - The generated code uses compile-time initialization for relations, flow ports, framework data, and all user-defined attributes. RunTime - The generated code uses runtime initialization for relations, flow ports, framework data, and all user-defined attributes.

What is the difference between compile time initialization and runtime initialization in arrays?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is determined at compile time?

Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.

How do you initialize an array at compile time?

Syntax: type array_name[size] = { list_of_values }; //integer array initialization int marks[4]={ 67, 87, 56, 77 }; //float array initialization float area[3]={ 23.4, 6.8, 5.5 }; //character array initialization char name[12]={ 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 'i', 'n', 'k', '\0' };


1 Answers

The value is calculated in runtime.

You can always create a tiny program and check that on practice:

int main() {
    srand( time(NULL) );
    std::cout << function() << std::endl;
}
like image 190
eugene_che Avatar answered Oct 19 '22 13:10

eugene_che