C++ Variables

C++ Variables

A variable is a named item used to hold a value. This can be an actual value or an address that points to where a value is stored. If the variable holds an address that points to a place a value is stored it is a pointer.

All variables need to be assigned a value to work, on occasion a value will automatically be assigned to the variable, but that will depend on the variable. You assign a value with the ‘=’ sign. You should be reading this symbol in code as ‘assigned’, not ‘equal to’. (When it is by itself, ‘==’ is checking for equality).

A variable declaration is just where you are declaring that a variable of datatype exists. For example:

    datatype varName;

In the above example, while a variable of datatype has been made it has not yet been assigned a value, you will also notice that it is using the camel case naming convention, which will be explained in a different post.

A expression is a number, a variable, or a calculation. Simply put it’s something that will simplify to a value. An assignment statement is when a variable is assigned a value, whether it already had a value stored in it or not. As such an assignment statement has a left value and a right value. The left value is the variable, it’s the location at which you will be storing the right value. The right value is an expression it is the value that will be stored in the location on the left side. When the right value datatype doesn’t match the left value datatype there can be errors, either in terms of something like rounding or an actual compiler error. An example of an assignment statement is:

    varName = expression;

You can also initialize a variable. This is when you declare a variable and assign it a value in the same line. For example:

    datatype varName = expression;

The rules for a variable name (also known as an identifier) are:

  • Must be a sequance of letters, underscores and numbers
  • Must start with a letter or a underscore
    • This means that it cant start with a number
  • They can’t be a reserved word
    • a identifier can only be declared once in the same scope
  • Identifiers are case sensative

A scope is where a variable is defined. For example, if you declare a variable at the beginning of a function the scope of that variable is that entire function, while if you were to declare it in an if statement or a while loop it would only be defined in the while loop or if statement.

Related posts:(coming soon)

C# variables

C# variables

In C# a variable is a name given to a space in the computers memory that the program manipulates. The various types of variables decides the size,layout and the range of values for that space, as well as the operators that can be applied to it.

These types are separated into value types and reference types.(built in means that the types should be available in any class )

  • Value types:
    • Simple types :
      • Signed integral(sbyte,short, int, long);
      • Unsigned integral(byte, ushort, uint, ulong);
      • Unicode characters (char);
      • IEEE binary floating-point( float, double);
      • High-precision decimal floating-point (decimal);
      • Boolean( bool) -true or false ;
    • User defined types :Enum types (enum E { . . . });Struct types (Struct S { . . .})
    • Nullable value types:Extensions of all other value types with a null value
  • Reference types
    • Class types
      • Ultimate base class of all other types: object
      • Unicode strings: string
      • User-defined types of the form class C { . . .}
    • Interface types
      • User-defined types interface I {. . . }
    • Array types
      • Single- and multi-dimensional
    • Delegate types
      • User-defined types of the form delegate int D( . . . )

The variables I tend to use are string, int, double, float, char bool and the reference types. You can also use the key word var to make a variable, it has to be within a method though.

You declare variables by doing:

variableType variableName = variable;

so when declaring an int it would be something like:

int myNum =5:

You don’t have to declare a variable and assign it a value at the same time so long as you assign the value before you use it.

In which case you do:

variableType variableName;

If you don’t want anyone to be able to change the variable after its declaration(and you do need to declare the variable for it to work). You put const in front of the variableType so it would be:

const myNum=5;

which would cause an error if someone tried to overwrite the variable assignment later in the code. If you are just changing the assigned value just do:

variableName = Variable;

You can declare multiple variables of the same type at the same time by doing:

variableType varN1=v1, varN2=v2, varN3 = v3;

Note you can’t use key words as names for variables, and you want the name to be somewhat descriptive so you know what it is when you use it. The names are also case sensitive and should start with a lowercase letter (although I don’t think the lowercase part is required by the system), they can’t contain whitespace and I believe they can only use letters,numbers and the underscore character( _ ).