The first array prints all right.
test[] =
However, when the array is like in the second example, test[][] ={"...","..."};
I get an incompatible warning.
In the second example with fputs, it prints without a line break and with printf, nothing prints.
#include <stdio.h>
//String to display Test
const char test[] =
"|T|\n\
|E|\n\
|S|\n\
|T|\n\
";
int main(){
//Print test
fputs(test,stdout);
printf("\n");
return 0;
}
Second example.
#include <stdio.h>
#define LINE 4
#define COLN 5
//String to display Test
const char test[LINE][COLN] ={
" |T| ",
" |E| ",
" |S| ",
" |T| "
};
int main(){
// Print test
//printf("%s", test);
fputs(test,stdout);
printf("\n");
return 0;
}
Warnings
_______________________________________________________________________________
field width should have type 'int', but argument has type 'const char (*)[11]'
[-Wformat] printf("%*s",test);
~^ ~~~
_______________________________________________________________________________
incompatible pointer types passing 'const char[11][11]'
to parameter of type 'const char ' [-Wincompatible-pointer-types]
fputs(test,stdout);
^~~~
passing argument to parameter '__s' here
int fputs(const char __s, FILE* __fp);
^
In the lines
you are defining an array of 4
chararrays which are not null-terminated (because you defined the array in such a way that there is no room for a null terminating character).Therefore, in order to print any of these 4
chararrays, you cannot usefputs. You can, however, use the%sconversion format specifier withprintf, but you must limit the number of characters written, for example by using the following syntax:See the documentation for
printffor further information.If you also want to be able to use
fputson the individualchararrays, then you will have to define thechararrays in such a way that there is room for a terminating null character, for example like this:However, in order to print all 4
chararrays, you cannot simply use the lineas you are doing in your code, because you can only print one string at a time. Therefore, you must write
in order to print all 4 strings, or better, use a loop:
Regarding your first example, it is worth noting that
is not good coding style, because it would be better to indent all lines except for the first one. You are probably not doing this because this would cause the indentation to become part of the string. However, this problem can be solved by writing the following instead:
The preprocessor will concatenate all 4 string literals into a single string literal in phase 6 of the translation process.