相关疑难解决方法(0)

定义常量的数据类型是什么?

我正在尝试这段代码.

#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()中指定不同的类型说明符,为什么会出错呢?

c

7
推荐指数
1
解决办法
7589
查看次数

可变参数函数参数的自动类型促销是什么?

请考虑以下代码段:

#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时,允许使用哪些类型,如何知道使用哪种类型以及避免此类运行时错误.

c runtime-error variadic-functions

4
推荐指数
2
解决办法
3948
查看次数

标签 统计

c ×2

runtime-error ×1

variadic-functions ×1