C #定义然后printf不工作
对于赋值,我必须将变量N定义为100,然后在printf语句中调用该变量。代码如下所示:C #定义然后printf不工作,c,c-preprocessor,C,C Preprocessor,对于赋值,我必须将变量N定义为100,然后在printf语句中调用该变量。代码如下所示: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <time.h> #define N 100 int main ( ) { ... printf("Try to guess a number between 1 and N \n\n") ; ... } #包括 #包括
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define N 100
int main ( )
{
...
printf("Try to guess a number between 1 and N \n\n") ;
...
}
#包括
#包括
#包括
#包括
#定义N 100
int main()
{
...
printf(“尝试猜测1到N之间的数字\N\N”);
...
}
N
只是显示为N
而不是100
这是因为双引号之间的所有内容都被视为字符数组,即字符串。因此,如果要在字符串中显示N
,应将其用作“常用”变量:
printf("Try to guess a number between 1 and %d \n\n", N) ;
#define
不会在文字字符串(介于“
之间的字符块)中展开。您应该编写:
printf("Try to guess a number between 1 and %d \n\n", N)
退一步,想象一下,如果预处理器真的在char常量内展开宏,那么让一个程序向屏幕写入任何有意义的文本将是多么困难。。。