我正在尝试这段代码.
#include <stdio.h>
#include <stdlib.h>
#define LOWER 0
#define UPPER 300
int main()
{
printf("%d %f",LOWER,UPPER);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我读了一些答案,它说定义的常量没有类型,也没有分配任何内存.那么,如果我在printf()中指定不同的类型说明符,为什么会出错呢?
请考虑以下代码段:
#include <stdio.h>
#include <stdarg.h>
void display(int num, ...) {
char c;
int j;
va_list ptr;
va_start(ptr,num);
for (j= 1; j <= num; j++){
c = va_arg(ptr, char);
printf("%c", c);
}
va_end(ptr);
}
int main() {
display(4, 'A', 'a', 'b', 'c');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序给出了运行时错误,因为vararg自动将char提升为int,在这种情况下我应该使用int.
当我使用vararg时,允许使用哪些类型,如何知道使用哪种类型以及避免此类运行时错误.