我正在尝试传递一个数字来scanf表示我想从输入流中读取多少个字符.但是,我无法让它发挥作用.
当我将数字放在代码中时,我的代码可以工作,但是我希望通过数字#define来轻松更改它.
这有效:
#include <stdio.h>
int main(void)
{
char tab[11];
printf("Podaj tekst: ");
scanf("%10[^\n]", tab);
printf("%s", tab);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用(它只在第一个空白字符之前获取字符):
#include <stdio.h>
#define size 10
int main(void)
{
char tab[size+1];
printf("Podaj tekst: ");
scanf("%size[^\n]", tab);
printf("%s", tab);
}
Run Code Online (Sandbox Code Playgroud)
我觉得它不起作用似乎很奇怪.是否有任何解决方法可以做我想要的(除了使用fgets)?
c ×1