printとgcc
printc
でコンパイルされたときのプログラムの書き方を誰か教えてもらえますか?g++
c++
5 に答える
9
#ifdef __cplusplus
printf("c++\n");
#else
printf("c\n");
#endif
ファイル拡張子が正しくない場合、問題が発生する可能性があります。
于 2012-09-24T14:28:22.690 に答える
7
このようなもの:
#if __cplusplus
printf("c++");
#else
printf("c");
#endif
コンパイルしg++ -x c
ている場合を除いて、g++でコンパイルしてもCが出力されます。それは落とし穴です。
于 2012-09-24T14:28:04.823 に答える
5
タグの扱いはstruct
C と C++ で異なります
#include<stdio.h>
typedef int T;
int main(void) {
struct T { int a[2]; };
puts((sizeof(T) > sizeof(int)) ? "C++" : "C");
return 0;
}
于 2012-09-24T15:15:29.460 に答える
3
C と C++ の違いの 1 つを使用します。(sizeof(int) == 1 の実装では間違ったことをします)
#include <stdio.h>
int main()
{
printf("c%s\n", (sizeof('a') == 1 ? "++" : ""));
return 0;
}
于 2012-09-24T14:43:35.263 に答える
0
あなたの質問は少し曖昧です。文字通りの意味ではないと思いますhow do you print 'C' or 'C++'
。しかし、私はそれを次のように読んでいますhow do you execute a print in either C or C++ depending on the compiler
それがあなたが求めているものであると仮定すると、これを試すことができます:
#ifdef __cplusplus //If you compiled with g++
#include <iostream> //include c++ headers and namespace
using namespace std;
#define PRINT(str) cout << str; //print the message with cout
#else //If you compiled with gcc
#include <stdio.h> //include c headers
#define PRINT(str) printf(str); //print using printf
#endif
int main(int argc, char *argv[])
{
PRINT("Hello\n"); //Whatever you compiled with, this now works
return 0;
}
于 2012-09-24T14:44:50.670 に答える