1. Data Type
Data type is a media or memory on a computer that is used to store. The following are the types of data types:
| Tipe Data | Keterangan | Ukuran | Jangkauan |
|-------------------|-------------------------------------------------------------------------------|--------|-------------------------------------------------------------|
| Char | Untuk menyimpan karakte | 1 byte | signed: -128 to 127 unsigned: 0 to 255 |
| Wchar_t | Untuk menyimpan karakter lebar biasanya untuk unicode | 2 byte | 1 karakter lebar |
| Short int (short) | Untuk menyimpan bilangan bulat dengan jangkauan pendek | 2 byte | signed: -32768 to 32767 unsigned: 0 to 65535 |
| Int | Untuk menyimpan bilangan bulat dengan jangkauan menengah | 4 byte | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
| Long int (long) | Untuk menyimpan bilangan bulat dengan jangkauan panjang | 4 byte | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
| Float | Untuk menyimpan bilangan cacah | 4 byte | 3.4e +- 38 (7 digit) |
| Double | Untuk menyimpan bilangan cacah dengan ketelitian ganda | 8 byte | 1.7e +- 308 (15 digits) |
| Long double | Untuk menyimpan bilangan cacah dengan ketelitian ganda lebih detail (panjang) | 8 byte | 1.7e +- 308 (15 digits) |
| Bool | Untuk pernyataan true or false | I byte | True or false |
To find out the memory size of a data type, you can use the sizeof() function as in the program below:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Cara mengetahui ukuran memori tipe data
*/
void main() {
clrscr();
cout << "Ukuran char : " << sizeof(char) << endl;
cout << "Ukuran int : " << sizeof(int) << endl;
cout << "Ukuran long : " << sizeof(long) << endl;
cout << "Ukuran float : " << sizeof(float) << endl;
cout << "Ukuran double : " << sizeof(double) << endl;
cout << "Ukuran long double : " << sizeof(long double) << endl;
getch();
}
As for the range of integer data types, a complete review is here Range of Integer and unsigned integer data types.
Data types related to integers are char, int, long. While others are related to fractional numbers.
There are also some additional data types owned by Borland C++. These additional data types are by adding the unsigned keyword in front of the data type name.
Unsigned is used when the data to be used is only positive numbers. Here are some examples:
Unsigned Data Type Range
Example Program:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
/**
*bundet.com
*mendeklarasikan beberapa variabel dengan tipe data berbeda
*/
void main() {
clrscr();
char kar1 = 66; char kar2 = 'A';
char kar3[30] = "Stmik El-Rahma Yogyakarta";
cout << "Isi kar1 = " << kar1 << '\n';
cout << "Isi kar2 = " << kar2 << '\n';
cout << "Isi kar3 = " << kar3 << '\n';
int bil_int1 = 32767;
int bil_int2 = 32768;
int bil_int3 = -1;
unsigned int bil_int4 = -1;
cout << "Isi bil_int1 = " << bil_int1 << '\n';
cout << "Isi bil_int2 = " << bil_int2 << '\n';
cout << "Isi bil_int3 = " << bil_int3 << '\n';
cout << "Isi bil_int4 = " << bil_int4 << '\n';
float bil_f1 = 1.23e2; float bil_f2 = 123.0;
cout << "Isi bil_f1 = " << bil_f1 << '\n';
cout << "Isi bil_f2 = " << bil_f2 << '\n';
float bil_f3 = 56.0123456789123456789123456789;
double bil_d1 = 56.0123456789123456789123456789;
long double bil_ld = 56.0123456789123456789123456789;
cout << setprecision(20); // mengatur presisi tampilan
cout << "Isi bil_f3 = " << bil_f3 << '\n';
cout << "Isi bil_d1 = " << bil_d1 << '\n';
cout << "Isi bil_ld = " << bil_ld << '\n';
getch();
}
2. Variables
A variable is a container used to store values or information. The value of a variable can change when the program is run, in other words, a variable is a memory with a certain identity to store values or information that are either fixed or changing.
Declaring Variables
The procedure for implementing variables in the main program must be declared first, the definition of declaration in C++ programming is to initialize variables with the name and data type used, so that later they can be recognized by the program. The method is as follows:
However, sometimes when declaring a variable it is immediately accompanied by its value and sometimes it is not, for example:
int jumlah;
jumlah = 10;
The two statements above can actually be shortened through a definition accompanied by value assignment, as follows:
int jumlah = 10;
Example:
void main(){
Int a=2;
cout << "a= "<<a;
getch();
}
3. Constants
It is a memory identity to store values or information that is permanent and cannot be changed.
Example:
void main(){
const a = 3.14;
cout << "a= "<<a;
getch();
}
Meanwhile, the example program below will provide additional explanations about the implementation of data types, variables and constants.
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Inisialisasi variabel dengan nilai ditentukan
*/
void main(){
int a = 100;
int b = 200;
cout<< "A + B = " <<a+b;
getch();
}
Execution result:
A + B = 300
Giving initials to variables with an expression is also permitted, for example:
float duaphi = 2 * 3.14;
Thus, the value of the variable duaphi is the result of multiplying 2 by phi (3.14).
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Inisialisasi variabel dengan operasi perkalian
*/
void main(){
float duaphi = 2 * 3.14;
cout<< "Isi duaphi = " <<duaphi;
getch();
}
Execution result:
Isi duaphi = 6.28
Variable creation is not always at the beginning of the program, but can be anywhere before being called or operated, as is often applied to the creation of special functions. What is a function? read more here Getting to Know Functions in C++.
Unlike variables, constants are containers that can store fixed values during program execution. To distinguish them from variables, constant names must use CAPITAL letters.
C++ provides 2 ways to create constants.
1. Using the const keyword, for example:
const float PHI = 3.14;
2. Using #define, example:
#define PHI 3.14
The advantage of using #define when compared to const is the speed of compilation, because before compilation is carried out, the compiler first looks for the #define symbol (which is why "#" is called a preprocessor directive) and replaces all Phi with the value 3.14.
Example Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*luas dan keliling lingkaran
*/
void main() {
clrscr();
const float PHI = 3.13;
float jari2, luas, keliling;
cout << "Masukkan nilai Jari-jari : "; cin >> jari2;
luas = 0.5 * PHI * jari2 * jari2;
keliling = 2 * PHI * jari2;
cout << "Luas Lingkaran : " << luas << endl;
cout << "Keliling Lingkaran : " << keliling;
getch();
}
Hope this is useful & happy learning!
Source:
© STMIK El Rahma Yogyakarta. Compiled by Mr. Eding Muhamad Saprudin
Comment 1
- RAHMAT JATNIKA Oct 14, 2015, 09:01:00 thanks bro, very helpful
- AYU IRIANTY Oct 21 2015, 23:38:00 Thank you very much, very useful ^^
- FANNY PUTRA Dec 5 2015, 08:54:00 Thank you very helpful friend^_^
- LIST OF THE LATEST SAD INDONESIAN AND WESTERN SONGS 2016 Apr 7, 2016, 11:24:00 Thank you very much, bro, this is what I was looking for.
- MN ROHMAN 10 Mar 2017, 20:40:00 permission to copy to your blog, bro
- BAYUADISAPUTO 27 Sep 2017, 21:34:00 thank you, lurd
Response 1
- Okay Rahmat J., you're welcome.
- Hello Ayu I. Okay, you're welcome, good luck
- Hello Fanny P. Okay, you're welcome, good luck
- okay, thanks
- Hi MN ROHMAN, please go ahead, as long as you include the source.