C++ Variables and Literals

C++ Variables:
  1. In programming, a variable is a container (storage area) to hold data. प्रोग्रामिंग में, डेटा रखने के लिए एक variable एक कंटेनर (भंडारण क्षेत्र) है।
  2. To Indicate the storage area, each variable should be given a unique name (identifier). (प्रोग्रामिंग में, डेटा रखने के लिए एक Variable एक कंटेनर (भंडारण क्षेत्र) है। भंडारण क्षेत्र की पहचान करने के लिए, प्रत्येक Variable को एक अद्वितीय नाम (पहचानकर्ता) दिया जाना चाहिए।) For example. (int age=14;)
  3. Here, age is a variable of the int data type, and we have assigned an integer value 14 to it.
Note: The int data type suggests that the variable can only hold integers. Similarly, we can use the double data type if we have to store decimals and exponentials.
  1. The value of a variable can be changed, hence the name variable.
    1. int age=14;        //age is 14
      age=17;              //age is 17
Rules for naming a variable:
  1. A variable name can only have alphabets, numbers, and the underscore _.
  2. A variable name cannot begin with a number.
  3. It is a preferred practice to begin variable names with a lowercase character. For example, name is preferable to Name.
  4. A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
  5. A variable name can start with an underscore. However, it's not considered a good practice.
Note: We should try to give meaningful names to variables. For example, first_name is a better variable name than fn.

C++ Literals:

  1. Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc. (लिटरल वे डेटा हैं जिनका उपयोग निश्चित मानों को दर्शाने के लिए किया जाता है। उन्हें सीधे कोड में इस्तेमाल किया जा सकता है। उदाहरण के लिए: 1, 2.5, 'c' आदि।)
  2. Here, 1,2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
Types of Literals:
  • Integers:
    1. An integer is a numeric literal (associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming: 
      1. Decimal (10 base)
      2. Octal (8 base)
      3. Hexadecimal (16 base)
    2. In C++ programming, octal starts with a 0, and hexadecimal starts with a 0x.
  • Floating-point Literals:
    1. A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example: 
      1. -2.0
      2. 0.000254
      3. -0.22E-5
  • Characters: 
    1. A character literal is created by enclosing a single character inside single quotation marks. For example:
      1. 'a', 'm', 'F', '2', '}'
  • Escape Sequences:
    1. Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For example, newline (enter), tab, question mark, etc.
      • Escape Sequences            Characters
      • \b                                        Backspace
      • \f                                         Form Feed
      • \n                                        New Line
      • \r                                         Return
      • \t                                         Horizontal tab
      • \v                                        Vertical tab
      • \\                                         Backslash
      • \'                                         Single Quotation Mark
      • \"                                        Double quotation mark
      • \?                                        Question mark
      • \0                                        Null Character
  • String Literals:
    • A string literal is a sequence of characters enclosed in double-quote marks. For example: 
    • "good" - string constant
    • "" - null string constant
    • "     " - string constant of six white space
    • "x" - string constant having a single character
    • "Earth is round \n" prints string with a newline

C++ Constants

  1. In C++, we can create variables whose value cannot be changed. For that, we use the const keyword. Here's an example:
    1. const int LIGHT_SPEED=299792458;
    2. LIGHT_SPEED=2500 //Error! LIGHT_SPEED is a constant.
  2. Here, we have used the keyword const to declare a constant named LIGHT_SPEED. if we try to change the value of LIGHT_SPEED, we will get an error.

Comments

Popular posts from this blog

History of Computer

Introduction to Computer

Computer Generation