# Constants

Be sure you understand the difference between a 'constant' and a declaration. A constant has a value that cannot be changed. For example:
```
1234
'x'
9.89
"String"

```

Constants are used to assign a value to a variable. E.G

```	int i;		/* declare a variable called 'i'	*/
i=1234;		/* assign the constant value 1234 to
* the variable 'i'			*/
i++;		/* Change the value of the variable.	*/
```

## Integer constants.

Interger constants can be expressed in the following ways.

```	1234	(decimal)
0xff	(Hexidecimal)
0100	(Octal)
'\xf'	(Hex character)
```

Examples of their use are:

```	int i=255;	/* i assigned the decimal value of 255	*/

i-=0xff		/* subtract 255 from i			*/

i+=010		/* Add Octal 10 (decimal 8)		*/

/* Print 15 - there are easier ways...	*/
printf ("%i \n", '\xf');

```

Integer constants are assumed to have a datatype of int, if it will not fit into an 'int' the compiler will assume the constant is a long. You may also force the compiler to use 'long' by putting an 'L' on the end of the integer constant.

```        1234L           /* Long int constant (4 bytes)          */
```

The other modifier is 'U' for Unsigned.

```        1234U           /* Unsigned int                         */
```

and to complete the picture you can specify 'UL'

```        1234UL          /* Unsigned long int                    */
```

## Floating point constants.

Floating point constants contain a decimal point or exponent. By default they are double.

```	123.4
1e-2

```

## Chararacter constants.

Are actually integers.

```	'x'
'\000'
'\xhh'

escape sequences

```

## String constants.

Strings do not have a datatype of their own. They are actually a sequence of char items terminated with a \0. A string can be accessed with a char pointer.

An example of a string would be:

```
char *Str = "String Constant";

```

See the discussion on strings for more information.

## Also see:

• #define Tells the compiler to replace all occurances of a name with a value computed at compile time.
• enum A define with scope and int type, which retains it's name after compile (usable for debugging), but can only be int
• Strings Arrays of chars can be defined as constants.
• const Flaggs a variable declaration as unchangable. But still takes up space as a variable.

 Top Master Index Keywords Functions

See also:

• https://www.quora.com/Should-I-use-define-or-enum-for-device-registers Includes an interesting way of using enum or struct to define registers from base address.
```// uart.h
struct UartRegisters {
uint32_t CTRL;
uint32_t STAT;
uint32_t TX;
uint32_t RX;
};

#define UART_CTRL_EN 0x01

#define UART0_BASE 0x80000000
#define UART1_BASE 0x80001000

// usage:
// main.c
struct UartRegisters *UartInst0;

int main(void)
{
UartInst0 = (struct UartRegisters*) USRT0_BASE;

UartInst0->CTRL |= UART_CTRL_EN;
// ...
}

```
• http://ctips.pbworks.com/w/page/7277580/BinaryConstants macros that allow binary constants!
```
/* turn a numeric literal into a hex constant
(avoids problems with leading zeroes)
8-bit constants max value 0x11111111, always fits in unsigned long
*/
#define HEX__(n) 0x##n##LU

/* 8-bit conversion function */
#define B8__(x) ((x&0x0000000FLU)?1:0) \
+((x&0x000000F0LU)?2:0) \
+((x&0x00000F00LU)?4:0) \
+((x&0x0000F000LU)?8:0) \
+((x&0x000F0000LU)?16:0) \
+((x&0x00F00000LU)?32:0) \
+((x&0x0F000000LU)?64:0) \
+((x&0xF0000000LU)?128:0)

/* *** user macros *** /

/* for upto 8-bit binary constants */
#define B8(d) ((unsigned char)B8__(HEX__(d)))

```
• http://embeddedgurus.com/barr-code/2011/08/dont-follow-these-5-dangerous-coding-standard-rules/

Martin Leslie