Literals Contents

# Literals

C works by figuring out the meaning or value of some code. This is true for the tiniest pieces of code to the largest programs. The process of finding out the meaning of code is known as evaluation.

The things whose values are the things themselves are known as literals. The literals of C can be categorized by the following types: integers, real numbers, characters, and strings.

## Integers

Integers are numbers without any fractional parts. Examples of integers are:

```    3
-5
0
```

Integers must begin with a digit or a minus sign. The initial minus sign must immediately be followed by a digit.

The integer type in C is `int`. There are other integer types that we will not get to in this book. You can find them by searching the interwebs for "C integral types".

It is important to realize that there are limits to what can be stored in an integer. As mentioned above, C has several different types of integers that are designed for specific situations. For example, if a developer only ever store values in the range of 0 to 100 then that person could use a smaller version of an integer that saves space. In this class, you should always use the default `int` class unless you are told otherwise.

It is recommended that you search the interwebs to determine the maximum (and minimum) value that you can store in an integer in C.

## Real Numbers

Reals are numbers that do have a fractional part (even if that fractional part is zero!). Examples of real numbers are:

```    3.2
4.000
5.
0.3
.3
3.2e-4
3e4
.000000987654321
```

Real numbers must start with a digit or a minus sign or a decimal point. An initial minus sign must immediately be followed by a digit or a decimal point. An initial decimal point must immediately be followed by a digit. C accepts real numbers in scientific notation. For example, 3.3 * 10-11 would be entered as 3.3e-11. The "e" stands for exponent and the 10 is understood, so `e-11` means multiply whatever precedes the `e` by 10-11.

The real number type in C is `double`. This is short for double precision floating point number. There is another real number type called float, which is short for single precision floating point number, but it is not used very much.

As with integers, there are limits to how bit (and small) a number can be stored in a double. It is recommended that you do another interwebs search to learn the limits on real numbers. You might find it useful to compare the limits of a double and a float as that will help you understand why most programmers simply use double.

## Characters

In C, characters generally are single letters, digits, or symbols delineated by single quotes:

```    'a'
'Z'
'&'
'9'
' '
```

The last character in the above list is the space character. There are also a number of two-character characters, some of which are:

```    '\n'
'\t'
'\0'
'\''
'\\'
```

The first symbol in these two-character characters is the backslash; symbols preceded by a backslash are said to have been escaped. Escaping a symbol will often change its meaning. For example, the character n, in a string refers to the letter n while the character sequence `\n` refers to the newline character. A backslash also changes the meaning of the letter t, converting it into a tab character. An escaped zero is the null character which used to terminate strings. To specify the single quote character or a backslash, one needs to escape them with a backslash, otherwise the C compiler will get confused. Are you confused? Don't worry. It'll become natural after a while.

The character type in C is `char`. There is a great debate on how to properly pronounce "char'. Three groups have emerged, those who know how to pronounce "char" properly (these folks say "care" as in "careful") and those who don't (these folks say "char" as in "char" as in "charcoal"). The third group takes a more Southern approach to the pronunciation, (these folks say "car" as in "cartoon"). Who knows who is right?

A character is one byte (8 bits). If you want to see how the various characters (`'a'`, `'\$'`, `'Z'`, etc.) are stored, do a search on the interwebs for ASCII tables.

## Strings

Strings are sequences of characters delineated by double quotation marks:

```    "hello, world!"
"x\nx"
"\"z\""
""
```

Like characters, the symbols in a string can be escaped with the backslash character, You can also escape double quotes with backslashes so that you can include them in a string. A string with no characters between the double quotes is known as an empty string.

The string type in C is `char *`, pronounced "char-star". This is because strings are actually arrays of characters and, as you will learn later, an asterisk can be used to indicate an array. So `char *` can be read as "array of `char`". We will delve into arrays in a later chapter.

## BOOLEANs

Many languages have an additional kind of literal that C does not have, the BOOLEAN literals `True` and `False`. BOOLEANs are used to guide the flow of a program. The term BOOLEAN is derived from the last name of George Boole, who, in his 1854 paper An Investigation of the Laws of Thought, on which are founded the Mathematical Theories of Logic and Probabilities, laid one of the cornerstones of the modern digital computer. The so-called BOOLEAN logic or BOOLEAN algebra is concerned with the rules of combining truth values (i.e., true or false). As we will see, knowledge of such rules will be important for making C programs behave properly. In particular, BOOLEAN expressions will be used to control conditionals and loops.

Even though C has no explicit BOOLEANs, the integers serve double-duty, both as numbers and as BOOLEAN literals. In C, the integer zero serves as `False` and any other integer or integer-like value serves as `True`. Novices sometimes make the mistake that only the number 1 is True. It is `True`, but it is not the only `True`. For example, 2384, -12, and 42 are also `True`.

## Activities

Note: all activities in assume a Unix-style system, either Linux, Mac OSX, or Cygwin on Windows.

 name purpose literals practicing with C literals

lusth@cs.ua.edu

 Literals Contents