Page 17 - Modular_V1.1_Flipbook
P. 17

Variables

                 Variables are memory locations used to store values. When a variable is created, some space is
                 allocated in the memory for it. This memory space is referred by the name that we give to the
                 variable. The name we give to a variable is called an identifier.
                 Naming Conventions for Variables


                 Following are the naming conventions used to declare a variable in C++:
                      A variable name must start with a letter (a–z, A–Z) or an underscore ( _ ).

                      A variable name cannot start with a digit.

                      Keywords cannot be used as variable names.
                      A variable can only contain alpha-numeric characters and underscore (A–Z both capital as
                      well as small) and (0–9) numbers.

                      No special symbols like ! , @ , #, $, %, etc. can be used in variable name.

                      Variable names are case sensitive.
                      Variable names cannot contain blank space.

                 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 excluding
                                                          underscore (_)
                          int                             Keyword cannot be used as variable name

                          User Name                       White space is 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, some space is reserved for it in the memory
                 of the computer. Syntax to declare a variable in C++ is as follows:
                 <type> <variable_name>;

                 Where,  type  is  the  data  type  that  specifies  the  type  of  value  a  variable  can  store  and
                 variable_name is the name of the variable which is used to access the variable in the program.





                                                                                         Getting Started with C++   15
   12   13   14   15   16   17   18   19   20   21   22