printf编译但不会打印任何内容

bry*_*is7 0 c printf

#include <stdio.h>

int tempconvert(int, int, int);

int main(void) {
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    int tempconvert(lower, upper, step)
    {
        fahr = lower;
        while (fahr <= upper) {
            celsius = (5.0/9.0) * (fahr-32.0);
            printf("%3.0f %6.1f\n", fahr, celsius);
            fahr = fahr + step;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里有关于C编程书示例的新增内容.试图运行此代码,并编译,但printf拒绝实际打印任何东西.我可以使用这本书的例子继续前进,但我不知道为什么我的代码不能正常工作.

对这里我可能缺少的任何帮助或见解都会很精彩.

Tob*_*ght 7

我猜你正在关闭警告进行编译.这是我得到的:

gcc -std=c11 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds -O2     -c -o 40157429.o 40157429.c
40157429.c: In function ‘main’:
40157429.c:13:5: warning: ISO C forbids nested functions [-Wpedantic]
     int tempconvert(lower, upper, step){
     ^~~
40157429.c: In function ‘tempconvert’:
40157429.c:13:9: warning: type of ‘lower’ defaults to ‘int’ [-Wimplicit-int]
     int tempconvert(lower, upper, step){
         ^~~~~~~~~~~
40157429.c:13:9: warning: type of ‘upper’ defaults to ‘int’ [-Wimplicit-int]
40157429.c:13:9: warning: type of ‘step’ defaults to ‘int’ [-Wimplicit-int]
40157429.c:20:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
40157429.c: In function ‘main’:
40157429.c:7:23: warning: variable ‘step’ set but not used [-Wunused-but-set-variable]
     int lower, upper, step;
                       ^~~~
40157429.c:7:16: warning: variable ‘upper’ set but not used [-Wunused-but-set-variable]
     int lower, upper, step;
                ^~~~~
40157429.c:7:9: warning: variable ‘lower’ set but not used [-Wunused-but-set-variable]
     int lower, upper, step;
         ^~~~~
At top level:
40157429.c:13:9: warning: ‘tempconvert’ defined but not used [-Wunused-function]
     int tempconvert(lower, upper, step){
         ^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

您可以通过内联修复这些错误:

#include <stdio.h>

int main(void) {
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

或者通过将循环移动到函数中:

#include <stdio.h>

void tempconvert(int, int, int);

int main(void) {
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;
    tempconvert(lower, upper, step);

    return 0;
}


void tempconvert(int lower, int upper, int step){
    float fahr = lower;
    while (fahr <= upper) {
        float celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}
Run Code Online (Sandbox Code Playgroud)

两者都做你想要的.