Character Set in C++

  • A character set in C++ is a set of valid characters (mainly including data types and operators in C++) that the C++ language/compiler can recognize during processing time.
  • The character set of C++ consists of letters, digits, and special characters.
  • There are 62 letters and digits character set in C++ (26 Capital Letters + 26 Small Letters + 10 Digits). There are 29, punctuation and special character sets in C++, and are used for various purposes during programming. The C++ has the following character set: –
    Letters (Alphabets)
           A- – -Z, a- – -z
    Digits
           0- – -9
    Special Characters
            +,  -,  *,  /,  ^,  \,  ( ),  [ ],  { }, =,  !, <, >, . , „ , ” , $,  ; , : , % , &, ?, _ , # , <=, >=, @ etc.

White Space Characters

  • A character that is used to produce blank space in the program or when printed in C++ is called white space character.
  • They are non-printable characters.
  • Examples are – spaces, tabs, new lines, and comments.

Escape Sequence

For Details About the Escape Sequence Click this Link

Tokens

  • A token is a group of characters that logically combine together.
  • The programmer can write a program by using these tokens.
  • C++ has the following types of tokens : – 
    • Keywords
    • Identifiers
    • Variables
    • Literals
    • Punctuators
    • Operators

Keywords/Reserved Words

  • Some reserved words in C++ have pre/system-defined meanings to the C++ compiler called keywords.
  • These are also known as Reserved words and are always written or typed in lowercase letters.
  • There are the following keywords in C++ object-oriented language:-
  • List of C++ Keywords are : –
asm default friend protected switch volatile
auto delete goto public template while
break do if register this
case double inline return try
catch else int short typedef
char enum long signed union
class extern new sizeof unsigned
const float operator static virtual
continue for private struct void

Identifiers

  • The identifier is a symbolic name that consists of a sequence of characters taken from a C++ character set.
  • Valid identifiers are a sequence of one or more letters, digits, or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier.
  • Only letters, digits, and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. In no case, they begin with a digit.
  • They cannot match any keyword of the C++ programming language.
  • The identifiers may follow the following rules –
    • The identifier must be the sequence of characters taken from a C++ character set.
    • An identifier may include alphabets, digits, and/or underscores. It must not start with a digit.
    • C++ is case sensitive, hence, upper case and lower case letters are considered different from each other, such as TOTAL and total are two different identifier names.
    • It should not be a reserved word. 

Variables

  • A variable is the most fundamental aspect of any computer language.
  • It is a location in the computer memory that can store data and is given a symbolic name for easy reference.
  • The variables can be used to hold different values at different times during the execution of a program.
  • For example: amount = 20;
  • Before a variable is used in a program, it has to be defined. This activity enables the compiler to make available the appropriate type of location in the memory.
  • The definition of a variable consists of the type name followed by the name of the variable.
  • Declaration of variables:
    • To use a variable in C++, we must first declare it specifying which data type we want it to be.
    • The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float, etc.) followed by a valid variable identifier.
    • For example- int a; float x; These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier x. Once declared, the variables a and x can be used within the rest of their scope in the program.
    • If we are going to declare more than one variable of the same type, we can declare all of them in a single statement by separating their identifiers with commas. For example- int a,b,c; This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as: int a; int b; int c; Similarly, a variable x of type float can be declared – float x; Similarly the variable y can also be defined as : – float y;
    • Examples of some valid variable declarations are:
      (i) int count; (ii) int i, j, k; (iii) char ch, first; (iv) float x, y; (v) long int sal;
  • Scope of variables: A variable can be either of global or local scope.
    • Global variable –
      • It is a variable declared in the main body of the C++ source code, outside all the functions.
      • Global variables can be called from anywhere in the code, even inside functions, whenever it is after its declaration.
    • Local variable –
      • It is one declared within the body of a function or a block. 
      • The scope of local variables is limited to the block enclosed in braces ({}) where they are declared.
      • For example, if they are declared at the beginning of the body of a function (like in function main(), their scope is between its declaration point and the end of that function.

Literals/Constants

Once an identifier is declared as constant at the time of declaration, its value can’t be changed during the execution of the program.

  • A number that does not charge its value during the execution of a program is known as a constant or literal.
  • Any attempt to change the value of a constant will result in an error message.
  • A keyword const is added to the declaration of an identifier to make that identifier constant.
  • A constant in C++ can be of any of the basic data types. For example: – const float Pi = 3.1215; here Pi is a constant of float types having a value: 3.1415.
  • Examples of some valid constant declarations are: – const int rate = 50; const float Pi = 3.1415; const char ch = ‘A’;.

Punctuators

  • In C++ programming language, certain characters are used as punctuators for enhancing the readability and maintainability of programs.
  • These are – 
Symbols/Characters Descriptions
Brackets [ ] Opening and closing brackets indicate single and multidimensional array subscripts.
Parentheses ( ) Opening and closing brackets indicate function calls, function parameters for grouping expressions, etc.
Braces { } Opening and closing braces indicate the start and end of a compound statement.
Comma , It is used as a separator in a function argument list.
Semicolon ; It is used as a statement terminator.
Colon : It indicates a labeled statement or conditional operator symbol.
Asterisk * It is used in pointer declaration or as a multiplication operator.
Equal sign = It is used as an assignment operator.
Pound sign # It is used as a pre-processor directive.

Data Types and Operators in C++

Operators in C++

  • C++ has a rich set of operators.
  • C++ supports six types of operators:
    • Arithmetic operators
    • Relational operators
    • Logical operators
    • Bitwise operators
    • Precedence of operators
    • Special operators
    • Type Conversion Operators
For Details about Operators Click this Link
Precedence Operators
Level of Precedence
(Descending Order)
Operators List
Description
Associativity
1 ::
scope Left-to-right
2 () [] . -> ++ — dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right
3 ++ — ~ ! sizeof new delete unary (prefix) Right-to-left
4 * & indirection and reference (pointers) Right-to-left
5 + – unary sign operator Right-to-left
6 (type) type casting Right-to-left
7 .* ->*
pointer-to-member Left-to-right
8 * / %
multiplicative Left-to-right
9 + –
additive Left-to-right
10 << >>
shift Left-to-right
11 < > <= >=
relational Left-to-right
12 == !=
equality Left-to-right
13 &
bitwise AND Left-to-right
14 ^
bitwise XOR Left-to-right
15 |
bitwise OR Left-to-right
16 &&
logical AND Left-to-right
17 ||
logical OR Left-to-right
18 ?:
conditional Right-to-left
19 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
20 ,
comma Left-to-right

    C++ Datatypes

    • Data Type in C++ is used to define the type of data that identifiers accept in programming and operators are used to perform a special task such as addition, multiplication, subtraction, and division etc of two or more operands during programming.
    • C++ supports a large number of data types which are categorized below –


    (A) Built-in-Data Types :
    • There are four types of built-in data types –
      • Char data type
        • It is used to store character values in the identifier.
      • Integer Data type (int)
        • An integer is an integral whole number without a decimal point.
        • These numbers are used for counting.
        • Normally an integer can hold numbers from -32768 to 32767.
        • For example, 33, 541, and -13 are valid integers.
        • The int data type can be further categorized into the following:- 
          1. Short int
          2. Long int
          3. Unsigned int
      • Floating point data type (float)
        • A floating point number has a decimal point.
        • Even if it has an integral value, it must include a decimal point at the end.
        • These numbers are used for measuring quantities.
        • Examples of valid floating point numbers are 23.4, -52.7, and 20.03.
      • Void data type
        • It is used for the following purposes:
          1. It specifies the return type of a function when the function is not returning any value.
          2. It indicates an empty parameter list on a function when no arguments are passed.
          3. A void pointer can be assigned a pointer value of any basic data type.
    Datatype Data Description Memory Consumed Data Range
    Signed  Unsigned Total 
    char Character – Single Character 1 Byte − 128 to 128 0 – 255 256
    short int/short Integer – Small numbers with +ive/-ive values without decimal points (non-fractional values) 2 Byte − 32,768 to 32,767 0 – 65535 65536
    long int/int/long Integer – Large numbers with +ive/-ive values without decimal points (non-fractional values) 4 Byte -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 4,294,967,296
    float Lower Precision Value Floating Point Number – Small numbers with +ive/-ive values with decimal point(fractional values) 4 Byte 3.4 e−38 to 3.4 e+38    
    long double/double Higher Precision Value Floating Point Number – Large numbers with +ive/-ive values with decimal point(fractional values)  8 Byte 1.7e − 308 to 1.7e + 308  
    void Special purpose type without any value  
    Bool Boolean value. It can take one of two values: true or false. 1 byte true or false true or false true or false
    wchar_t Wide character. 2 or 4 bytes 1 wide character  
    (B) Derived Data Types :
    • Derived data types are basically derived from the built-in data types.
    • C++ also permits four types of derived data types. These are : –
      • Array
      • Function
      • Pointer, and
      • Reference
    (C) User-Defined Data Types :
    • User-defined data types are defined by the programmers during the coding of software development.
    • C++ also permits four types of user-defined data types. These are : –
      • Structure
      • Union
      • Class, and
      • Enumerator

    A Sample C++ Program

    • A simple Classical C++ program has five sections –

    #include <iostream.h>                // Section: 1- The include Directive with header files.
    using namespace std;                // Section: 2 – Namespace declaration
    int main ()                                     // Section: 3 – Main function definition
    {                                                     
           cout << “Codershelpline.com” ;    // Section: 4 – Body of codes
           return 0;                                           // Section: 5 – control return
    }

    This is one of the simplest programs that can be written in C+ programming language. It contains all the fundamental components which every C++ program can have. The explanation of the above program codes of this program and its sections is given below: –

    Section: 1 – The Include directive with header files

    Here, #include <iostream.h> Lines beginning with a hash sign (#) are directives for the compiler’s pre-processor. In this case, the directive #include <iostream> tells the pre-processor to include the iostream standard header file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program to provide input and output operations. File iostream. h is a header file needed for input/output requirements of the program. Therefore, this file has been included at the top of the program.

    Section: 2 – Namespace Declaration

    All the elements of the standard ANSI C++ library are declared within namespace std;.To access its functionality, we declare all the entities inside namespace std;. This line is very often used in C++ programs that use the standard library and defines a scope for the identifiers that are used in a program.

    Section: 3 – Main function definition

    int main () line corresponds to the beginning of the definition of the main function. The main function is the point by which all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it – the instructions contained within this function’s definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration.

    The word void main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word void before main ( ) indicates that no value is being returned by the function main ( ). Every C++ program consists of one or more functions. However, when a program is loaded in the memory, the control is handed over to the function main ( ) and it is the first function to be executed.

    Section: 4 – Body of codes

    This is inside the main function that may contain one line or multiple lines of code as per the requirement of the program. These codes mainly include a variable declaration, input code, processing code, output code, etc. using control statements or functions.

    Each C ++ program starts with a function called main ( ). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end with a semicolon, without which an error message is generated.

    Section: 5 – control return

    The last line of the program returns the control to the main function which is normally integer return type. The return statement causes the main function to finish.

    Section: 6 – Comment line

        • A comment line is a statement in the program body to enhance the reading and understanding of the program.
        • Comments are included in a program to make it more readable.
        • If a comment is short and can be accommodated in a single line, then it is started with a double slash sequence in the first line of the program. It is called Single Line Comment. The syntax of short and one-line comments is:
          // Comment line…
        • However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */. Everything between /* and */ is ignored by the compiler. It is called Multiline Comment. The syntax of multiple line comments is –
    /* Start of multiple line comment . . .End of multiple line comment */
    • A typical Object Oriented C++ program has the following sections –

    #include <iostream.h>                // Section: 1- The include Directive with header files.
    using namespace std;                // Section: 2 – Namespace declaration

    class calculation                        // Section: 3 – class declaration
    {
    private:                            // Section: 4 – Datamember Access Specifier declaration
    int num1, num2, total;
    public:                         // Section: 5 – Member function Access Specifier declaration
    void input()
    {
    cout<<“Enter two values”;
    cin>>num1>>num2;
    }
    void process()
    {
    total= num1+num2;
    }
    void output()
    {
    cout<<total;
    }
    };
    int main ()                                     // Section: 6 – Main function definition
    {                                                     
         calculation cal1;                    // Section: 7 – Object declaration/creation
    cal1.input();                        // Section: 8 – Function calling using object.
    cal1.process();
    cal1.output();
       
         cout << “Codershelpline.com” ;    // Section: 9 – Rest main body of codes
         return 0;                                           // Section: 10 – control return
    }

    Loading


    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.