Possible duplicate:warning: format is not a string literal and format arguments
I have a very simple question: why, when I do char[] s = "hi"; printf(s), it gives a warning: "warning: format is not a string literal and format arguments", meanwhile printf("aa")not.
char[] s = "hi"; printf(s)
printf("aa")
I already read the difference between a char array and a string literal (one is const char const*, and the other is char*), but from printf()signature:
const char const*
char*
printf()
http://www.gnu.org/software/libc/manual/html_node/Formatted-Output-Functions.html#Formatted-Output-Functions
I see that it is suitable for any of these types. So my question is, why printf("aaa")not give any warnings (does it somehow verify that the literal is a constant, while the array is not)?
printf("aaa")
Currently, the GNU compiler and many other compilers do check format strings for the printf-family against the given arguments. The compiler warns that it cannot do this for non-literal strings.
printf
Using a non-literal format string is considered bad practice. Using a format string that you do not control is much worse.
, 10-15 , , , printf (s), "s" . , . , puts(). , , printf, "%", printf ( "% s", s);
, , , , :
/* "aa" is not dangerous, so do not display a warning */ printf("aa")
, :
/* the compiler doesn't know the content of the memory region pointed by `s`, so he can't determine if it dangerous or not. Then display a warning */ printf(s)
: , , .
prinrf , ( , , ). , , - - , . ( ) , , (.. ).
prinrf
, -, , , , , puts, printf. (IMHO, puts - C. - ", ", ?)
puts