0
#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.

7
  • 4
    You cannot have nested functions in C.
    – user3185968
    Commented Oct 20, 2016 at 14:35
  • 5
    But even if you could - you are not calling it...
    – Eugene Sh.
    Commented Oct 20, 2016 at 14:39
  • 2
    @EOF I think gcc has a non standard extension that allows it. Commented Oct 20, 2016 at 14:41
  • 2
    Regrettably, GCC allows nested functions. It should require an enabling option but it doesn't. Don't use nested functions in code posted on SO unless you are really confident they're what you're asking about. Even nested functions don't get called unless you call them. Commented Oct 20, 2016 at 14:42
  • agh.. Sorry guys, I'm still confused. Like, what area specifically is the offending part? I'm following an example and I can't tell why his: learntosolveit.com/cprogramming/Ex_1.15_tempconv.html Isn't a nested function but mine is? Commented Oct 20, 2016 at 14:49

4 Answers 4

7

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.

1
  • Thank you so much, this answered my question perfectly! Glad I asked, now I have a little better of an understanding of the language. I come from Javascript so there's a lot that I'm having to learn. Commented Oct 20, 2016 at 17:08
3

There are two things -

  1. You can not have nested functions in C. (Details here)

  2. You have only declared the function tempconvert but never called it. Even though names of variables in your main function are same as parameters to your tempconvert function, they are different variable. You should read about Scope of variables too.

To correct that, Declare the function outside main and then call it.

2

This code contains a nested function. This is not standard C.

Even on compilers which support it, the function in question is never called, so of course nothing happens.

You need to either move the nested function body out of main, or get rid of the declaration line entirely so the function's code is inline in main.

2

You defined a nested function which is not standard C (it was a former gcc extension, but didn't notice it remains up to now). Anyway your code almost only defines a function inside the main and never call it. A more correct C rewriting of your code is:

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

They are a missing call to tempconvert in the main and no return value from that function.

I think you would like something like or alike:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.