P T H I N K F A ST S
Competitive ProgrammingFrom Problem 2 Solution in O(1)
C++ Data Types and Variables
Mostafa Saad Ibrahim
Teaching Assistant @ Cairo University
Remember
Magical Fact:
Read little + Practice much => Write good
Knowledge will be weird in the begin
Keep trying…read weird things again
Don’t be depressed. Challenge yourself
Suggested Books
A First Book C++
C++ Primer
Data Types
Saywe want to computer program sums numbers
from 50 to 90
Weneed to pass from 50 to 90 and sum numbers:
50+51+52+….90
Sosum initially is 50. In second step = 101. In 3 rd
step = 153 and so on.
Where to save this updated sum?
Data Types
Our computers has memory (RAMs)
Memory has size (e.g. 256 M) & stores 0s
&1s
We use this memory to store our computed
data
What types of data that we need to save?
Data Types
We need to save discrete numbers (integer)
And fractions 3.14 (float)
What about decisions? True& False
What about Blood type B? Character
What about a name? Sequence of characters
C++ Data Types
C++ Supports Built in data types
Integral Data Types
▪ Signed (allow negative) or unsigned (no negative)
Floating Point Data types
Void (no value)
C++ allows you create your types
Structure / Class
Data Types Details
Variables
Data in memory has addresses (in range RAM
size) and types (e.g. integer)
It is not convenient to refer to physical address
Variable = give name to location in memory
Variable = named storage to a data type
stored in RAM
Variables Declaration Examples
Integral Types
short x = 50;
int y = 10;
long long z = 9223372036854775807
char letter = ‘B’;
bool status = true;
int t; => Contains garbage
Floating Point Types
float f1 = 3.14;
double salary = 1500.6;
Investigating variable statement
int sum = 255;
int = integer variable type = 4 bytes in RAM
sum = variable name (called identifier)
Memory value = 255 (called literal)
▪ In binary format:255= 00000000000000000000000011111111
▪ In hexadecimal format:255= 000000FF
; end of statement
Reserve variable address in memory: 90E0100D
Usage Examples
Declaration
initialization
Assignment. Don’t declare again
Identifier (variable name)
Identifier: Variable name
int sum = 10; => sum is identifier
Identifier consist of: letters, digits, _
iNumber, status1, status2, mostafa_saad, _valid
Can’t start with digit
7Core
Case sensitive: sum != SUM
Shouldn’t use reserved keyword
int return = 6;
Reserved Keywords
Identifier Naming Conventions
Use good names (sum, salary)
starts with lower letter
Multiple words use _ or upper letters
first_name or firstName.
Some people prefer include type in start
iNum => i for integer
bStatus => b for bool
Literal
Value 20 is called literal
We can write an integer literal in 3 ways. E.g.
for value 20:
Decimal = 20
Octal = 024 (start with 0)
Hexadecimal = 0x14 (start with 0x)
Char literal: ‘b’
String Literal: “hello world”
Literals with Suffix
Floating Point Literals
Automatic Type Conversions
What if we used a type where we expect
another?
bool status = age;
▪ age is integer type, but we expect bool type!
Int age = salary
▪ salary is double, but we expect integer type!
Type conversion converts one type to another
Also happens on operations:
double multiplication = age * salary;
Type Conversions Examples
Forced Type Conversions
Forcing conversion is by casting in 3 ways:
Advise on data types
If values may be negative => signed
Use integer or long long (based on needs)
Use char/bool only when you need them
Use double, don’t use float. Usage only when
fractions appear.
When declaring value, always initialize it.
int x; => BAD
int x = 0; => Good
Guess Output
Guess Output
List all Errors and Warrings
At the end…
This is the first basics of C++ knowledge
Many students will find content weird.
If you understood it =
If you don’t understand = Repeat twice.
If still, find other video on web + Read book.
Success key: Practice much.
Write all codes and play with them.