C Plus Plus Variable Declaration Methods

C Plus Plus Variables Declaration Methods
C Plus Plus Variables Declaration Methods

C++ Variable, Literals, and Constants with Examples


    C++ Variable: Variables are containers used to store a value in our program. A variable is a named area of​​memory where we can store and manipulate the values ​​of our program.
    The variable name can be chosen by the programmer in any meaningful way to reflect its function or nature in the program. The rules for naming variables are the same as for naming identifiers.
    All variables are temporarily stored in random access memory called RAM. For this reason, they are automatically deleted upon completion of the function/program.

    Variables in C++| What are the rules in naming variables

    There are some restrictions on variable names and symbolic constants.
    Variables and constants are the main data objects processed by the program.
    The manifest lists the variables used and describes their types and, possibly, their initial values.
    1. Names are made up of letters and numbers.
    2. The first character must be a letter.
    3. An underscore is considered the letter "_" but does not start a variable name with an underscore.
    4. Uppercase and lowercase letters are different, so v and V are two different names.
    5. Traditional C++ practice is to use lowercase for the variable name and all uppercase for symbolic constants.
    6. Keywords like other, int, float, etc. are Reserved, we cannot use them as a variable name.
    7. At least the first 31 characters of a local variable name are important.
    For function names and external variables, the number may be less than 31. For external names, the standard guarantees exclusivity for only 6 characters and one case.

    How to print a string variable in C++ using cout? 

    #include<iostream>

    #include<conio.h>

    int main()

    {

    int id = 7834;

    char name[20]="codeswithbaby"; 

    char gender = 'm'; 

    float height = 6.4;    

    cout<<" Student ID No:"<<id;  

    cout<<"\nStudent Name:"<<name; 

    cout<<"\n Student Gender:"<<gender;

    cout<<"\nStudent Height:"<<height;

    return 0;

    }

    *****OUTPUT*****

    Student ID No:7834

    Student Name:codeswithbaby

    Student Gender:m

    Student Height:6.4


    Variables are case sensitive like lower and upper case letters are different

    #include<iostream>

    #include<conio.h>

    int main()

    {

    int a ; 

    int A ;

    a = 100; 

    A = 200; 

    cout<<"Value of a:"<<a<<endl; 

    "Value of A:"<<A;

    return 0;

    }

    *****OUTPUT*****

    Value of a:100

    Value of A:200


    How many types of variables are there in C++?

    We are using basically two types of variables in C++ Programming  language,
    1. Local variables
    2. Global variables

    Local Variables: Local variables are accessible only inside the function or block.

    Global Variables: A global variable can be accessed anywhere within the program.


    How To Use Local variables in C++?

    #include<iostream>

    #include<conio.h>

    int main()

    {      

    int a = 100;    

    cout<<"Local variables value of a:<<a;

    return 0;

    }

    *****OUTPUT*****

    Local variables value of a:100


    How To Use Global Variables in C++?

    #include<iostream>

    int a = 100;

    int main()

    {  

    cout<<"Global variables value of a:<<a;

    {  

    int a = 30;    

    cout<<"Local variables value of a:<<a;

    return 0;

    }

    *****OUTPUT*****

    Global variables value of a:100

    Local variables value of a:30


    Constant Variables and Volatile Variables C++ Language

    Constant Variable: To keep the value of the variable unchanged during the program execution. It can be done by declaring the variable as a constant. 
    The keyword const is then prefixed before the declaration. It tells the compiler that the variable is a constant. Thus, constant declared variables are protected from modification.
    Example: Const int x = 20;
    Where const is a keyword and x is the variable name and 20 is a constant.
    The Compiler protects the value of ‘x’ from modification.
    The user can not assign any value ‘x’ but using a pointer the value can be changed.
    If a user attempts to modify the value of the constant variable, the message ‘ cannot modify the constant object” will be displayed.
    Volatile Variable: The volatile variable is those variables that can be changed at any time by another external program or the same program.

    The syntax: volatile int d;


    Example: Write a Program Constant and Volatile Variable

    #include<iostream>

    #include<conio.h>

    void main()

    {

    volatile int x;

    Volatile cons tint y = 10;

    clrscr();

    cout<<Enter an integer:;

    scanf(“%d”,&x);

    cout<<An Entered integer is :<<x;

    x = 3;

    cout<<The Changed value of x is:<<x ;

    cout<<Value of y: <<y;

    getch();

    }

    *****OUTPUT*****

    Enter an integer: 5

    An entered integer is: 5

    The changed value of x is : 3

    Value of y: 10


    Read Also Post Other Programming Languages

    👉Java Variables


    👉C Variables


    👉Python Variables


    👉Php Variables

    Post a Comment

    0 Comments
    * Please Don't Spam Here. All the Comments are Reviewed by Admin.