Hello Variables

In previous lecture we use our first variable std::string name = "C++"; and std::string greeting

int main(){ 
	std::string name = "C++";
	std::string greeting = "Hello " + name;
	std::cout << greeting << std::endl;
} 
  • std::string -> variable type
  • name -> variable name
  • = -> assignment operator
  • “C++” -> value

Numbers

We can store our text data inside string. If we want to store a number we can use int or float

#include <iostream>
using namespace std;

int main() {
	int number_of_students = 30;
	float average_grade_of_students = 62.8;

	cout << "       number_of_students : " << number_of_students << endl;
	cout << "average_grade_of_students : " << average_grade_of_students << endl;

}
/* Output:
       number_of_students : 30
average_grade_of_students : 62.8
*/

int use 4 byte of memory and also float use 4 byte of memory BUT integer holds integer values like -5,0,1,2,128000000000, … in contrast float hold floating point numbers like 0.45, 3.14, 24314.2, … Both of them only use Zeros and Ones.

You can ask “If they are use same zeros and ones why they hold different values?” Because we use different represents. Think Latin alphabet. We can represent different languages with same letters.

This is the main idea of variables. We have data in memory and we interpret it from different perspective.

Size in Bytes

We have different types besides int for represent integers. The difference is the memory that they use. If a variable use more memory space. It can represent more data. For example a char only hold 28 dinstinct value. A int can hold 232 dinstinct value

#include <iostream>
using namespace std;

int main() {
	char c; 
	short s;
	int i;
	long l;
	long long ll;

	cout << sizeof(c) << endl;	 // 1
	cout << sizeof(s) << endl;	 // 2
	cout << sizeof(i) << endl;	 // 4
	cout << sizeof(l) << endl;	 // 4
	cout << sizeof(ll) << endl;	 // 8
}

We can use sizeof() operator (it is not a function) for learning size in bytes of a variable

Characters

A is a character we can store it inside char:

	char c = 'A';
	cout << c << endl; // A 

We can store an integer inside a char 🙂 Because every character has an integer value also. For understand the concept we need to look up ASCII:

ASCII (/ˈæskiː/ASS-kee),[3]: 6  abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because of technical limitations of computer systems at the time it was invented, ASCII has just 128 code points, of which only 95 are printable characters, which severely limited its scope. Modern computer systems have evolved to use Unicode, which has millions of code points, but the first 128 of these are the same as the ASCII set.

https://en.wikipedia.org/wiki/ASCII

If we know the value of upper and lower cases we can convert it to each other

	cout << (char)65 << endl; // A 
	cout << (char)(65 + 32) << endl; // a 

Convert a given lowercase string to upper case:

#include <iostream>
using namespace std;

int main() {
	string name = "micheal";
	
//Apply same operation for every character of name 
	for (auto& i : name) 
		i = i - 32; 

	for (auto& i : name)
		cout << i;
	cout << endl;
	
}
// Output:
// MICHEAL


  • We can store data inside a variable
  • The computer stores data by Zeros and Ones
  • We can represent different types by 0 & 1:
    • Integers: 1, 3, -23423, 99, 0, …
    • Floating point numbers: 0.5555, 3.14, 51.61, …
    • Characters: A, B, Z, W, …
    • Booleans: True, False
    • Car brands, geometric shapes, music, images, …
  • There are two types of variables
    • Primitive data types: The data types which defined by programming language.
    • Non-primitive data types: The data types which defined by user -programmers-.
  • Computer stores data inside the memory in other words RAM (Random access memory).
  • Different data types hold different spaces. We can represent an integer with 1 byte, also we can represent an integer by 4 byte.
  • The difference is the capacity.
    • 1 byte can hold 256 distinct value
    • 4 byte can hold 2^32 distinct value
  • There are some naming rules for variables:
    • The name can start with an underscore _ or a letter
      • myvar
      • my_var
      • _my_var
      • 2929my_var
      • $my_var
    • The name can contain a letter, an underscore, or a number
      • my_var2
      • var3
    • The name is case senstive
      • myvar
      • myVar
      • MyVar
      • MYvar
      • All of them are different for C++
    • The name cannot contain whitespace
      • my var
      • my var
    • The name cannot be a reserved word
      • sizeof
      • for
      • not
#include <iostream>

using namespace std;

int main() {

	// INT
	
	// There are three siblings which are 5,15,27. What is the average of their ages? 
	// initialize variables
	int s1 = 5;
	int s2 = 15;
	int s3 = 27;
	int total_age = s1 + s2 + s3;
	int average_age = total_age / 3;

	cout << "Average age of siblings:" << average_age << endl << endl;
	// Average age of siblings:15 //Why the result is wrong? 



	// Area of an rectangle 
	int a, b, area; // we declare but we dont initialize. 

	a = 5; // we assing 5 to a variable 
	b = 10; // we assing 10 to b variable 
	area = a * b; // we assing the multiplication of a and b to area variable 

	cout << "The area of the rectangle:" << area << endl << endl;
	// The area of the rectangle : 50



	// CHAR
	char achar = 'W'; // we can store single char inside a char variable 
	char astring[] = "e can store multiple char inside a char array";
	cout << achar << astring << endl;
	// We can store multiple char inside a char array

	cout << "We can use char as a number: " << (int)achar << endl << endl;
	// We can use char as a number: 87


	// Example: 
	// Look up ASCII table and turn following word to upper case
	char c1 = 'c', c2 = 'p', c3 = 'p';
	c1 = c1 - 32; // find the result of c1 - 32 and assign it to c1 
	c2 = c2 - 32;
	c3 = c3 - 32;

	cout << c1 << c2 << c3 << endl; //CPP


	// Instead of using three variable we can use one array 
	char name[] = "cpp";
	name[0] = name[0] - 32; // index of first element is 0 
	name[1] = name[1] - 32; // second element
	name[2] = name[2] - 32; // third element

	cout << name << endl; //CPP

}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *