Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 139 - CoreGPT(Podar)_Flipbook_C8
P. 139

    Examples of some valid variable names are:

                                  Valid Variable Name                             Reason

                              Gaurav123                       Starts with a letter
                              _usha93                         Starts with an underscore symbol

                     Examples of some invalid variables are:

                                 Invalid Variable Name                             Reason

                              1_Name                          Cannot start with a digit
                              @Address                        Cannot starts with a special symbol (except _ )

                              int                             Keyword cannot be used as variable name
                              User Name                       Spaces are not allowed in variable names

                     Declaring a Variable

                     C++ is a strongly-typed language, which means you need to declare a variable before using it.
                     You also need to specify the data type of the variable, so that the compiler understands how the
                     variable is interpreted. When a variable is declared, the computer reserves memory space for it.
                     Syntax to declare a variable in C++ is as follows:

                     data_type variable_name;
                     Where,  data_type  specifies  the  type  of  value  a  variable  can  store  (e.g.,  int,  float,  char)  and
                     variable_name is the name of the variable which is used to access the variable in the program.

                     For example:
                           int age;

                           char grade;
                     Where, int and char are the data types and, age and grade are the variable names. You can also
                     declare multiple variables of the same type in a single line in the following way:
                           int emp_code, age, pincode;
                     Initialising a Variable

                     Initialising a variable means to assign a value to the variable. You can assign a value to a variable
                     using the assignment operator (‘=’) in the following way:
                          age = 21;               // initialising an integer variable

                          grade = 'A';            // Initialising a character variable
                     In the above example, you can see that the single quotes (' ') are used to initialise a character
                     variable. A variable can also be initialised during their declaration itself in the following way:

                           int age = 21;
                           char grade = 'A';
                     Let's write a program to show this concept more clearly.

                                                                                      Developing Programs in C++        137

| 134 135 136 137 138 139 140 141 142 143 144 |