user-0 提问: 请问一下 #define IPP 4.3 定义宏用printf输出IPP为什么不可以

回复:
user: user-1
content: 预编译期 编译期 运行期

user: user-2
content: #define是预编译时期进行的展开工作，如果用printf 打印 PI，预编译阶段 会将PI展开为3.14，就相当于3.14是常量，用printf 打印可能么 ------------------------------------------------------------------------- 一般规范的做法是一个xxx.cpp对应一个xxx.h，头文件之间这样包含，容易糊涂，一般头文件经常使用#ifndef...#define....#endif来防止多个头文件之间重复包含 ----------------------------------------------------------------------------- 另外头文件里面一般不定义变量，可以声明变量，  extern 变量类型  a;   //说明a在其他文件中定义。

user: user-3
content: 问题2, 不要在头文件中定义变量， 这个在随便一本C编程教材应该都有讲。

user: user-3
content: 引用 3 楼 qq_15944771 的回复:#include <stdio.h> #define IPP 3.1 void main() { 	#if IPP>2.0 	printf("大于2.0\n"); 	#endif  }  fatal error C1017: invalid integer constant expression  我个是为啥啊  上边第一题我从建了个文件用printf从新输出结果可以输出了 可能是原来的文件太乱了？ 求老师辅导  fatal error C1017: invalid integer constant expression，很清楚吗， #if 需要整型常量

user: user-4
content: 目测lz是自学的。。。

user: user-0
content: 赵老师还有第二个问题 您能说下？

user: user-0
content: 赵老师那个#if为啥不可以这样用啊， 我看说#if 后边的为真 就执行下边的    我就放了个条件表达式

user: user-5
content: 预编译和编译不是一回事。

user: user-1
content: 引用 6 楼 u013163178 的回复:[quote=引用 2 楼 zhao4zhong1 的回复:] 编译选项加/EP /P，重新编译，查看宏展开后对应的.i文件。gcc加-E #define IPP 4.3 printf("%lg\n",IPP);赵老师，%lg是什么意思[/quote] printf Type Field Characters The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of  c and s with printf functions, are Microsoft extensions and are not ANSI-compatible.   Table R.3   printf Type Field Characters  Character Type Output Format  c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.  C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.  d int Signed decimal integer.  i int  Signed decimal integer.  o int  Unsigned octal integer.  u int  Unsigned decimal integer.  x int Unsigned hexadecimal integer, using “abcdef.”  X int Unsigned hexadecimal integer, using “ABCDEF.”  e  double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –.  E double Identical to the e format except that E rather than e introduces the exponent.  f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.  g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.  G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).  n  Pointer to integer  Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument.  p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits.  s String  When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.  S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.

user: user-0
content: 引用 9 楼 zxh707wk 的回复:目测lz是自学的。。。    啊

user: user-0
content: 引用 11 楼 mujiok2003 的回复:问题2, 不要在头文件中定义变量， 这个在随便一本C编程教材应该都有讲。   恩我看过别人这么说   但是我想把这个包含的搞明白 怎么样才能不出错  有的人用extern定义的这个变量  还有加#ifndef  头文件名。。。。。。

user: user-6
content: 引用 2 楼 zhao4zhong1 的回复:编译选项加/EP /P，重新编译，查看宏展开后对应的.i文件。gcc加-E #define IPP 4.3 printf("%lg\n",IPP);赵老师，%lg是什么意思

user: user-7
content: #if  后面只能整型比较

user: user-1
content: #if和if不是一回事！

user: user-0
content: #include <stdio.h> #define IPP 3.1 void main() { 	#if IPP>2.0 	printf("大于2.0\n"); 	#endif  }  fatal error C1017: invalid integer constant expression  我个是为啥啊  上边第一题我从建了个文件用printf从新输出结果可以输出了 可能是原来的文件太乱了？ 求老师辅导

user: user-1
content: 编译选项加/EP /P，重新编译，查看宏展开后对应的.i文件。gcc加-E #define IPP 4.3 printf("%lg\n",IPP);

user: user-0
content: 刚才看到点东西，那个第一个问题是因为 define定的的宏 没有变量类型所以不能printf？？？？？   怎么没有人啊！！！！