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


int a;
char b;
int c = 10;
char d ='d';
int nums[6];
int nums[6] = {0,1,2,3,4,5};

What’s in a Name? Aspect #1.

a Rose, is not a rose, is not a ROSE 🌹
Global Scopeinclude <iostream>
using namespace std;
int x; // Global variable declared
int main()
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;
--------------------------------------------------Local Scopeinclude <iostream>
using namespace std;
int main()
int i=10;
if(i<20) // if condition scope starts
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
β•‘asm β•‘ else β•‘ new β•‘ this β•‘
β•‘auto β•‘ enum β•‘ operator β•‘ throw β•‘
β•‘bool β•‘ explicit β•‘ private β•‘ true β•‘
β•‘break β•‘ export β•‘ protected β•‘ try β•‘
β•‘case β•‘ extern β•‘ public β•‘ typedef β•‘
β•‘catch β•‘ false β•‘ register β•‘ typeid β•‘
β•‘char β•‘ float β•‘ reinterpret_castβ•‘ typename β•‘
β•‘class β•‘ for β•‘ return β•‘ union β•‘
β•‘const β•‘ friend β•‘ short β•‘ unsigned β•‘
β•‘const_cast β•‘ goto β•‘ signed β•‘ using β•‘
β•‘continue β•‘ if β•‘ sizeof β•‘ virtual β•‘
β•‘default β•‘ inline β•‘ static β•‘ void β•‘
β•‘delete β•‘ int β•‘ static_cast β•‘ volatile β•‘
β•‘do β•‘ long β•‘ struct β•‘ wchar_t β•‘
β•‘double β•‘ mutable β•‘ switch β•‘ while β•‘
β•‘dynamic_castβ•‘ namespace β•‘ template β•‘ β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•

Why Specify the Type? Aspect #2

Quick Intro to Data Types & Modifiers

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 type
int a = 1; // integer type
float a = 3.14159; // floating point type
double a = 6e-4; // double type (e is for exponential)
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.
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 β•‘
float pi = 3.14;
double morePrecisePi= 22.0 / 7;
β•‘ 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 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
//Use the keyword β€˜const’ to define a constant.enum RainbowColor
Violet = 0;
enum MONTH {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
#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;
typedef double MyDouble;double dAmount;
MyDouble dAmount; //Same like 'double dAmount;'

Aspect #3: Value.

unsigned int highScore;
highScore = 3000;

#TEDx SpeakersπŸŽ™οΈ| M.I.T Media Lab BC Alumni ⛓️| Top 5 IBM #SmartCities Finals πŸŒ‡ | πŸš€PR πŸ‡΅πŸ‡· LA | #TwitchDev #Blockchain #AI| |

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store