在c模式下编译c ++代码

时间:2021-01-23 16:58:27

Following is my code saved as .cpp file and .c file

以下是我的代码保存为.cpp文件和.c文件

in .c it compiled fine, but threw the following error in .cpp

在.c中编译正常,但在.cpp中抛出以下错误

test.cpp:6: error: initializer-string for array of chars is too long
test.cpp:6: error: initializer-string for array of chars is too long

 

#include< stdio.h>

int main()
{

char str[2][2]= { "12", "12"};
int i;

for(i=0; i<2; i++)
printf("%d %s\n", i, str[i]);

return 0;
}

Is there any compiler directive or anything so that the c++ compiler takes this as C code itself.

是否有任何编译器指令或任何东西,以便c ++编译器将其作为C代码本身。

I tried, extern "C", which didn't help.

我试过,extern“C”,没有帮助。

5 个解决方案

#1


0  

Although it won't help your problem, you can select language to compile. With gcc it's -x flag, that needs to be followed by language. Like gcc -x c something.cpp ... will use c compiler to compile.

虽然它对您的问题没有帮助,但您可以选择要编译的语言。使用gcc它是-x标志,需要后跟语言。像gcc -x c something.cpp ...将使用c编译器进行编译。

#2


14  

The character string "12" hold 3 places in C++ (In C too, BTW). You need one more char for the terminating '\0'.

字符串“12”在C ++中保持3个位置(在C中也是BTW)。你需要多一个字符来终止'\ 0'。

char str[2][3]= { "12", "12"};

#3


10  

This would 'fit'

这将“适合”

char str[2][2] = {
  { '1', '2' },
  { '1', '2' }
};

But you want this: https://ideone.com/wZB8F

但你想要这个:https://ideone.com/wZB8F

char str[2][3]= { "12", "12"};

Otherwise, there is no room for the terminating null character

否则,没有空间终止空字符

Equivalent:

char str[2][3]= { { '1', '2', '\0' },
                  { '1', '2', '\0' } };

#4


1  

C++ is a stricter language than C. The problem is that you create an array containing of arrays of two characters, then assign each sub-array three characters (the sequence '1', '2' and the string terminator '\0').

C ++是一种比C更严格的语言。问题是你创建一个包含两个字符数组的数组,然后为每个子数组分配三个字符(序列'1','2'和字符串终结符'\ 0') 。

The array should be declared like this:

该数组应该像这样声明:

char str[2][3]= { "12", "12"};

The C compiler doesn't flag for this, and skips the string-terminator, so your printf statement will most likely print garbage after the string.

C编译器没有为此标记,并跳过字符串终止符,因此您的printf语句很可能在字符串后打印垃圾。

#5


0  

#include< stdio.h>

int main() 
{
    char str[][3]= { "12","12"}; 
    int i;

    for(i=0; i<2; i++) 
    {
        printf("%d %s\n", i, str[i]);
    }

    return 0; 
}

is the version that will work in C++....

是在C ++中工作的版本....

#1


0  

Although it won't help your problem, you can select language to compile. With gcc it's -x flag, that needs to be followed by language. Like gcc -x c something.cpp ... will use c compiler to compile.

虽然它对您的问题没有帮助,但您可以选择要编译的语言。使用gcc它是-x标志,需要后跟语言。像gcc -x c something.cpp ...将使用c编译器进行编译。

#2


14  

The character string "12" hold 3 places in C++ (In C too, BTW). You need one more char for the terminating '\0'.

字符串“12”在C ++中保持3个位置(在C中也是BTW)。你需要多一个字符来终止'\ 0'。

char str[2][3]= { "12", "12"};

#3


10  

This would 'fit'

这将“适合”

char str[2][2] = {
  { '1', '2' },
  { '1', '2' }
};

But you want this: https://ideone.com/wZB8F

但你想要这个:https://ideone.com/wZB8F

char str[2][3]= { "12", "12"};

Otherwise, there is no room for the terminating null character

否则,没有空间终止空字符

Equivalent:

char str[2][3]= { { '1', '2', '\0' },
                  { '1', '2', '\0' } };

#4


1  

C++ is a stricter language than C. The problem is that you create an array containing of arrays of two characters, then assign each sub-array three characters (the sequence '1', '2' and the string terminator '\0').

C ++是一种比C更严格的语言。问题是你创建一个包含两个字符数组的数组,然后为每个子数组分配三个字符(序列'1','2'和字符串终结符'\ 0') 。

The array should be declared like this:

该数组应该像这样声明:

char str[2][3]= { "12", "12"};

The C compiler doesn't flag for this, and skips the string-terminator, so your printf statement will most likely print garbage after the string.

C编译器没有为此标记,并跳过字符串终止符,因此您的printf语句很可能在字符串后打印垃圾。

#5


0  

#include< stdio.h>

int main() 
{
    char str[][3]= { "12","12"}; 
    int i;

    for(i=0; i<2; i++) 
    {
        printf("%d %s\n", i, str[i]);
    }

    return 0; 
}

is the version that will work in C++....

是在C ++中工作的版本....