Skip to main content

Chapter 3 - Literals

Literals are data used for representing fixed values or constant variables that cannot be changed or modified. They can be used directly in the code. For example:

const int = 10

The 10 represents the value 10, not a variable named 10.

Integersโ€‹

An integer is known as a numeric literal without a fraction or an exponent. In C programming there are three different types of integer literals:

  • Decimal
    • Eg: 0, -10, 22
  • Octal (base 8 system)
    • Eg: 021, 077, 033
  • Hexadecimal (base 16)
    • Eg: 0x7f, 0x2a, 0x521

Example: change 78 to octal

Divide 78 by 8 until 0

78 รท 8 = 9 R 6

9 รท 8 = 1 R 1

1 รท 8 = 0 R 1

The output will be 116

(Reminder: the remainder from the first division should be in the ones digit)

What the code should look like

void setup(){
Serial.begin(9600);
Serial.println(78, OCT);
}
void loop(){

}

Serial Monitor

116

YOU TRY!

In serial.print() enter a number then a comma and OCT. See what the output is in the serial monitor.

Eg: serial.print(78, OCT); should print out 116

Floating-Point Literalsโ€‹

A floating-point literal isa numeric literal that contains a decimal point. It is usually stored in either a decimal form or an exponent form.

Example:

Valid EntriesInvalid Entires
-22.5123E
0.000342353554

Character Literalsโ€‹

  • Character literals are used to store a single character within a single quote (โ€˜xโ€™). To store multiple characters, you will use a character array instead.
Valid EntriesInvalid Entries
'a''Programming'
'F''Flower'
'3''integer'
',''sign'

String Literalsโ€‹

String literals are similar to character literals, the difference is that string literals are capable of storing multiple characters enclosed in double quotation marks (โ€œbookโ€).

Valid EntriesInvalid Entries
"myAge"'True'
"remainingNumber"'false'
"percentYield"'Keys'

What are Literals used for?โ€‹

Using a literal, you can use the variable name rather than rewriting the value again and again.

Example:

Instead of using 11

void setup(){
pinMode(11, OUTPUT);
}
void loop(){
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
}

Use a declared integer (Eg. int LED = 11)

int LED = 11; //<---Declare an integer
void setup(){
pinMode(LED), OUTPUT);
}
void loop(){
digitalWrite(LED), HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}

Escape Sequencesโ€‹

Some characters/shortcuts that we used cannot be typed in C programming. For example, enter (for a new line), and tab (for indent). Escape sequences are used to replace of these actions.

Escape SequencesCharacters
\fForm Feed
\nNewline
\rReturn
\tHorizontal Tab
\vVertical Tab
\Backslash
\โ€™Single Quotation mark
\โ€Double Quotation mark
\?Question mark
\0Null Character