1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C++】基礎を学ぶ④~変数の宣言と値の代入、変数の型~

Last updated at Posted at 2022-07-09

変数の宣言

変数の宣言は、「変数の型」と「変数名」を書きます

int value;

値の代入

宣言した変数に値を代入する方法です

value = 10;

変数の初期化

変数の宣言と値の初期化を同時に行います

int value = 10;

プログラム

#include <iostream>

using namespace std;

int main(){
  int value1;
  value1 = 10;
  cout << value1 << endl;

  int value2 = 20;
  cout << value2 << endl;
  return 0;
}
10
20

変数の型について

変数の「型」とは、変数の「種類・データの大きさ」を表す
「データ型」とも呼ばれる

int型の場合

種類は「整数」
データの大きさは「4バイト(最大値は2147483647、最小値は-2147483648)」

よく使う型

大きさ 種類
char 1バイト 文字
int 4バイト 整数
double 8バイト 小数

型変換(キャスト)について

型変換とは、データ型を変数の宣言時と別の型にすることです
変数の前に変換先の型を()で囲って書く

doubleからintに変換する場合

#include <iostream>

using namespace std;

int main(){
  double value = 1.5;
  cout << value << endl;
  cout << (int)value << endl;
  return 0;
}
1.5
1

次の記事

【C++】基礎を学ぶ⑤~複合代入演算子、インクリメント~

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?