数组c - 警告:格式字符串不是一个字面 [英] c array - warning: format not a string literal
本文介绍了数组c - 警告:格式字符串不是一个字面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图学习C和已经我碰到的一个问题。我认为它微不足道,但我需要知道它。我已经写:
I'm attempting to learn C and already I've run into an issue. I assume its trivial but I need to know it. I have written:
#include <stdio.h>
#include <string.h>
int main()
{
char str_a[20];
strcpy(str_a, "Hello, world!\n");
printf(str_a);
}
有一次,我试着用编译:gcc的-g -o char_array2 char_array2.c我收到一个错误说:
Once I attempt to compile it with: gcc -g -o char_array2 char_array2.c I receive an error saying:
char_array2.c: In function ‘main’:
char_array2.c:9:2: warning: format not a string literal and no format arguments [-Wformat-security]
谁能帮助吗?
推荐答案
在用printf,格式串最好是一个字符串,而不是一个变量:
When using printf, the format string is better be a string literal and not a variable:
printf("%s", str_a);
这篇关于数组c - 警告:格式字符串不是一个字面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文