teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

質問のタグがC\+\+だったのでCからC\+\+にコードを変更

2017/07/21 17:24

投稿

退会済みユーザー
answer CHANGED
@@ -1,21 +1,24 @@
1
1
  1バイトずつ取り出して%cで表示でしょうか。
2
2
 
3
- ```c
3
+ ```c++
4
+ #include <iostream>
4
- #include <stdio.h>
5
+ #include <string>
6
+ #include <vector>
5
7
 
6
8
  int main()
7
9
  {
8
10
  // Hello, world!
9
- const char ascii[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
11
+ std::vector<char> ascii = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
10
- 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00};
12
+ 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00};
11
13
 
12
- for (int i = 0; i < sizeof(ascii); i++)
14
+ for (const auto& chr : ascii) {
15
+ std::cout << chr;
16
+ }
13
- printf("%c", ascii[i]);
17
+ std::cout << std::endl;
14
18
 
15
- printf("\n");
19
+ std::cout << ascii.data() << std::endl;
16
20
 
17
- printf("%s\n", ascii);
18
-
19
21
  return 0;
20
22
  }
23
+
21
24
  ```