What is a Variable ?
- ये data types की values अपने अंदर store करके रखता है |
- Variable ये एक memory location का नाम भी होता है |
Rules for Variable
- Variable ये case-sensetive होता है | for eg int a और int A ये अलग-अलग variables है |
- Variable की शुरुआत किसी भी alphabet(a-z, A-Z) या underscore( _ ) से होती है |
- Variables का नाम alphanumeric हो सकता है | For eg. a1 = 5, var1, var2
- Variable ये space को allow नहीं करता |
- Variable name कोई भी C++ Keywords नहीं होता |
हर एक data type का variable होता है |
| data type | Example | Description |
|---|---|---|
| int | int a = 2; | numeric values को declare करने के लिए int data type का इस्तेमाल होता है | |
| char | char ch = 'H'; and char str[]="hello"; | Character को declare करने के लिए char data type का इस्तेमाल करते है | |
| float | float f = 2.3; | floating-point जैसे variables को declare करने के लिए float data type का इस्तेमाल होता है, लेकिन ये single-precision होते है | |
| double | double d = 2.30; | floating-point जैसे variables को declare करने के लिए double data type का इस्तेमाल होता है, लेकिन ये double-precision होते है | |
| bool | bool b = true; or bool b = (!false) | bool के लिए दो values होती है | True और False |
| wchar_t | wchar_t str[] = L"Hello"; | ये एक wide character data type है | |
C Variable का Declaration और initialization
Variable Declaration
- जब Variable declare होता है तब ये variable जिस data type होता है, उसके हिसाब से Memory allocate करता है |
- Variable Declare होने के बाद ये अपने अंदर Garbage Value लेता है |
Syntax for Single Variable Declaration
data_type single_variable_name;for eg.
int a;
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a;
cout<<"Value of a : "<<a;
return 0;
}
Output :
Value of a : 4309822
| Variable_name | a | |
| Variable_value | 4309822 | Garbage Value |
| Address | 0x69fefc |
Syntax for Multiple Variable Declaration
data_type multiple_variable_name;for eg.
int a, b, c;
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a, b, c;
cout<<"Value of a : "<<a<<endl;
cout<<"Value of b : "<<b<<endl;
cout<<"Value of c : "<<c<<endl;
return 0;
}
Output :
Value of a : 4309822 Value of b : 6946708 Value of c : 4309728
| Variable_name | a | b | c | |
| Variable_value | 4309822 | 6946708 | 4309728 | Garbage Value |
| Address | 0x69fefc | 0x69fef8 | 0x69fef4 |
Variable Initialization
- जब Variable initialize होता है तब ये variable जिस data type होता है, उसके हिसाब से Memory allocate करता है |for eg. int for 2bytes(16-bit) | 4bytes(32-bit) | 8bytes(64-bit), char
- Variable intialization में Variable को normal value दी जाती है |
- Variable intialization में एक variable सिर्फ एक ही value लेता है for eg.
int a = 5, 6 ;int a = 5;
Syntax for Single Variable Initialization
data_type single_variable_name = value;for eg.
int a=5;
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a = 5;
cout<<"Value of a : "<<a;
return 0;
}
Output :
Value of a : 5
| Variable_name | a |
| Variable_value | 5 |
| Address | 0x69fefc |
Syntax for Multiple Variable Initialization
data_type single_variable_name = value, single_variable_name = value;for eg.
int a=5, b=6;
Source Code :
#include <iostream.h>
int main(){
int a = 5, b = 6;
cout<<"Value of a : "<<a<<endl;
cout"Value of b : "<<b;
return 0;
}
Output :
Value of a : 5 Value of a : 6
| Variable_name | a | b | |
| Variable_value | 5 | 6 | |
| Address | 0x69fefc | 0x69fef8 |
Variable Redeclaration
Variable को redeclare नहीं किया जा सकता |Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a, b;
int b;
cout << "Value of a : " <<a;
return 0;
}
Output :
error : redeclaration of 'int b'
Variable Scopes
- Variable Scope के दो प्रकार है |
- Local Variable
- Global Variable
Local Variable
- Local Variables function के अंदर होते है |
- Local Variables जिस function के अंदर होते है वह पर ही वो visible रहते है |
- Local Variables की default value 'garbage value' होती है |
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a = 5, b = 6, c; // Local Variable
cout<<"Value of a : "<<a;
cout<<"Value of b : "<<b;
cout<<"Garbage Value of c : "<<c;
return 0;
}
Output :
Value of a : 5 Value of b : 6 Default Value of c : 4309710 // garbage value
Global Variable
- Global Variables function के बाहर होते है |
- Global Variables की visibility पूरे program में होती है |
- Global Variables की default value '0' होती है |
Source Code :
#include <iostream.h>
using namespace std;
int a = 5, b = 6, c; // Global Variable
int main(){
cout<<"Value of a : "<<a;
cout<<"Value of b : "<<b;
cout<<"Default Value of c : "<<c;
return 0;
}
Output :
Value of a : 5 Value of b : 6 Default Value of c : 0







No comments: