Lecture 4 - Variables Data Types
Lecture 4 - Variables Data Types
• Literals
• Variables
• Data Types
int main() {
int age = 25; // Integer literal
double pi = 3.14159; // Floating-point literal
char grade = 'A'; // Character literal
string name = “Sara"; // String literal
bool isPassed = true; // Boolean literal
return 0;
}
Getting Started with C++ 10
Variables
• Variables are containers (locations in computer memory) to store
values
• Variables are defined using names (also known as identifiers) and
their data types
• The equal sign (=) is used to assign values to variables
• Example:
int age = 15;
15
Reserved memory for variable “age”
Data Type Value
Variable Name
RAM
Getting Started with C++ 11
Syntax
int a = 10;
int num;
num = 15;
cout << num;
int x, y, z;
x = y = z = 50;
cout << x + y + z;
• Case-sensitive
• age, Age and AGE are three different variables
• Example:
const int NUM = 5; ✔
int main() {
const int myNum = 15;
myNum = 10;
cout << myNum;
return 0;
}
error: assignment of read-only variable 'myNum'
• Always declare the variable as constant when you have values that
are unlikely to change
• For example:
const int minutesPerHour = 60;
const float PI = 3.14;
Void
Wide Character
• The precision of a floating point value indicates how many digits the
value can have after the decimal point.
• The precision of float is only six or seven decimal digits, while
double variables have a precision of about 15 digits.
• Therefore, it is safer to use double for most calculations.
C++ language provide the sizeof() function to check the size of the data types
29
Getting Started with C++
Keywords
Keywords in C++
https://www.oreilly.com/library/view/sams-teach-yourself/0672327112/apb.html
words in a language
• Cannot be used as an
identifier (variable name or
function name)
Getting Started with C++ 30
User Input/Output
• #include <iostream> is a header file that lets us work with input and
output objects
#include <iostream>
using namespace std;
• Output using cout int main() {
• cout is pronounced "see-out" string fullName;
return 0;
}
Getting Started with C++ 31
Escape Sequences in C++
• Sequences of characters to insert special characters in a string
• Starts with a backslash ( \ )
Escape Sequence Name Description
\\ Backslash Inserts a backslash character.
\’ Single quote Displays a single quotation mark.
\” Double quote Displays double quotation marks.
\? Question mark Displays a question mark.
\a Alert (bell) Generates a bell sound in the C++ program.
\b Backspace Moves the cursor one place backward.
\f Form feed Moves the cursor to the start of the next logical page.
\n New line Moves the cursor to the start of the next line.
\r Carriage return Moves the cursor to the start of the current line.
Inserts some whitespace to the left of the cursor and moves the cursor
\t Horizontal tab
accordingly.
\v Vertical tab Inserts vertical space.
\0 Null character Represents the NULL character.
\ooo Octal number Represents an octal number.
\xhh Hexadecimal number Represents a hexadecimal number.
https://www.geeksforgeeks.org/cpp-escape-sequences/
Getting Started with C++ 32
Escape Sequences in C++ - Examples
cout << "Welcome to \“EE\"\n You are in CS107.";
Welcome to “EE"
You are in CS107 Class.
cout << "\t" << 5 << "\t" << "\x65" << endl;
cout << "\t" << 10 << "\t" << "\073";
5 e
10 ;
Getting Started with C++ 33
C++ Operators
• Arithmetic
• Assignment
• Comparison
• Logical
• Bitwise
int main() {
int x = 5;
x %= 3;
cout << x;
return 0;
}
6 110
3 011
1 0 0
1 1 1
0 1 0
Binary Decimal
010 2
int main() {
int x = 5;
x &= 3;
cout << x;
return 0;
}