Variables and Data Types
Variable: Portion of memory for storing a determined value. Could be numerical, could be character or sequence of characters called strings Variable examples: 20, “California”, ‘a’ etc…
Valid Identifier: Used to identify a particular variable in the memory. Like a reference. Sequence of one or more letters, digits or one underscore character. Cannot start with number Best practice is to start with a letter. Examples: myValue, _num, num2 etc…
Cannot use words like: asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while Or, cannot use and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq
C++ is case sensative Name identifier like abc is not same as Abc or ABC. They are identified completely differently
Computer needs to know what kind of data we are storing in case we will have to process those data later on for other purposes. Fundamental data types: char : Character or small integer, size=1 byte short int(short) : short integer, size: 2 bytes int: Integer, size: 4bytes long int(long): long integer 4bytes bool: Boolean value(true or false, 1 or 0), size: 1byte float: decimal number, size: 4 bytes double: double precision floating point number, size: 8 bytes long double: size: 8bytes wchat_t : Wide character, size: 2 or 4 bytes
int a; identifier is a, data type is integer float b; identifier is b, data type is b; double num; identifier is num, data type is double int a, b, c; 3 integer variables named a, b, c Same as int a; int b; int c;
Signed: can have both positive and negative numbers Unsigned: Only positive numbers Example: signed int a; unsigned int b; Default is signed in c++ so if nothing is given it takes it as signed. Example int a; is signed integer a.
#include using namespace std; int main() { int a, b; int sum; a=23; b=56; sum=a+b; cout << sum; return 0; }
Variable needs to be declare first to be initialized. int a; is declaring variable, saying that make a new variable named a which is an integer a = 23; Initializing variable a and giving value 23 to a; In c++ both can happen in same line Example int a = 23; First initialized int a( = 23;) Then initialized int ( a = 23; )
Can be initialized in two ways: int a = 23; Or int a(23);
#include using namespace std; int main() { int a = 23; int b(56); int sum; sum=a+b; cout << sum; return 0; }
Need to add #include to start the program String: sequence of characters Declaring string same as other variables except changing data type to string Example: string myname; Value of string data type always needs to be inside “ ”. Example myname = “Dean”; While declaring characters they should be given inside single quotes: Example: char myChar=‘a’; Character takes only one character while string is collection of character in one variable identifier
#include using namespace std; int main() { string MyName = “My name is Dean”; cout << MyName << endl; string AnotherString = “Jones”; string AddedString = MyName+AnotherString; cout << AddedString << endl; } Important!!! String Concatination (Adding two strings)
Next Video: Constants Then: Operators Following with: Basic input and output to end the basics!!!