C++でC言語の関数を利用する

提供: C++入門
移動: 案内検索
スポンサーリンク


概要

C++ は、C言語の関数(printf や exit など)やマクロを利用できます。

C言語では、Cの標準関数を利用するために、 stdio.h や stdlib.h をインクルードします。 C++ では、それらのヘッダファイルの代わりに、cstdio や cstdlib をインクルードします。

time.h は ctime 、 stdint.h は cstdint 、 stdarg.h は cstdarg などが用意されています。

ヘッダファイル

FreeBSD 9.0-RELEASE の g++ 4.2.1 の環境では、以下のヘッダファイルが用意されています。

% cd /usr/include/c++/4.2/tr1/
% ls c*
cctype     cinttypes  common.h   cstdbool   cstdlib    ctype.h
cfenv      climits    complex    cstdint    ctgmath    cwchar
cfloat     cmath      cstdarg    cstdio     ctime      cwctype

ソースコード c.cpp

#include <cstdio>
#include <cstdlib>
 
int
main (int argc, char *argv[]) {
	(void) printf ("C++\n");
	exit (EXIT_SUCCESS);
}

コンパイル

g++ c.cpp

実行例

% ./a.out
C++

関連項目




スポンサーリンク