4(a): ***(Syntax)*** Constants, Variables and Keywords

 

Keywords:

              Keywords are reserved words whose meaning is already known to the compiler and they can not be used elsewhere in the program for naming a variable or a function. 
==> List of C++ keywords is: https://en.cppreference.com/w/cpp/keyword

Constants:

Constants are very similar to a variable and they can also be of any data type. The only difference between a constant and a variable is that a constant’s value never changes.

Types of constants:

        There are three types of constants:
  1.   Integer Constants ===================>    -1,    6,    7,    9  (all non-fraction numbers)
  2.   Real Constants =====================>    -30.1,     2.5,    7.0   (all fraction numbers)
  3.   Character Constants =================>      'a'     '$'     '@'     '+'    (letters or characters  enclosed within single inverted commas)

String Laterals (String Constants):

They are a sequence of characters enclosed in double quotation marks(" "). For example,  “This is a string literal!” is a string literal or just string. Escape sequences are also string literals.

If you write const before a variable, it will become constant for rest of the program and changing it will cause error.

#include <iostream>
using namespace std;
 
int main()
{
    const float PI = 3.14;      //PI becomes constant
    cout << "The value of PI is " << PI << endl;
    PI = 3.00;                //error, since changing a const variable is not allowed.
}

Thus output will be:

error: assignment of read-only variable 'PI'

C++ Variables:

Variables are containers for storing data values. For example:
                a = 3;           //  a is assigned "3"
                b = 4.7;       //   b is assigned "4.7"
                c = 'A' ;       //  c is assigned 'A'

Rules for naming a Variable:

  1. A variable name can only contain alphabets, digits, and underscores( _ ).
  2. No commas, blanks, or special characters other than underscore ( _ ) are allowed.
  3. Variable names are case-sensitive ie Abc and abc are two different variables.
We must create meaningful variable names in our programs. This increases the readability of our program.

Types of Variables:

Various types of integers and their writing format in C are as follow:
  1. Integer variables ======> int a = 3;                                           numbers without decimal
  2. Real variables ========> float a = 7.7;    int a=7.7; is wrong   decimal number of low precision
  3. Character variable ====> char a = 'B';        add ''                       single character i.e '+' or 'A'
  4. Double variable =======> double a = 3.1412653589            decimal number of high precision   
  5. Boolean variable ======> bool a = true                                  store true or false, 0 or 1
  6. String variable ========> string a = "This is my name"

Declaration of Variable:

We cannot declare a variable without specifying its data type. The data type of a variable depends on what we want to store in the variable and how much space we want it to hold. The syntax for declaring a variable is simple:
data_type  variable_name = value;

For example:

    int a=3;
    float b=4.5;
    char c='A';
    double d=3.14159265;
    bool e=true;

Note:

Taking full line as input and printing it in 4(b) ie next post

Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_