sizeof can be used to find the number of bytes needed to store an object (variable or data type)
Type Conversion
// Binary expressionint r = 2;double pi = 3.14159;cout << pi * r * r << "\\n"; // double// Assignmentdouble a = 1.23;int b = a;cout << a << "\\n";// Type-Castingdouble a = 1.23;int b = (double) a;cout << a << "\\n";
Implicit type conversion
Binary expression: lower-ranked operand promoted to a higher-ranked operand
Assignment: right operand is promoted/demoted to match the variable type on the left
Constants must be initialized during the declaration
Scope
#include <iostream>using namespace std;int a = 0;int main() { int a = 1; cout << "a in main: " << a << endl; // will use local variable return 0;}
Scope of a variable/constant refers to the region of a program where the variable/constant is visible (can be accessed)
Scope using namespace
#include <iostream>using namespace std;int a = 0;namespace level1 { int a = 1; namespace level2 { int a = 2; }}int main() { int a = 3; cout << ::a << endl; // will use global variable cout << level1::a << endl; cout << level1::level2::a << endl; return 0;}
Operators
Assignment Operator, =
float a = 2.0 * 4.0 * 8.0;float b = a - sprt(a);char x = 'a';
a = b+++c. // same as (b++) + cint a, b = 1;a = b = 3 + 1; // a = 4, b = 4
Precedence - order of evaluation for different operators
Associativity - order of evaluation for operators with the same precedence
Bitwise Operators
#include <iostream>using namespace std;int main() { short a = 3, b = 5, c; c = a & b; cout << c << endl; // 1 c = a | b; cout << c << endl; // 7 c = a ^ b; cout << c << endl; // 6 char t = 254; c = ~t; cout << c << endl; // 1 char x = 6; int i = (x >> 1) & 1; // 0110 -> 0011 -> 0001 -> 1 cout << i << endl; int j = (x >> 3) & 1; // 0110 -> 0000 -> 0000 -> 0 cout << j << endl; return 0;}
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Bitwise NOT ~
Left shift <<
Right shift >>
Basic I/O
cout: Output Operator <<
cout: Change the Width of Output
cout.width(5);cout << 123 << endl; // --123cout << 123 << endl; // effect lasts for one field onlycout << setw(5); // same as cout.width, but requires <iomanip>cout << 1234567 << endl; // 1234567, prints all if exceeds