Intro to C++ : Variables, Constants, & Data Types

It lets the complier know how much memory needs to be set aside to hold that variable’s value.

So all in one, a variable has three aspects that needs to be checked: name, type, value.

The basic syntax shown below accomplishes the first two.

Basic Syntax VariableType VariableName;Multiple Variables of the same data type can be created.

VariableType VariableName1, VariableName2, VariableName3;Run the program below to see the memory size of different data types.

Quick Intro to Data Types & ModifiersDiving into Primitives, Modifiers, TypeQualifier, TypeDef, & more before moving onto aspect #3.

Primitives Data Types Built-in or predefined data types and can be used directly by the user to declare variables.


g int, char , float, bool etc.

DATA TYPES (Primitive)╔══════════╦════════════════╦══════════════════════╦═══════════════╗║ Keyword ║ Type ║Description ║ Example ║╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ char ║ character ║ a single byte ║ 'A' ║ ╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ int ║ integer ║ a whole number ║ 10 ║╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ float ║ floating point ║ a floating number, ║ .

0123456 ║ ║ ║ ║ correct to 6 numbers ║ ║ ╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ double ║ double floating║ a floating number, ║ .

0123456789 ║ ║ ║ point ║correct to 10 numbers ║ ║ ╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ bool ║ boolean ║ check the state ║ true or false ║ ║ ║ ║ of a variable ║ 0 or 1 ║ ╠══════════╬════════════════╬══════════════════════╬═══════════════╣║ void ║ valueless ║ W/o any value ║ ║ ╠══════════╬════════════════╬══════════════════════╬═══════════════╣ ║ wchar_t ║ wide character ║ size greater than the║ ║║ ║ ║ normal 8-bit datatype║ ║╚══════════╩════════════════╩══════════════════════╩═══════════════╝More exampleschar a = 'A'; // character typeint a = 1; // integer typefloat a = 3.

14159; // floating point type double a = 6e-4; // double type (e is for exponential)Data Types Modifiers There are four datatype modifiers: signed, unsigned, long, short.

The datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold, except for type void.

Data Types (Modifiers)╔════════════════╦══════════════╦════════════════╗║ short ║ int ║ long ║║ unsigned short ║ unsigned int ║ unsigned long ║║ signed short ║ signed int ║ signed long ║╚════════════════╩══════════════╩════════════════╝Note short is short name for short int Note long is short name for long int.

What are the rules for assigning variable with the above data types?Assign short, int or long data types when you are sure a variable is an integer number (NO decimal points).

While these three integer choices are available, based upon the size of the numbers you are using.

Assign float or double when decimals are needed.

Assign char if the variable will always contain ONE character of data.

This means only one letter (or character).

Data Types Modifiers Be used along with built in datatypes to make them more precise and even expand their range.

Data Types (Modifiers by Categories)╔════════════════╦════════════════╦════════════════╗║ Integer Type ║ Character Type ║ Floating-Point ║ ║ Example ║ Modifier ║ Type Modifiers ║╠════════════════╬════════════════╬════════════════╣║ short ║ char ║ float ║║ unsigned short ║ unsigned char ║ double ║║ signed int ║ signed char ║ long double ║╚════════════════╩════════════════╩════════════════╝Integer Type Modifiers Offers three types of integers: short, int, and long that can represent up to three different integer sizes.

Unsigned, numbers are always positive.

Unsigned type are used for quantities that are never negative such as populations, sports, scores, inventory, etc.

Signed types includes both positive and negative numbers and is the default type.

Size hierarchy: short int < int < long intCharacter Type Modifiers Large enough to represent the entire range of basic symbols — all the letters, digits, punctuation and the like — for the target computer system.

char is neither signed nor unsigned by default, unlike int.

holds numbers i.

e characters/symbolsunsigned char range(0–256)signed char range (-128 to 127)Floating Point Type Modifiers Have three floating types : float, double, and long double.

A float allows you to store decimal values that would be declared in the following.

float pi = 3.

14;double morePrecisePi= 22.

0 / 7;double precision float is simply called a double.

long float is not a legal type.

there are no short floating point numbers.

Size Hierarchy : float < double < long doubleType Qualifiers in C++The type qualifiers can help provide additional information about the variables they precede.

╔═══════════╦═══════════════════════════════════════════════╗║ const ║ Objects of type const cannot be changed ║║ ║ by your program during execution ║╠═══════════╬═══════════════════════════════════════════════╣║ restrict ║ a pointer qualified is initially the only ║║ ║ means by which the object it points to can ║║ ║ be accessed.

║╠═══════════╬═══════════════════════════════════════════════╣ ║ volatile ║ the modifier volatile tell the compiler that ║║ ║ a variable value maybe changed in ways not ║ ║ ║ explicit specified by the program ║ ╚═══════════╩═══════════════════════════════════════════════╝ConstantsA constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program.

Define a variable as a constant.

Use the keyword ‘const’ to define a constant.

*Should not change a single thing because that variable cannot be changed during the program.

You should be receiving this error: cannot assign to variable ‘benchGoal’ with const-qualified type ‘const int’ and such.

*Enumerated ConstantsThis means that programmer can create a new variable type and then assign a finite number of values to it.

What purpose does this serve?.When you need a type of variable whose values are restricted by a certain set.

//Use the keyword ‘const’ to define a constant.

enum RainbowColor{ Violet = 0; Indigo, Blue, Green, Yellow, Orange, Red};Another example has “enum variable” has 12 possible values.

enum MONTH {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};BTW, Use a singular name for the enum.


g MONTH instead of MONTHS.

Another way to understand this is using a compass example.

#include <iostream>using namespace std; int main(){ // set up the enumeration enum Direction {North, Northeast, East, Southeast, South, Southwest, West, Northwest }; //create a variable to hold the it Direction heading; heading = Southeast; cout << "Moving " << heading; return 0;}TypeDefYou can also define your own name for a built in data type.

You have to at least use the keyword typedef.

typedef double MyDouble;double dAmount;MyDouble dAmount; //Same like 'double dAmount;'Aspect #3: Value.

A variable can be given a value through an assignment statement.

unsigned int highScore;highScore = 3000;Congrats you learned a lot more about variables, constants, and data types !.The biggest challenge with variables comes with choosing the proper data type.

Remember to keep in mind when working with variables, the number of bytes differs by data type.

Next we will be focusing more on using expression, statements, operators.


. More details

Leave a Reply