It turns out that the definition just had to be moved outside of the main loop. Once I did this it worked fine.
clock() is not letting me store in a clock_t variable [closed]
-
02-09-2022 - |
题
I am writing some code for a C2000 microcontroller, and I am trying to time certain segments of my code using time.h
. However, when i enter this code:
clock_t begin = clock();
It sends back a bunch of errors. Including one that says: error: function call is not allowed in a constant expression
. But I don't believe it is a constant expression.
Does anyone have an idea what might be going on here?
Further errors include:
--output_all_syms --cdebug_asm_data --preproc_with_compile -- preproc_dependency="Example_2802xAdcTempSensor.pp" "../Example_2802xAdcTempSensor.c" "../Example_2802xAdcTempSensor.c", line 155: error: expected a "}" clock_t start = clock();
"../Example_2802xAdcTempSensor.c", line 155: error: function call is not allowed in a constant expression clock_t start = clock(); ^
Thanks in advance.
解决方案 2
其他提示
Split it into two lines:
clock_t begin;
begin = clock();
The compiler wants the initialization to be a constant expression (per the error message) - which a function call is not.
By splitting it like this you are saying "when the program is running and gets to this point, that is the time I want to evaluate this function". And all will be well.