![]() |
C Plus Plus Variables Declaration Methods |
C++
Variable, Literals, and Constants with Examples
Variables in C++| What are the rules in naming variables
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.
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.
#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 |
#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++?
1. Local 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.
#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 |
#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
The
syntax: volatile
int d;
#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 |