Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
void output(int number)
{
    int air_temp;
    int speed_of_sound;

    air_temp = main();

    speed_of_sound = 1086 * sqrt(((5.0 * air_temp) + 297.0) / 247.0);


    printf(" \nThe speed of sound at that temperature is %d", speed_of_sound,
    "ft/sec.");

    return;
}


说我在第二个打印行中有太多关于格式的参数。我是编码方面的新手,这让我陷了几个小时...

最佳答案

您使用的printf()不正确。

你可能是说

printf(" \nThe speed of sound at that temperature is %d %s", speed_of_sound, "ft/sec.");


要不就

printf(" \nThe speed of sound at that temperature is %d ft/sec", speed_of_sound);


printf()的原型就像

int printf( const char *format, ... );​


其中format是格式字符串,其余参数指定要打印的数据。

http://en.cppreference.com/w/c/


  如果默认参数提升之后的任何参数都不是相应的转换说明符期望的类型,或者参数的数量少于格式要求的数量,则该行为是未定义的。如果参数超出格式要求的数量,则会评估并忽略无关的参数。


在使用printf()时,格式字符串(即%d)中只有一个格式说明符,而您要提供两个要打印的数据。第二项是字符串"ft/sec"。由于参数数量超出了格式要求,因此多余的参数将被忽略。

但是更严重的问题是您在main()中使用output()
您尚未指定何时停止递归。调用output()后,它将一遍又一遍地调用main(),导致无限递归。

关于c - C格式错误的参数过多,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48414676/

10-11 22:44
查看更多