ShopDreamUp AI ArtDreamUp
Deviation Actions
Literature Text
Chapter 2 - Data Types
You can now get input to make your programs reuseable and give
output to show the results. The next most important thing is
to be able to use the input, but before you get to that you
need to learn about the data types.
All types of data have a number of bytes of memory assigned
to them. This memory comes from the RAM, and you only have
so much to work with. Many default data types have a signed
and unsigned version, the differences being the unsigned cannot
be negative. To make a signed value you put the "unsigned"
key word infront of the declaration.
Data Types: <size in bytes> | <value range> | <unsigned values>
char 1 | -128 to 127 | 0 to 255
A one byte ANCII character.
short 2 | -32768 to 32767 | 0 to 65535
A short integer.
int 2 or 4 | -32768 to 32767 or -2147483648 to 2147483647 | 0 to 65535 or 0 to 4294967295
An integer value whose size varies with the system.
long 4 | -2147483648 to 2147483647 | 0 to 4294967295
A long integer.
bool 1 | 0 to 1
A boolean that reads true (1) or false (0).
wchar_t 2 | 65535 character values.
Two byte unicode character.
float 4 1.2e-38 to 3.4e38
4 byte decimal value.
double 8 | 2.2e-308 to 1.8e308
8 byte decimal value.
long double 10 | 3.4e-4932 to 1.1e4932
10 byte decimal value.
Now it is time to show somethings with them.
Data Program
data.cpp:
-----
#include <iostream>
using std::cin;
using std::cout;
int main() {
char c;
unsigned short a;
bool b;
float f;
cout << "Enter a letter, a number greater than -1, 1 or 0, and a decimal: ";
cin >> c >> a >> b >> f;
if (b == 0) {
c = '\';
a += 50;
} else {
c = 'a';
f = a / 1000;
}
cout << "The output is: " << c << ' ' << a << ' ' << f << 'n';
cout << "Enter a letter and the program will end: ";
cin >> c;
return 0;
}
-----
Now, lets review. I am boring you, correct? If you make it to the next
chapter you may have what it takes to be a programmer.
Include files, declare using, int main... then we get to declaring the
varialbles. Then we get user input. Then we have an "if" statement that
will be covered in chapter 4. Then we have operators which are next
chapter. Then we return out put. End function. The '' marks cause the
symbol inside to be treated a char value. The 'n' is a special one,
I will list a few like it now.
Special character codes:
null
\ Backslash
r carriage return
n new line (enterreturn)
t tab
a ring alarm bell
There are others, all of which can be found on Wikipedia by searching
"ANCII". For once Wiki is right.
Now, a bit about legal variable names. Their names must start with an
letter of a '_' symbol. The only valid characters for the names are
numbers and the characters that it is allowed to start with.
You are now ready to move on to more exciting things. On to
functions and operators.
You can now get input to make your programs reuseable and give
output to show the results. The next most important thing is
to be able to use the input, but before you get to that you
need to learn about the data types.
All types of data have a number of bytes of memory assigned
to them. This memory comes from the RAM, and you only have
so much to work with. Many default data types have a signed
and unsigned version, the differences being the unsigned cannot
be negative. To make a signed value you put the "unsigned"
key word infront of the declaration.
Data Types: <size in bytes> | <value range> | <unsigned values>
char 1 | -128 to 127 | 0 to 255
A one byte ANCII character.
short 2 | -32768 to 32767 | 0 to 65535
A short integer.
int 2 or 4 | -32768 to 32767 or -2147483648 to 2147483647 | 0 to 65535 or 0 to 4294967295
An integer value whose size varies with the system.
long 4 | -2147483648 to 2147483647 | 0 to 4294967295
A long integer.
bool 1 | 0 to 1
A boolean that reads true (1) or false (0).
wchar_t 2 | 65535 character values.
Two byte unicode character.
float 4 1.2e-38 to 3.4e38
4 byte decimal value.
double 8 | 2.2e-308 to 1.8e308
8 byte decimal value.
long double 10 | 3.4e-4932 to 1.1e4932
10 byte decimal value.
Now it is time to show somethings with them.
Data Program
data.cpp:
-----
#include <iostream>
using std::cin;
using std::cout;
int main() {
char c;
unsigned short a;
bool b;
float f;
cout << "Enter a letter, a number greater than -1, 1 or 0, and a decimal: ";
cin >> c >> a >> b >> f;
if (b == 0) {
c = '\';
a += 50;
} else {
c = 'a';
f = a / 1000;
}
cout << "The output is: " << c << ' ' << a << ' ' << f << 'n';
cout << "Enter a letter and the program will end: ";
cin >> c;
return 0;
}
-----
Now, lets review. I am boring you, correct? If you make it to the next
chapter you may have what it takes to be a programmer.
Include files, declare using, int main... then we get to declaring the
varialbles. Then we get user input. Then we have an "if" statement that
will be covered in chapter 4. Then we have operators which are next
chapter. Then we return out put. End function. The '' marks cause the
symbol inside to be treated a char value. The 'n' is a special one,
I will list a few like it now.
Special character codes:
null
\ Backslash
r carriage return
n new line (enterreturn)
t tab
a ring alarm bell
There are others, all of which can be found on Wikipedia by searching
"ANCII". For once Wiki is right.
Now, a bit about legal variable names. Their names must start with an
letter of a '_' symbol. The only valid characters for the names are
numbers and the characters that it is allowed to start with.
You are now ready to move on to more exciting things. On to
functions and operators.
Comments0
Join the community to add your comment. Already a deviant? Log In