Keil Logo

GENERAL: AUTOMATIC GENERATION OF LOOKUP TABLES


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.02
  • C251 Version 2.14
  • C51 Version 5.50

QUESTION

I am trying to write some code to automatically generate sin, cos and tan look-up tables for me. In order to test this method am declaring a global variable as:

double x = sqrt(2);

Yet it gives me an error, basically stating that the scalar initializer is not a constant expression. I have seen other C compilers accept this, so how do I do it?

ANSWER

Part 6.5.7 of the 1990 ISO C Standard states that "All the expressions in an initializer for an object that has static storage ... shall be constant expressions."

Part 6.4 of the 1990 ISO C Standard states that "Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators ..." and that "A constant expression can be evaluated during translation rather than runtime ...".

Therefore, if another C compiler allows double x = sqrt(2); when declaring a global variable, it is not behaving with full ANSI compliance.

There are several ways to solve this problem.

You could declare your look-up table as a global array/structure/whatever and in the main function initialize all the values using for loops. Some form of debugging method could then be used to extract the initialized table, or alternatively transmit it out of the serial port to a terminal program where you can "capture" it.

An easier solution is to write a PC DOS C program to do the same thing and output the initialized table to a text file in the form of a C array/structure/whatever declaration. You can then add that file to your C166 project.

Last Reviewed: Wednesday, November 7, 2001


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.