Search code examples
c++cc-mode

compile c++ code in c mode


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

in .c it compiled fine, but threw the following error in .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.

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


Solution

  • 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.