アットウィキロゴ

基本

基本

c++ではプログラムの開始はmain関数から始まります。
以下のサンプルはiostreamライブラリのcoutを使用して標準出力を行います。
  1. // sample01-01.cpp : メイン プロジェクト ファイルです。
  2.  
  3. #include "stdafx.h"
  4.  
  5. // ライブラリ読み込み
  6. #include <iostream>
  7.  
  8. using namespace System;
  9. using namespace std;
  10.  
  11. int main(array<System::String ^> ^args)
  12. {
  13. // テスト
  14. cout << "Hello, World!1";
  15. cout << "Hello, World!2" << endl;
  16. return 0;
  17. }
  18.  
  19.  

名前空間

ライブラリ間で同じ命令が重複しないようにするために、ライブラリの関数を使用する場合は
名前空間を指定する必要があります。
しかし、using namespaceを定義することで名前空間を省略することも可能
  1. // sample01-02.cpp : メイン プロジェクト ファイルです。
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5.  
  6. // using namespaceに定義した場合は省略が可能
  7. using namespace System;
  8. using namespace std;
  9.  
  10.  
  11. int main(array<System::String ^> ^args)
  12. {
  13. // namespaceを指定して出力
  14. std::cout << "Hello world-01" << std::endl;
  15.  
  16. // 上記で指定しているので省略する
  17. cout << "Test Hello" << endl;
  18.  
  19. // 返却
  20. return 0;
  21. }
  22.  

コメント

指定の行のみをコメントにしたい場合は「//」を文の先頭に設定
複数の範囲をコメントにしたい場合は「/* ~ */」を設定
  1. // sample01-03.cpp : メイン プロジェクト ファイルです。
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5.  
  6. using namespace System;
  7.  
  8. int main(array<System::String ^> ^args)
  9. {
  10.  
  11. std::cout << "Test Message1" << std::endl;
  12.  
  13. // ↓はコメント行
  14. // std::cout << "Test Message2" << std::endl;
  15. std::cout << "Test Message3" << std::endl;
  16.  
  17. // ↓はコメント行
  18. //std::cout << "Test Message4" << std::endl;
  19.  
  20. std::cout << "Test Message5" << std::endl;
  21.  
  22. /* ここからコメント
  23.   std::cout << "Test Message6" << std::endl;
  24.   std::cout << "Test Message7" << std::endl;
  25.   ここまでコメント */
  26.  
  27. std::cout << "Test Message8" << std::endl;
  28.  
  29. return 0;
  30. }
  31.  

文字、数値

文字の場合は'(シングルクォート)で挟む
文字列の場合は"(ダブルクォート)で挟む
数値の場合はなし
  1. // sample01-04.cpp : メイン プロジェクト ファイルです。
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5.  
  6. using namespace System;
  7.  
  8. int main(array<System::String ^> ^args)
  9. {
  10. // 文字を出力
  11. std::cout << "文字:" << 'a' << std::endl;
  12.  
  13. // 文字列を出力
  14. std::cout << "文字列リテラルを出力:" << "あああ" << std::endl;
  15.  
  16. // 数値リテラルを出力
  17. std::cout << "数値リテラルを出力:" << 9999 << std::endl;
  18.  
  19. return 0;
  20. }
  21.  

エスケープシーケンス

改行やタブ、¥、”などを表示したい場合はそのままでは出力できないので
エスケープシーケンスとして出力する必要がある
  1. // sample01-05.cpp : メイン プロジェクト ファイルです。
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5.  
  6. using namespace System;
  7.  
  8. int main(array<System::String ^> ^args)
  9. {
  10.  
  11. // 改行出力
  12. std::cout << "あああ\nいいい\nううう" << std::endl;
  13.  
  14. // タブ
  15. std::cout << "かかか\tききき\tくくく" << std::endl;
  16.  
  17. // ダブルクォートおよび¥
  18. std::cout << "\"c:\\test.dat\"" << std::endl;
  19.  
  20. /* 出力結果サンプル
  21. あああ
  22. いいい
  23. ううう
  24. かかか ききき くくく
  25. "c:\test.dat"
  26. */
  27.  
  28. return 0;
  29. }
  30.  
  31.  
エスケープシーケンス一覧
エスケープシーケンス 説明
¥n 改行
¥t タブ
¥b バックスペース
¥r キャリッジリターン
¥f ページフィールド
¥’ シングルクォーテーション
¥” ダブルクォーテーション
¥0 NULL
¥¥ 円記号
¥? クエスチョンマーク
¥a ベル音(alert)
¥xhh 16進拡張
¥ooo 8進拡張


最終更新:2011年09月02日 11:01