Node:Constant expressions, Previous:const, Up:Constants



Constant expressions

You can declare constant expressions explicitly as a particular type of value, such as a long integer, a float, a character, or a hexadecimal value, with certain typographical conventions. For example, it is possible to declare a value explicitly as a long by placing the letter L after the numeric constant. For example:

#define MY_LONG1 23L;
#define MY_LONG2 236526598L;

Similarly, you can declare a value to be a float by appending the letter F to it. Of course, numeric constants containing a decimal point are automatically considered floats. The following constants are both floating-point numbers:

#define MY_FLOAT1 23F;
#define MY_FLOAT2 23.5001;

You can declare a hexadecimal (base-16) number by prefixing it with 0x; you can declare an octal (base-8) number by prefixing it with 0. For example:

int my_hex_integer = 0xFF;   /* hex FF */
int my_octal_integer = 077;  /* octal 77 */

You can use this sort of notation with strings and character constants too. ASCII character values range from 0 to 255. You can print any character in this range by prefixing a hexadecimal value with \x or an octal value with \. Consider the following code example, which demonstrates how to print the letter A, using either a hexadecimal character code (\x41) or an octal one (\101).

#include <stdio.h>

/* To shorten example, not using argp */
int main ()
{
  printf ("\\x41 hex   = \x41\n");
  printf ("\\101 octal = \101\n");

  return 0;
}

The preceding code prints the following text:

\x41 hex   = A
\101 octal = A

Of course, you can assign a variable declared with the const qualifier (the first kind of "constant" we examined) a constant expression declared with one of the typographical expressions above. For example:

const int my_hex_integer = 0xFF;   /* hex FF */
const int my_octal_integer = 077;  /* octal 77 */