Skip to content

[C++] Tutorial(1) Data Type

C++ is the first programming language that many people have learned, and it is also the first impression of programming of many people with information background.

I am one of them. I came into contact with VB when I was in high school, but I learned very basic, I only know some basic concepts of program. Later, I went to university, I took a class on programming and then I learn C++.

C++ is the first programming language I am familiar with. However, after I learned C# and Python, and when l was a graduate student, I plunged into the field of machine learning, os I wrote less and less C++.

Since I wanted to review C++ and after I started looking for a job, I needed to practice the whiteboard questions, so I reviewed C++ again and recorded the basic grammar of several chapters.

The record teaching article I currently write is expected to have the following posts:

  • Data Type
  • Arithmetic Operation
  • Data Input and Output
  • Loop and Conditional judgment
  • File Read and Write
  • Pointer
  • Function

This will be a relatively brief teaching note, but my purpose is to review it quickly and quickly review the Data Structure and Algorithm, so I think the notes are actually enough.

And then, I start to introduce some different data type in C++. The data types are the basics of variable declaration.

If you need some clarification of installation of compiler in Linux or Mac OS, may be you can refer: [Linux][C++] How to compile and execute a C++ program


Introduction of Data Type

Data for different purpose will have different data types. For example, numerical data can be roughly divided into Integer (int) and Floating point (float), and the most obvious difference between integer and floating point is IS THERE A DECIMAL.

If the data is text data, use characters or string to store.

Let’s look at a simple example (to use the string type, you need to include first):

#include <iostream>
#include <string>
using namespace std;


int main() {
    int a = 5;
    float b = 3.3;
    char c = 'a';
    string d = "test";

    cout << "int:    " << a << endl;
    cout << "float:  " << b << endl;
    cout << "char:   " << c << endl;
    cout << "string: " << d << endl;

    return 0;
}



Output:

int:    5
float:  3.3
char:   a
string: test


In addition, C++ has the so-called boolean value (bool), which only occupies one byte, with only 1 or 0, which represents true and false respectively.

#include <iostream>
#include <string>
using namespace std;


int main() {
    cout << "Size:  " << sizeof(bool) << endl;
    cout << "true:  " << true << endl;
    cout << "false: " << false << endl;

    return 0;
}



Output:

Size:  1
true:  1
false: 0


It can be seen that as long as it is a bool data type, it only occupies 1 byte, and true means 1 and false means 0.

In addition, C++ also has a structure like Array. Array can store multiple elements of the same data type and can be retrieved any time.

#include <iostream>
using namespace std;


int main() {
    char c_arr[3] = {'a', 'b', 'c'};
    int i_arr[3] = {1, 2, 3};

    cout << c_arr[0] << endl;
    cout << i_arr[2] << endl;

    return 0;
}



Output:

a
3

In fact, the data types of C++ are very finely divided, and the memory used have some difference in different compilers. So we need to be cautious in C++.

In addition, we can even use typedef to define the data type name we need. It can be said that the data type is quite esoteric.


References


Read More

Tags:

Leave a Reply