Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C #定义然后printf不工作_C_C Preprocessor - Fatal编程技术网

C #定义然后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") ; ... } #包括 #包括

对于赋值,我必须将变量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") ;
...
}
#包括
#包括
#包括
#包括
#定义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常量内展开宏,那么让一个程序向屏幕写入任何有意义的文本将是多么困难。。。