1.7 — Keywords and naming identifiers

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/51

flashcard set

Earn XP

Description and Tags

https://www.learncpp.com/cpp-tutorial/keywords-and-naming-identifiers/

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

52 Terms

1
New cards
  1. How many keywords are reserved in C++ as of C++23?

92

2
New cards
  1. Why can't you use C++ keywords as variable names?

Because they are reserved and have special meaning in the language

3
New cards
  1. Which of the following is NOT a valid identifier in C++?
    a) myVariable
    b) myVariable c) 2myVariable d) myvariable

c) 2myVariable

4
New cards
  1. What does C++ require as the first character of an identifier?

A letter (uppercase or lowercase) or an underscore

5
New cards
  1. Which naming convention is unconventional for variable names in C++?
    a) int value;
    b) int Value;
    c) int myVariableName;
    d) int myvariablename;

b) int Value;

6
New cards
  1. What is a disadvantage of using names like int _count; in your programs?

Identifiers starting with an underscore are typically reserved for OS, library, or compiler use

7
New cards
  1. What does the following variable name likely fail to clarify? int time;

It doesn’t specify the unit (e.g., seconds, minutes, hours)

8
New cards
  1. Which of these is a well-named identifier?
    a) int data;
    b) int x1;
    c) int numApples;
    d) int i;

c) int numApples;

9
New cards
  1. What does the term “camelCase” refer to in identifier naming?

A convention where multi-word names start with a lowercase letter and each subsequent word starts with an uppercase letter (e.g., myVariableName)

10
New cards
  1. What is the key insight mentioned regarding writing vs. reading code?

Code is read more often than it is written, so writing readable code saves time in the long run

11
New cards

Is this conventional, unconventional, or invalid? - int sum {};

Conventional

12
New cards

Is this conventional, unconventional, or invalid? - int _apples {};

Unconventional -- variable names should not start with an underscore.

13
New cards

Is this conventional, unconventional, or invalid? - int VALUE {};

Unconventional -- single word names should be in all lower case.

14
New cards

Is this conventional, unconventional, or invalid? - int my variable name {};

Invalid -- variable names can not contain spaces.

15
New cards

Is this conventional, unconventional, or invalid? - int TotalCustomers {};

Unconventional -- variable names should start with a lower case letter.

16
New cards

Is this conventional, unconventional, or invalid? - int void {};

Invalid -- void is a keyword.

17
New cards

Is this conventional, unconventional, or invalid? - int numFruit {};

Conventional

18
New cards

Is this conventional, unconventional, or invalid? - int 3some {};

Invalid -- variable names can not start with a number.

19
New cards

Is this conventional, unconventional, or invalid? - int meters_of_pipe {};

Conventional

20
New cards

C++ special identifies

override, final, import, and module

21
New cards
  • Name of variable is called

An identifier

22
New cards

Rule 1 of identifier naming

  • The identifier can not be a keyword. Keywords are reserved.

23
New cards

Rule 2 of identifier naming

  • The identifier can only be composed of letters (lower or upper case), numbers, and the underscore character.

  • That means the name can not contain symbols (except the underscore) nor whitespace (spaces or tabs).

24
New cards

Rule 3 of identifier naming

  • The identifier must begin with a letter (lower or upper case) or an underscore. It can not start with a number.

25
New cards

Rule 4 of identifier naming

  • C++ is case sensitive, and thus distinguishes between lower and upper case letters. nvalue is different than nValue is different than NVALUE.

26
New cards

It is conventional in C++ that variable names should begin with?

a lowercase letter.

27
New cards

If the variable name is a single word or acronym?

  • the whole thing should be written in lowercase letters.

28
New cards

Identifier names that start with a capital letter are typically used for

  • user-defined types

29
New cards

If the variable or function name is multi-word, there are two common conventions:

Either use underscores as spaces, or capitalize each word to separate them

30
New cards

When working in an existing program…

use the conventions of that program (even if they don’t conform to modern best practices)

31
New cards

Use modern best practices when…

  • you’re writing new programs.

32
New cards

Avoid naming your identifiers starting with an…

underscore. That’s typically reserved for OS, library, and/or compiler use.

33
New cards

The name of your identifiers should

make clear what the value they are holding means

34
New cards

int ccount

Bad

What does the c before “count” stand for?

35
New cards

int customerCount

Good

Clear what we’re counting

36
New cards

int i

Either

Okay if use is trivial, bad otherwise

37
New cards

int index

Either

Okay if obvious what we’re indexing

38
New cards

int totalScore

Either

Okay if there’s only one thing being scored, otherwise too ambiguous

39
New cards

int _count

Bad

Do not start names with underscore

40
New cards

int count

Either

Okay if obvious what we’re counting

41
New cards

int data

Bad

What kind of data?

42
New cards

int minutesElapsed

Either

Okay if obvious what this is elapsed from

43
New cards

int x1, x2

Either

Okay if use is trivial, bad otherwise

44
New cards

int userinput1, userinput2

Bad

Hard to differentiate between the two due to long name

45
New cards

int numApples

Good

Descriptive

46
New cards

int monstersKilled

Good

Descriptive

47
New cards

Code is read more often than it is written, so…

any time saved while writing the code is time that every reader, including future you, will waste while reading it

48
New cards

For variable declarations, it can be useful to use…

  • a comment to describe what a variable is going to be used for, or to explain anything else that might not be obvious.

49
New cards

An identifier that exists for only a few statements (e.g. in the body of a short function)

  • can have a shorter name.

50
New cards

An identifier that is accessible from anywhere

might benefit from a longer name.

51
New cards

An identifier that represents a non-specific number (e.g. anything the user provides)

  • can have a shorter name.

52
New cards

An identifier that represents a specific value (e.g. the length of an inseam in millimeters)

should have a longer name.