-1

I don't understand why this code produces the below output:

#include <stdio.h>

#define LOWER 0
#define UPPER 300
#define STEP 20

float c_to_f(float c);

int main(void) {
    for (int c = LOWER; c <= UPPER; c += STEP) {
        printf("%3.0f %6.1f\n", c, c_to_f(c));
    }

    return 0;
}

float c_to_f(float c) {
    return c * (9.0/5.0) + 32;
}

with output:

  0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
  0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
  0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
  0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
  0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
  0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0

I do understand that c should be a float, just not the output. Any help would be greatly appreciated.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Aptent
  • 3
  • 1

2 Answers2

3

The problem here, as I see it is

printf("%3.0f %6.1f\n", c, c_to_f(c));

c is an int and you're trying to print it's value using %f, which is undefined behavior.

Using an inappropriate type of argument for a format specifier is undefined behavior.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks, do you have any greater understanding as to why the output is specifically as it is? – Aptent Aug 10 '15 at 20:19
  • @Aptent sorry, no. An undefined behavior is really undefined, it can;t be defined or justified in general. – Sourav Ghosh Aug 10 '15 at 20:22
  • Why do posters continually ask for UB to be explained? This tendency is really getting annoying now:( – Martin James Aug 10 '15 at 20:53
  • I asked, which I now see does not make sense, because I did not fully understand what undefined behavior meant. Perhaps that was lazy of me, but I don't see what is the problem with asking. I think it is very important to feel free to ask stupid questions. – Aptent Aug 10 '15 at 21:02
0

When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

NSGangster
  • 2,397
  • 12
  • 22
  • 2
    it is my understanding that %6.1f would mean print as floating point, at least 6 wide, with 1 decimal place. – Aptent Aug 10 '15 at 20:23