AnDeriensのブログ

個人的なブログです

C++ノート 変数の初期化

プログラミング歴2年のPHPerがプログラミング力を上げるためにC++を勉強中です。 C++の勉強のために色々試してみて、その記録を残していきます。 今回は、変数の初期化について。

コンパイラ

$ clang++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

実行コマンド

$ clang++ {filename} -o temp && ./temp

<追記> 文字列は配列で作られるってことを忘れて、以下の記事を書いていた。。。 char型は無視してください。

整数0を入れる

コード

#include<iostream>

using namespace std;

int main() {
    int a;
    float b;
    double c;
    short d;
    long e;
    char f;
    bool g;

    a = 0;
    b = 0;
    c = 0;
    d = 0;
    e = 0;
    f = 0;
    g = 0;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
    cout << "e: " << e << endl;
    cout << "f: " << f << endl;
    cout << "g: " << g << endl;
    return 0;
}

出力

a: 0
b: 0
c: 0
d: 0
e: 0
f:
g: 0

考察

整数1を入れてみる

コード

#include<iostream>

using namespace std;

int main() {
    int a;
    float b;
    double c;
    short d;
    long e;
    char f;
    bool g;

    a = 1;
    b = 1;
    c = 1;
    d = 1;
    e = 1;
    f = 1;
    g = 1;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
    cout << "e: " << e << endl;
    cout << "f: " << f << endl;
    cout << "g: " << g << endl;
    return 0;
}

出力

a: 1
b: 1
c: 1
d: 1
e: 1
f: 
g: 1

考察

  • 文字列型だけ、出力されていない
  • コンパイルエラーも起きない

2を入れてみる

コード

#include<iostream>

using namespace std;

int main() {
    int a;
    float b;
    double c;
    short d;
    long e;
    char f;
    bool g;

    a = 2;
    b = 2;
    c = 2;
    d = 2;
    e = 2;
    f = 2;
    g = 2;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
    cout << "e: " << e << endl;
    cout << "f: " << f << endl;
    cout << "g: " << g << endl;
    return 0;
}

出力

a: 2
b: 2
c: 2
d: 2
e: 2
f:
g: 1

考察

  • bool型は1(true)になる

-1を入れてみる

コード

省略

出力

a: -1
b: -1
c: -1
d: -1
e: -1
f:
g: 1

考察

  • マイナスの値でもboolはtrue(当たり前)

小数1.1を入れてみる

コード

#include<iostream>

using namespace std;

int main() {
    int a;
    float b;
    double c;
    short d;
    long e;
    char f;
    bool g;

    a = 1.1;
    b = 1.1;
    c = 1.1;
    d = 1.1;
    e = 1.1;
    f = 1.1;
    g = 1.1;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
    cout << "e: " << e << endl;
    cout << "f: " << f << endl;
    cout << "g: " << g << endl;
    return 0;
}

出力

エラー

init.cpp:14:9: warning: implicit conversion from 'double' to 'int' changes value from 1.1 to 1 [-Wliteral-conversion]
    a = 1.1;
      ~ ^~~
init.cpp:17:9: warning: implicit conversion from 'double' to 'short' changes value from 1.1 to 1 [-Wliteral-conversion]
    d = 1.1;
      ~ ^~~
init.cpp:18:9: warning: implicit conversion from 'double' to 'long' changes value from 1.1 to 1 [-Wliteral-conversion]
    e = 1.1;
      ~ ^~~
init.cpp:19:9: warning: implicit conversion from 'double' to 'char' changes value from 1.1 to 1 [-Wliteral-conversion]
    f = 1.1;
      ~ ^~~
init.cpp:20:9: warning: implicit conversion from 'double' to 'bool' changes value from 1.1 to true [-Wliteral-conversion]
    g = 1.1;
      ~ ^~~
5 warnings generated.

出力

a: 1
b: 1.1
c: 1.1
d: 1
e: 1
f:
g: 1

考察

  • floatとdouble以外はWarningがでる
  • 一応出力までされる

文字列にしてみる

コード

#include<iostream>

using namespace std;

int main() {
    int a;
    float b;
    double c;
    short d;
    long e;
    char f;
    bool g;

    a = "1.1";
    b = "1.1";
    c = "1.1";
    d = "1.1";
    e = "1.1";
    f = "1.1";
    g = "1.1";

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
    cout << "e: " << e << endl;
    cout << "f: " << f << endl;
    cout << "g: " << g << endl;
    return 0;
}

出力

init.cpp:14:9: error: assigning to 'int' from incompatible type 'const char [4]'
    a = "1.1";
        ^~~~~
init.cpp:15:9: error: assigning to 'float' from incompatible type 'const char [4]'
    b = "1.1";
        ^~~~~
init.cpp:16:9: error: assigning to 'double' from incompatible type 'const char [4]'
    c = "1.1";
        ^~~~~
init.cpp:17:9: error: assigning to 'short' from incompatible type 'const char [4]'
    d = "1.1";
        ^~~~~
init.cpp:18:9: error: assigning to 'long' from incompatible type 'const char [4]'
    e = "1.1";
        ^~~~~
init.cpp:19:9: error: assigning to 'char' from incompatible type 'const char [4]'
    f = "1.1";
        ^~~~~
6 errors generated.

考察

  • 何かおかしい
  • ここで文字列は配列で扱われるから、この初期化じゃまずいと気づく

まとめ

  • 整数はだいたいサラッと流れていく。
  • 文字列の扱い方が課題

参考資料

https://tunasalmon.com/2016/07/23/%e5%a4%89%e6%95%b0/#01