Basics - sheerazwalid/COMP-I GitHub Wiki
- Used to store data in the memory.
- Referred to by its identifier (the name of a variable).
- The data stored in a variable can be changed using the assignment operator.
- To use a variable, you must declare it first.
- A declaration specifies a type, and contains a list of one or more variables of that type.
variable_type variable_name1, variable_name2;
int age, steps;
char c;
bool win;
double height, width, length;
double weight;
- Tells the computer how to interpret the data stored in a variable
- Determines the size of storage needed to store the data
- Some basic data types in C++:
type | typical size in bytes |
---|---|
char | 1 byte |
int | 4 bytes |
double | 8 bytes |
bool | 4 bytes (because it's an int) |
Use sizeof() to determine the size of a variable in bytes.
cout << sizeof(int) << endl; // normally prints 4
int k;
cout << sizeof(k) << endl; // normally prints 4
An identifier must start with either:
- a letter (i.e., A to Z, and a to z), or
- the underscore symbol (i.e., _)
- The rest of the character may be _letters (i.e., A to Z, and a to z), digits (i.e., 0 to 9), or The underscore symbol (i.e., )
- C++ is case‐sensitive. So radius, RADIUS, Radius, etc., are different
- Cannot be a keyword in C++
There are various ways to initialize variables; the following shows one way and contrasts with an uninitialized variable.
#include <iostream>
using namespace std;
int main () {
int k = 3; // Declares an integer k and initializes it to 3.
int n; // Declares an integer n with an undetermined value.
char c = ‘Y’ // A character constant is written as a character within single quotes
cout << k; // prints 3
cout << n; // output undetermined
return 0;
}
Sometimes we want to assign a fixed value to a variable, and never change it.
- double PI = 3.1416;
- constant double PI = 3.1416;
- Add in front of a variable declaration to declare the variable as a constant variable. Then, the complier will not allow the program to change its value.
A string is not a primitive datatype; it is a complex datatype. The dot operator is used with complex datatypes to access functionality of that datatype. The code below illustrates how to declare strings and work with them.
#include <iostream>
using namespace std;
int main () {
string s = "hi"; // Declares a string s and initializes it to "hi".
string t; // Declares a string t and initializes it to empty.
cout << s.size(); // Prints "2".
cout << t.size(); // Prints "0".
s += " bob";
cout << s; // Prints "hi bob".
// Use substr(start_index, number_of_chars) to extract substrings.
cout << s.substr(3, 0); // Prints "bo".
return 0;
}
The type char is a primitive type; it represents a single byte that can be treated as a character (letter, number, punctuation). Strings are made up of sequences of chars.
Character literals are demarcated with apostrophes.
char c = 'x';
String literals are demarcated with quotes.
string s = "xyz";
Special characters -- such as the new line character -- start with a backslash and are followed by one or more other characters.
string t = "Dear John,\nHow are you?\n"; // '\n' is a newline character.
To include a backslash in a string you need to escape it with a backslash.
cout << "\\"; // prints a single backslash
To include a quote in a string you need to escape it with a backslash.
cout << "\""; // prints a single quote character
String literals can be spread out into multiple chunks across multiple lines.
cout << "This is " "a single "
"line of characters\n";
- A variable may also be initialized or changed at a later time after its declaration using an assignment statement.
- An assignment statement consists of a variable on the left-hand side of an equal sign, and a value or an expression on the right‐hand side.
variable_name = expression;
int x;
double y;
x = 7; // initializes the value of x to 7
y = 20.5 + 13.22; // 20.5 and 13.22 are examples of the expressions
- Expressions combine variables and constants to produce new values.
- Expressions are composed of operands (data) and operators (instructions).
- Might come in the form of variables or constants
- Specify what is being done to the operands
2 + x / y; // 2, x and y are operands
// '+' and '/' are operators
// 'x / y' is an expression
Declare an integer k and assign a value of 3 to it.
int k;
k = 3;
Declare an integer n and assign the value represented by k to it.
int n;
n = k;
The following show several examples of addition and substraction.
int k = 3;
k = k + 1; // k becomes 4.
k += 2; // k becomes 6.
k -= 2; // k becomes 4
++k; // k becomes 5.
k++; // k becomes 6.
--k; // k becomes 5.
k--; // k becomes 4.
Here are examples of the pre-increment and post-increment operators.
cout << ++k; // Prints 8 because increment is done before k is used.
cout << k++; // Prints 8 because increment is done after k is used.
cout << k; // Prints 9.
Here are some examples of multiplication and division.
int n = 2;
n = n * 2; // n becomes 4
n *= 2; // n becomes 8
n /= 2; // n becomes 4
It's important to keep in mind the difference between floating point division and integer division. If either the left-hand side or the right-hand side of a division are floating point numbers, then you get floating point division. In floating point division, the fractional component is retained.
cout << 5 / 2.0; // Prints 2.5
If the left-hand side and right-hand side of a division are both integers, then you get integer division. In integer division, the fractional component is discarded.
cout << 5 / 2; // Prints 2
Use the modulo operator % to get the remainder from integer division.
cout << 5 % 2; // Prints 1 because 5 divided by 2 produces a remainder of 1.
See table in Operators for operator precedence.