bryanwillis7 bryanwillis7 - 1 month ago 19
C Question

printf compiles but won't print anything

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


Total newb here following The C Programming Book examples. Trying to run this code, and it compiles, but printf refuses to actually print anything. I could just use the book example and move on, but I'm frustrated in not knowing why my code isn't working.

Any help or insight into what I might be missing here would be wonderful.

Answer

I'm guessing you're compiling with warnings turned off. Here's what I get:

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

You can fix those errors by inlining:

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

or by moving the loop into the function:

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

both of which do what you seem to want.