printf编译但不会打印任何东西

时间:2016-10-20 14:34:13

标签: 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;
}

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

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

4 个答案:

答案 0 :(得分:7)

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

public class MainActivity extends AppCompatActivity {
    public Paint paint;
    public List<Point> coords;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawingView(this));

        paint = new Paint();
        coords = new ArrayList();

        ImageView iv = new ImageView(getApplicationContext());
        iv.setImageResource(R.drawable.car);
        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        iv.setLayoutParams(parms);
    }

    class DrawingView extends SurfaceView {

        private final SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public DrawingView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
              addpoint(event.getX(), event.getY());
            }
            return false;
        }

        public void addpoint(float x, float y){

            Point point = new Point();
            point.x = Math.round(x);
            point.y = Math.round(y);
            coords.add(point);

            for(int i = 0; i< coords.size(); i++) {
                Canvas canvas = surfaceHolder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                canvas.drawCircle(coords.get(i).x, coords.get(i).y, 20, paint);
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

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

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){
         ^~~~~~~~~~~

或将循环移动到函数中:

#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;
}

这两者都做你想要的。

答案 1 :(得分:3)

有两件事 -

  1. 您不能在C中使用嵌套函数。(详情here

  2. 您只声明了函数tempconvert但从未调用它。 即使main函数中的变量名称与tempconvert函数的参数相同,它们也是不同的变量。您也应该阅读变量范围。

  3. 要纠正此问题,请在main之外声明该函数,然后调用它。

答案 2 :(得分:2)

你定义了一个不是标准C的嵌套函数(它是以前的gcc扩展,但是没有注意到它仍然是到现在为止)。无论如何,你的代码几乎只在main中定义一个函数而从不调用它。更正确的C代码重写是:

#include <stdio.h>

int tempconvert(int, int, int);

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;
    }
}


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

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

    return 0;
}

它们是main中tempconvert的缺失调用,并且该函数没有返回值。

我想你会喜欢或类似的东西:

#include <stdio.h>

float tempconvert(int fahr) {
    float celsius = (5.0f/9.0f) * (fahr-32.0f);
    return celsius;
}


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

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

    fahr = lower;
    while (fahr <= upper) {
        celsius = convert(fahr);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }   
    return 0;
}

答案 3 :(得分:2)

此代码包含嵌套函数。这不是标准C.

即使是支持它的编译器,也不会调用有问题的函数,所以当然没有任何事情发生。

您需要将嵌套函数体移出main,或者完全摆脱声明行,以便函数的代码在main中内联。