How unit 1 is examined
This unit covers the C/C++ building blocks: character set, constants, variables, keywords, identifiers, data types, storage classes, conversions and operator precedence; the only past questions are the Jun 2020 7-mark "constants, variables, identifiers, literals" and "type conversions".
Character set
Not asked since 2022
Definition. The character set is the group of valid letters, digits and symbols that a C++ program may use to build tokens.
Key points.
- Letters are the uppercase A-Z and lowercase a-z, and C++ treats the two cases as different characters.
- Digits are 0 to 9, used to form numeric constants and parts of identifiers.
- Special symbols include
+ - * / % = < > ( ) { } [ ] ; , . # & ! _and quotes. - White space (blank, tab, newline) separates tokens and is ignored by the compiler except inside strings.
Constants
Not asked since 2022
Definition. A constant is a fixed value that does not change during execution of the program.
Key points.
- Integer constants have no decimal point, for example
25,-7,0x1F(hex) and017(octal). - Floating constants have a decimal point or exponent, for example
3.14and2.5e3. - Character constants are one symbol in single quotes, such as
'A', and string constants are in double quotes, such as"RGPV". - A named constant is made with
const float PI = 3.14;, and any later attempt to change it is a compile error.
Asked: [7 marks] (Jun 2020) Explain the term constants, variables, identifiers and literals.
Variables
Not asked since 2022
Definition. A variable is a named memory location whose value can change while the program runs.
Key points.
- A variable must be declared with its type before use, in the form
type name;such asint age;. - It may be initialised at declaration, for example
float pi = 3.14;. - The type decides how much memory is reserved and which values are allowed.
- Declaring two variables of the same name in one scope is an error.
Keywords
Not asked since 2022
Definition. Keywords are reserved words with a fixed meaning to the compiler, and they cannot be used as identifiers.
Key points.
- Examples are
int,float,char,if,else,while,for,class,return,staticandconst. - They are written in lowercase, so
Intis legal whileintis not usable as a name. - C++ has about 95 keywords, more than C, including
class,public,private,virtualandnew.
Identifiers and literals
Not asked since 2022
Definition. An identifier is the programmer-chosen name of a variable, function, class or array; a literal is a fixed value written directly in the source code.
Key points.
- An identifier may contain letters, digits and underscore, and it must not begin with a digit.
- It cannot contain spaces or symbols and cannot be a keyword.
- It is case sensitive, so
Totalandtotalare different names. - Kinds of literal are integer (
10), floating (1.5), character ('x'), string ("hi") and boolean (true).
Type declaration instruction
Not asked since 2022
Definition. A type declaration instruction tells the compiler the data type of a variable before it is used.
Key points.
- The form is
type var1, var2 = value;, for exampleint a, b = 5;. - Declarations tell the compiler how much memory to reserve for each variable.
- A variable that is declared but not initialised holds a garbage value if it is a local variable.
- In C the declarations come at the start of a block, while C++ allows them anywhere before use.
Integer, long and short types
Not asked since 2022
Definition. Integer types store whole numbers, and the qualifiers short and long change the size and range.
Key points.
- Typical sizes on a 32-bit or 64-bit compiler are
short2 bytes,int4 bytes,long4 or 8 bytes andlong long8 bytes. - An
intoccupies 4 bytes, so its signed range is -2147483648 to 2147483647. shortsaves memory for small values, whilelongholds larger values.- Real numbers use
float(4 bytes) anddouble(8 bytes), andsizeof(type)gives the exact size.
Signed and unsigned
Not asked since 2022
Definition. A signed type can hold negative and positive values, while an unsigned type holds only zero and positive values.
Key points.
- Signed is the default for
int, and one bit stores the sign. - With $n$ bits the signed range is $-2^{n-1}$ to $2^{n-1}-1$ and the unsigned range is $0$ to $2^n-1$.
- For 16 bits, signed is -32768 to 32767 and unsigned is 0 to 65535.
- Assigning a value out of range wraps around, so
unsigned char u = 300;stores 44.
Storage classes
Not asked since 2022
Definition. A storage class decides the scope, lifetime, default value and storage place of a variable.
Key points.
autois the default for local variables, which live in the stack, have block scope and start with a garbage value.registerrequests that the variable be kept in a CPU register for speed, and its address cannot be taken.statickeeps a local variable alive for the whole program with default value 0, and it is initialised only once.externdeclares a global variable defined in another file, which has program-wide scope and default value 0.
| Class | Storage | Default | Scope | Lifetime |
|---|---|---|---|---|
| auto | Stack | Garbage | Block | Block |
| register | CPU register | Garbage | Block | Block |
| static | Data area | 0 | Block | Program |
| extern | Data area | 0 | Global | Program |
Integer and float conversions
Low weight
Definition. Type conversion is the change of a value from one data type to another, either automatically by the compiler (implicit) or by the programmer (explicit, casting).
Key points.
- Implicit conversion promotes the smaller type to the larger one in an expression (
chartointtofloattodouble), so'A' + 1is 66. - Explicit conversion is written
(float)iorfloat(i), so(float)7/2is 3.5 while7/2is 3. - Class-type conversion is done by a constructor (basic to class) or a conversion operator function
operator float()(class to basic); class to class uses either of them.
class Meter { float m; public:
Meter(float x = 0) : m(x) {} // basic to class
operator float() { return m; } }; // class to basic
int main() { int i = 7; float a = i; // implicit
float b = (float)i / 2; // explicit: 3.5
Meter d = 2.5f; float x = d; } // x = 2.5
Answer frame. Open with the definition of type conversion; then implicit with program, explicit with program, then the three class conversions; close with the note that a narrowing conversion may lose data.
Asked: [7 marks] (Jun 2020) Explain with programs the various types of type conversions.
Type conversion in assignment
Not asked since 2022
Definition. In an assignment the right-hand value is converted to the type of the left-hand variable.
Key points.
- Assigning a float to an int drops the fractional part, so
int a = 3.9;stores 3. - Assigning an int to a float stores it with a decimal part, so
float f = 7;stores 7.0. - Assigning a larger integer type to a smaller one may lose the high bits, so
short s = 40000;stores -25536. - In
float f = 7/2;integer division happens first, so f is 3.0, not 3.5.
Hierarchy of operations
Not asked since 2022
Definition. Hierarchy (precedence) decides which operator is evaluated first, and associativity decides the order among operators of equal priority.
Key points.
- Order from high to low is:
(), unary- ! ++ --, then* / %, then+ -, then relational, then&&, then||, then=. * / %and+ -associate left to right, and assignment associates right to left.- Example:
2 + 3 * 4 - 6 / 2 % 2= 2 + 12 - ((6/2)%2) = 2 + 12 - 1 = 13. - Parentheses override the default order.
Last-minute revision
- A constant never changes, a variable is a named changeable memory location, and an identifier is a programmer-chosen name.
- Identifiers start with a letter or underscore, are case sensitive and cannot be keywords.
- Literals are integer, floating, character, string and boolean values written in code.
- Sizes: short 2, int 4, float 4, double 8 bytes;
intrange is -2147483648 to 2147483647. - Signed range $-2^{n-1}$ to $2^{n-1}-1$, unsigned $0$ to $2^n-1$.
unsigned char u = 300;stores 44.- Storage classes are auto, register, static, extern; static and extern default to 0.
7/2is 3 but(float)7/2is 3.5.- Class conversion uses a constructor (basic to class) and
operator type()(class to basic). 2 + 3 * 4 - 6 / 2 % 2is 13.
Memory hooks
- CVIL: Constant, Variable, Identifier, Literal, the four terms of the 7-mark question.
- ARSE: storage classes Auto, Register, Static, Extern.
- Promote up, truncate down: small to large is safe, large to small loses data.
- BODMAS-like: brackets, unary, multiply/divide/mod, add/subtract, relational, logical, assign.
Coverage checklist
- Character set: covered.
- Constants: covered; Jun 2020 constants, variables, identifiers, literals question.
- Variables: covered; same Jun 2020 question.
- keywords: covered.
- identifiers literals: covered; same Jun 2020 question.
- Instructions: Type Declaration Instruction: covered.
- arithmetic Integer Long Short: covered.
- Signed unsigned: covered.
- storage classes: covered.
- Integer and Float Conversions: covered; Jun 2020 type conversions question.
- type conversion in assignment: covered.
- hierarchy of operations: covered.