Skip to main content

Variables

Variables

Variables in C and C++ language
Variables in C#.NET
Variables in VB.Net

Variables & Datatypes

The Datatypes are something which gives information about

1 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

Popular posts from this blog

Robot

Robot is drawn from an old Church Slavonic word, robota, for “servitude,” “forced labor” or “drudgery.” The word, which also has cognates in German, Russian, Polish and Czech, was a product of the central European system of serfdom by which a tenant’s rent was paid for in forced labor or service. The word robot was coined by artist Josef Čapek, the brother of famed Czechoslovakian author Karel Čapek. As a word, robot is a relative newcomer to the English language. It was the brainchild of a brilliant Czech playwright, novelist and journalist named Karel Čapek (1880-1938) who introduced it in his 1920 hit play, R.U.R., or Rossum’s Universal Robots. The robots in this play were not what we would call robots today, and they weren’t made of steel, plastic, and lines of code. Those robots were manufactured as pseudo-organic components out of a substance that acted like protoplasm in a factory, then “assembled” into humanoids. Watch video --> Saudi Arabia grants citizenship to huma...

The 120 year old light bulb that never been turned off.

Light bulb that never been turned off since 1901. ivermore's Centennial Light Bulb The Centennial Light is the world's longest-lasting light bulb, burning since 1901 , and almost never switched off. Due to its longevity, the bulb has been noted by The Guinness Book of World Records , Ripley's Believe It or Not!, and General Electric.  The Centennial Light was originally a 30-watt(or 60-watt) bulb, but is now very dim, emitting about the same light as a 4-watt nightlight. The hand-blown, carbon-filament common light bulb was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s and was invented by Adolphe A. Chaillet. The hand-blown, carbon-filament common light bulb was invented by Adolphe Chaillet, a French engineer who filed a patent for this technology. It was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s; many just like it still exist and can be found functioning. According to Zylpha Bernal Beck, the bulb was...

Wireless power transfer

Wireless power transfer Inventor and engineer Nikola Tesla, 'the man who invented the 20th century' theorized about wireless electricity back in the 1890s. He even demonstrated the principle by lighting up glass tubes with wireless power transmission. Nikola Tesla wanted to create the way to supply power without stringing wires. He almost accomplished his goal when his experiment led him to creation of the Tesla coil. It was the first system that could wirelessly transmit electricity. Wireless power transfer (WPT), wireless power transmission, wireless energy transmission (WET), or electromagnetic power transfer is the transmission of electrical energy without wires as a physical link. Wireless power transfer (WPT) is one of the hottest topics being actively studied, and it is being widely commercialized. In particular, there has been a rapid expansion of WPT in mobile phone chargers, stationary charging electric vehicles (EVs), and dynamic charging EVs, also called road-po...