Basics About C++





In this section, we are going to introduce the fundamentals of a C++ program. These elements are named "lexical elements" or "tokens," and they are used to construct statements, definitions, declarations, and so on, which are used to construct complete programs.

The following are lexical elements:


  1. Tokens and Character Sets:
    • Tokens are the smallest units in a C++ program, such as keywords, identifiers, literals, and punctuators.
    • C++ source code is typically written using the ASCII character set, but it also supports wide characters and Unicode.
  2. Comments:
    • Comments are used to annotate code and provide explanations.
      • In C++, there are two types of comments:
      • Single-line comments, which start with // and continue until the end of the line.
      • Multi-line comments, which start with /* and end with */, allowing comments to span multiple lines.
  3. Identifiers:
    • Identifiers are names given to entities such as variables, functions, classes, etc.
    • They must start with a letter (uppercase or lowercase) or an underscore (_), followed by letters, digits, or underscores.
    • Identifiers are case-sensitive.
  4. Keywords:
    • Keywords are reserved words that have special meanings in C++ and cannot be used as identifiers.
    • Examples of keywords include int, if, else, for, class, return, etc.
  5. Punctuators:Punctuators are symbols used to punctuate C++ code, such as braces {}, parentheses (), commas ,, semicolons ;, etc.
  6. Numeric:
    • Numeric literals represent numerical values in C++.
    • They can be integers, floating-point numbers, or hexadecimal numbers.
    • Examples include 42, 3.14, 0xFF, etc.
  7. Boolean:
    • Boolean literals represent truth values.
    • In C++, true and false are the Boolean literals.
  8. Pointer Literals:
    • Pointers are variables that store memory addresses.
    • There are no pointer literals in C++, but you can initialize pointers with nullptr to represent a null pointer or assign them the address of a variable.
  9. String and Character Literals: String literals represent sequences of characters enclosed in double quotes " ". Character literals represent single characters enclosed in single quotes ' '. Examples include "Hello, World!", 'A', '5', etc.
  10. User-Defined Literals:
    • User-defined literals allow programmers to define custom suffixes for numeric or string literals.
    • They enable creating custom types or performing custom operations on literals. For example, 42s or 3.14_deg.