Literals ๐ชจ#
Literals are values we defined in our source code that are fixed.
Integer Literal#
To create an integer literal, we write the integer like how you would normally write it. The integer literal can have an optional negative sign.
Ex.
10
-10
0
2340
Float Literal#
To create a float literal, we just write the number like how you would normally write it. The float literal can have an optional negative sign and an optional decimal point.
Ex.
10.2340
-10.204
0
2340.43438
Character Literal#
To create a character literal, we can use single-quotes surrounding a single character:
Ex.
'a'
'#'
'&'
'9'
String Literal#
To create a string literal, we can use double-quotes surrounding some text:
Ex.
"The quick brown fox"
"My name is Bob!"
"apples, peaches, grapes"
"101 Dalmatians"
Tip
Youโve already seen the string literal in action, because the string you put into printf()
is a string literal.
printf("This is a string literal being used in printf!");
Tasks ๐ฏ#
Create a string literal for your full name.
Solution โ
"John Smith"
Create a character literal for the first letter of โappleโ.
Solution โ
'a'
Create a float literal for three and fifty-four hundredths.
Solution โ
3.54
Create an integer literal for negative four-hundred and five.
Solution โ
-405