Lesson 65: Data types
The right box for each value
- A data type says what kind of value a variable holds.
- Choose the data type from the value: a whole number, a number with decimals, one character, some text, or true/false.
| Exam data type | C++ on the Arduino | Example |
|---|---|---|
| INTEGER | int |
48 |
| REAL | float |
21.5 |
| CHAR | char |
'C' |
| STRING | String |
"Ningbo" |
| BOOLEAN | bool |
true |
- One character goes in single quotes: 'C'. Text goes in double quotes: "Ningbo".
Whole-number division
| Code | Result |
|---|---|
25 / 3 |
8 (both are whole numbers) |
25 / 3.0 |
8.33 |
25 % 3 |
1 (the remainder, MOD) |
In the exam
- 0478/21/O/N/23 Q3: link each description to the most appropriate data type. Mark scheme: a whole number: INTEGER; a single letter: CHAR; a word or phrase: STRING; a number with two decimal places: REAL. (BOOLEAN is not used.)
- Database questions often ask for the data type of each field, for example a price is REAL and "in stock: yes or no" is BOOLEAN.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
A weather station keeps a record. Every variable is an int, so the Serial Monitor shows Temperature: 21 и Unit: 67. Give each variable the best data type: float for a number with decimals, char for one character, bool for true or false. The Monitor should then show Temperature: 21.50 и Unit: C.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Type three marks, one per line; the program prints their average. With 7, 8 and 10 it prints Average: 8, but the real average is 8.33. Make average a float, and divide by 3.0 so that C++ does not use whole-number division.
Нажмите Запустить, чтобы увидеть результат здесь.