C++ Variables and Data Types
Variables
A variable is reserved memory location for storing some values.That means to tell the compiler about storage of the variable with help of data-type.
the general syntax is,
data-type variable_name = 10;
for example,
int myvariable = 10;
the variable is also referred as an Identifier.
C++ Identifiers
A C++ identifier is a name used for identifying any user defined things.That names refer a variable, function, class, template, or any other custom-defined.Identifiers are sequences of characters
Rules:
- Identifier can be include letters uppercase(A-Z) and lowercase(a-z)
- Identifier can be decimal digits(0-9).
- The first character of an identifier cannot be a digit.
- You Can Include underscore character ?_? in identifiers.The first character of an identifier cannot be an underscore(Some Compilers Accept underscore as a first Character).
- Lowercase letters and uppercase letters are distinct, such that foo and FOO are two different identifiers.
- When using GNU extensions, you can also include the dollar sign character ?$? in identifiers.
Fundamental data types
Data Type | Description | Size | Limit |
---|---|---|---|
char | Character or small integer. | 1byte signed: -128 to 127 | unsigned: 0 to 255 |
short(intshort) | Short Integer | 2bytes |
signed: -32768 to 32767 unsigned: 0 to 65535 |
int | Integer | 4bytes |
signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
long int (long) | Long integer. | 4bytes |
signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
bool | Boolean value | It can take one of two values: true or false |
1byte true or false |
float | Floating point number. | 4bytes |
+/- 3.4e +/- 38 (~7 digits) |
double | precision floating point number. | 8bytes |
+/- 1.7e +/- 308 (~15 digits) |
long double | double precision floating point number. | 8bytes |
+/- 1.7e +/- 308 (~15 digits) |
wchar_t | Wide character. | 2 or 4 bytes |
1 wide character |
Declaration Of Variables
In C++, all the variables must be declared before to use or initial statements of the block or main or function or global. variables should specify with data type.
It binds a data type and an identifier with the variable. The data type allows the compiler to execute statements correctly.
int a;
float number;
simple example program declares the variable, assign value and printing variable.
// variable declaration in c++
#include <iostream>
using namespace std;
int main ()
{
// declaring variables, Its name is b
int a;
// assining values in a:
a = 5;
// just printing a value
cout << a;
// terminate the program:
return 0;
}
5
Operating Of Variables Example Program
// variable declaration and Operating variables in c++
#include <iostream>
using namespace std;
int main ()
{
// declaring variables , Its name are a,b and c
int a,b,c;
// assining values in a:
a = 5;
b = 10;
//Operating variables
c = a + b;
// just printing a result c
cout << c;
// terminate the program:
return 0;
}
15
Introduction to strings
- The C++ language supports strings.
- strings data type (string class type) support through the standard string class and Its supports all the basics operations.
- strings is not a fundamental type.
- It behaves in a mostly similar way as basic data types.
- C++ supports also The C-style character string(Array of Char).
Simple Example Program for C++ string
// string variable declaration in c++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
// declaring variable str for string type
string str = "Hello C++ String";
// just printing string
cout << str;
// terminate the program:
return 0;
}
Hello C++ String
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming