Variables in C and C++ language
Variables in C#.NET
Variables in VB.Net
Variables & Datatypes
The Datatypes are something which gives information about1 Size of the memory location.
2 The range of data that can be stored inside that memory location
3 Possible legal operations which can be performed on that memory location.
4 What types of results come out from an expression when these types are used inside that expression.
Predefined Data Types – Example includes Integer, Boolean, Float, etc.
User-defined Data Types – Example includes Structure, Enumerations, etc.
Variable Declarations C & C++Language
A programs consist of objects, functions, variables, and other component parts.A variable is a named data storage location in your computer's memory. A variable is nothing but a name given to a storage area that our programs can manipulate. By using a variable's name in your program, you are, in effect, referring to the data stored there.
Variable Names
The variable name can be virtually any combination of letters, but cannot contain spaces. Good variable names tell you what the variables are for; using good names makes it easier to understand the flow of your program.To use variables in your C programs, you must know how to create variable names. In C, variable names must adhere to the following rules:
1) The name can contain letters, digits, and the underscore character (_).
2) The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended.
3) Case matters (that is, upper- and lowercase letters). Thus, the names count and Count refer to two different variables.
4) C keywords can't be used as variable names. A keyword is a word that is part of the C language. (A complete list of 33 C keywords can be found in Appendix B, "Reserved Words.")
Variable Name |
Legality |
Percent | Legal |
p2y5__kg7w | Legal |
year_profit | Legal |
_2021_profit | Legal but not advised |
saving#account | Illegal: Contains the illegal character # |
double | Illegal: Is a C keyword |
5ideas | Illegal: First character is a digit |
When you define a variable, you must tell the compiler what kind of variable it is: an integer, a character, and so forth. This information tells the compiler how much room to set aside and what kind of value you want to store in your variable.
Variable Type |
Keyword |
Bytes Required |
Character | char | 1 |
Integer | int | 2 |
Short integer | short | 2 |
Long integer | long | 4 |
Unsigned character | unsigned char | 1 |
Unsigned integer | unsigned int | 2 |
Unsigned short integer | unsigned short | 2 |
Unsigned long integer | unsigned long | 4 |
Single-precision | float | 4 |
floating-point | ||
Double-precision | double | 8 |
Variable Declaration: Syntax is data_type variableName;
Declaration & Initializing: data_type variableName = value;
In the above syntax, variableName is the variable name, while data_type is the name to which the variable belongs.
e.g: Integer variable:
int count; int num = 50;
int age = 20, lengths = 10;
int count, number, start; /* three integer variables */
int year_of_birth;
char gender;
C#.NET Variable Declaration:
Syntax is data_type variableName;
Declaration & Initializing: data_type variableName = value;
data_type variableName_list;
e.g.: int num, j, k;
char c, ch;
float f, salary;
double d;
int i = 100;
double pi = 3.14159;
float myNum = 5.75F;
The most common data types are:
Data Type | Size | Description |
---|---|---|
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
bool | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter, surrounded by single quotes |
string | 2 bytes per character | Stores a sequence of characters, surrounded by double quotes |
VB.NET
Variable Declaration in VB.Net
The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim statement is used at module, class, structure, procedure or block level.
Data Type | Storage Allocation | Value Range |
---|---|---|
Boolean | Depends on implementing platform | True or False |
Byte | 1 byte | 0 through 255 (unsigned) |
Char | 2 bytes | 0 through 65535 (unsigned) |
Date | 8 bytes | 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999 |
Decimal | 16 bytes | 0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal |
Double | 8 bytes | -1.79769313486231570E+308 through -4.94065645841246544E-324, for negative values 4.94065645841246544E-324 through 1.79769313486231570E+308, for positive values |
Integer | 4 bytes | -2,147,483,648 through 2,147,483,647 (signed) |
Long | 8 bytes | -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807(signed) |
Object | 4 bytes on 32-bit platform 8 bytes on 64-bit platform |
Any type can be stored in a variable of type Object |
SByte | 1 byte | -128 through 127 (signed) |
Short | 2 bytes | -32,768 through 32,767 (signed) |
Single | 4 bytes | -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values |
String | Depends on implementing platform | 0 to approximately 2 billion Unicode characters |
UInteger | 4 bytes | 0 through 4,294,967,295 (unsigned) |
ULong | 8 bytes | 0 through 18,446,744,073,709,551,615 (unsigned) |
User-Defined | Depends on implementing platform | Each member of the structure has a range determined by its data type and independent of the ranges of the other members |
UShort | 2 bytes | 0 through 65,535 (unsigned) |
In VB.NET, the declaration of a variable involves giving the variable a name and defining the data type to which it belongs. We use the following syntax:
Dim Variable_Name as Data_Type
Dim Variable_Name as Data_Type = Value
In the above syntax, Variable_Name is the variable name while Data_Type is the name to which the variable belongs.
Variable Initialization
Dim x As Integer
x = 10
Dim name As String
name = "Lalit"
Dim num as Integer = 20
Dim intInterestRate, intExchangeRate As Integer
Comments
Post a Comment