Variables in C++

Variables in C++

Requirements:
Linux Distribution
g++
any text editor

My Setup:
Debian 10
g++ version 6.3.0
pluma

In this tutorial, i will be talking about variables in C++. Variables are the
building blocks of a program. Their values determine what what is going on within
a program. There are many types of variables, and categorized as following:

1. Primitive Data Types:
1. int – integer data type represents a 4 byte natural number (whole
number)
2. char – character type data that represents a single byte ASCII character.
3. short int – integer data type represents a 2 byte natural number (whole
number)
4. long int- integer data type represents a 4 byte natural number (whole
number)
5. long long int – integer data type represents a 8 byte natural number (whole
number)
6. bool – boolean data type used to store logical values (true or false)
7. float – floating point number represented by 4 bytes
8. double – double precision floating point number represented by 8 bytes.
9. wchar_t – character type data that represents a 2 or 4 byte UTF-16 (or
other multi-byte encodings) value.
10. void – tecnically this is a data type that returns nothing, you can only
create functions out of this data type, meaning the function will not return a
value.

2. Derived Data types:
1.Functions – Technically are data types which return a value based on the
input they are received (or void if otherwise).
2. Arrays – When you have a certain amount of a specific data type you would
like to contain in a single area, using arrays is an excellent way to do so,
as long as you know the amount being stored at runtime.
3. Pointers – Pointers are simply a variable whose value is a memory address
of that specific type of pointer. These are treated the same way as they are
in C , except you need to type-cast if you are trying to manipulate it via
void pointer. They are declared with the “*” character.
4. References – These act as an alias to another object or value. I think of
these as safe pointers, because you can not change what you are referencing afte
declaration.They are declared with the “&” character. Ill get more into these in
another tutorial.

3. User-Defined Data Types:
1. class – creates a blueprint to be used to create objects. The data in a
class is private by default.
2. struct – creates a plain-old-data type (POD type) with the data you give
it. in C++, they act the same as a class, however the data is public by default.
3. union – this data type isnt used as often as the others, this type contains
multiple other types defined by the user, and is the size of the biggest data
type in it. It is used to store multiple data types in one area.
4. enum – This is a distinct type whose value is restricted to the range of
the values given. In c++, you can actually define the primitive data type it is
stored in.
5.typedef – This keyword will define a datatype to a new datatype.

Along with the primitive types, you also have datatype modifiers. These are:
1. signed – The value is either a positive or negative value .
-2^(8*size)/2 – 2^((8*size)/2)-1
2. unsigned – The value’s range is from 0 – 2^(8*size)
3. short – this tells the compiler to use a 2 byte data type rather then 4.
4. long – using two longs in succession tell the compiler to use an 8 byte
data type rather then 4. If used with a double ( “long double foo;”) the data
will be 12 or 16 bytes

Keep in mind the primitive types are sometimes platform specific. The size of
longs can be different depending on what linux distribution you are using, and
its Application Binary Interface (ABI).

Today we will be exploring the sizes of each primitive data type. Start off by
creating your project folder, and main.cpp, or however you do it , and we will
start off by writing out some data types:

#include <iostream>
int main(int argc, char **argv){

  int integer = 5;
  long int longInt = 1000000;
  short int shortInt = 1023;
  long long int longLongInt = 2147044090;
  char character = 'b';
  unsigned char uchar = 12;
  unsigned int uint = 150;
  float floatingPoint = 3.15f;
  double doublePrecision= 3.14431654262;
  long double longDouble = 15.21348594643163;
  wchar_t wideChar = L'H';

Now im going to output all the variables, their values, and sizes. Check these
out with your compiler as they may be different on your computer.

std::cout << "integer value : " << integer
          << " size: " << sizeof(integer) << std::endl;
std::cout << "longInt value : " << longInt
          << " size: " << sizeof(longInt) << std::endl;
std::cout << "shortInt value : " << shortInt
          << " size: " << sizeof(shortInt) << std::endl;
std::cout << "longLongInt value : " << longLongInt
          << " size: " << sizeof(longLongInt) << std::endl;
std::cout << "character value : " << character
          << " size: " << sizeof(character) << std::endl;
std::cout << "uchar value : " << uchar
          << " size: " << sizeof(uchar) << std::endl;
std::cout << "uint value : " << uint
          << " size: " << sizeof(uint) << std::endl;
std::cout << "floatingPoint value : " << floatingPoint
          << " size: " << sizeof(floatingPoint) << std::endl;
std::cout << "double value : " << doublePrecision
          << " size: " << sizeof(doublePrecision) << std::endl;
std::cout << "longDouble value : " << longDouble
          << " size: " << sizeof(longDouble) << std::endl;
std::cout << "wideChar value : " << wideChar
          << " size: " << sizeof(wideChar) << std::endl;

return 0;
}

These were my results:

clim@debian:~/Desktop/Website/C++ Programming/Example 2$ g++ main.cpp
clim@debian:~/Desktop/Website/C++ Programming/Example 2$ ./a.out
integer value : 5 size: 4
longInt value : 1000000 size: 8
shortInt value : 1023 size: 2
longLongInt value : 2147044090 size: 8
character value : b size: 1
uchar value :
               size: 1
uint value : 150 size: 4
floatingPoint value : 3.15 size: 4
double value : 3.14432 size: 8
longDouble value : 15.2135 size: 16
wideChar value : 72 size: 4
clim@debian:~/Desktop/Website/C++ Programming/Example 2$

I notice that the wide character is represented by an integer from within cout
statements. Interesting. Also i couldn’t output the value of the unsigned
character.Maybe i typed a null character and it represented it as a char?
Anyways, these are used to represent variables your program may contain. Maybe
you need a counter for the amount of times you went through a prompt? you can
keep track of that with an unsigned int , since the result will never need to
go under 0.
Primitive data types are normally manipulated via operators such as ‘+’,’-‘,
‘*’,’/’,’%’, or ‘=’. For example :

#include <iostream>

int main(int argc, char **argv){

int a =5;
int b = 6;

int add = a + b;
int subtract = a - b;
int multiply = a * b;
float divide = ((float)a /b); //this is gonna produce a value which is incorrect
                              // as an int. Probably would truncate to either 0
                              //or 1
int modulus = a % b;          //  the remainder after division

  std::cout << "a = " << a << std::endl;
  std::cout << "b = " << b << std::endl;
  std::cout <<"Results :" << std::endl;
  std::cout << "Add : " << add << std::endl << "Subtract :" << subtract <<
            std::endl << "Multiply: " << multiply << std::endl << "Divide: "
            << divide << std::endl << "Modulus: " << modulus << std::endl;

  return 0;
}
clim@debian:~/Desktop/Website/C++ Programming/Example 2$ g++ main.cpp
clim@debian:~/Desktop/Website/C++ Programming/Example 2$ ./a.out
a = 5
b = 6
Results :
Add : 11
Subtract :-1
Multiply: 30
Divide: 0.833333
Modulus: 5
clim@debian:~/Desktop/Website/C++ Programming/Example 2$

Also, you can you can use the cin object from the standard output to get input
from the console as such:

#include <iostream>

int main(int argc, char **argv){

  int a ;
  std::cout << "Pick a Number: ";
  std::cin >> a;
  std::cout << "You Picked: " << a << std::endl;
  return 0;
}

So go on and try out creating primitive data types, maybe create a calculator
that will do basic arithmetic for you! I will catch you in the next tutorial
when we go over conditional statements !

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s