#So Confused By These Results Using Printf And Specifiers e, f, g

36 messages · Page 1 of 1 (latest)

orchid locust
#

Hello, I am new to c++ programming. This is just a simple test program and I was curiously messing with outputting some numbers when I observed this peculiar behavior.

int main() {
    int quotient = 25 / 7;

    std::cout << quotient << std::endl;
    printf("%e\n", quotient);
    printf("%f\n",quotient);
    printf("%g\n", quotient);
    
    std::cout << 25.0 / 7.0 << std::endl;
    printf("%g\n", (25.0 / 7.0));
}

Output:
3
1.482197e-323
0.000000
1.4822e-323
3.57143
3.57143

I am expecting the integer variable outputs to always be 3 but in reality, see that only the std::cout prints 3 while the 3 printf calls with specifiers print essentially 0. I have tried to do individual research and think I have a decent grasp on what the %g specifier is trying to do but cannot find evidence of why I am seeing these results.

Thanks!

pale nymphBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

empty stream
#

printf doesn't know what you pass to it automatically, that's why you need to pass a format string. You lied in the format string, claiming to have passed a double but actually having passed an int. printf, having no idea it was lied to, casts the int * to a double * and prints it which is undefined behavior. There should be compiler warnings you can enable so it tells you about a mismatch of format string and passed arguments.

sleek crescent
#

this is precisely why we don't use printf in C++ ^^

orchid locust
#

interesting, and thanks for the information

empty stream
#

std::cout does know what was passed to it because it has overloaded operator<< for a bunch of types, but you can still trick it using *reinterpret_cast<double *>(&quotient); which probably shows the same number, but UB is weird.

orchid locust
#

to be honest this is part of an introductory module to a class on opengl so the use of the cout and printf is part of the exercise

sleek crescent
#

wtf

#

there's absolutely zero reason why opengl would require the use of printf

#

like

#

none

#

and even less reason why you should mix the two

#

like

#

wtf 😄

orchid locust
#

yeah its looking like this class is going to be a mess

#

im still a little lost in the format string part

#

how is 25.0/7.0 a format string... just because it is being read as a double?

sleek crescent
orchid locust
#

what is UB?

sleek crescent
#

undefined behavior

empty stream
#

25.0/7.0 is not the format string. "%e\n" is the format string.

orchid locust
#

but the format string being an integer is just not allowed?

#

becuase it is not in that table?

empty stream
#

printf takes a const char * as first argument.

#

And int is in the table, %i and %d both print integers.

#

So printf("%i\n", quotient); is the correct use of printf and then it stops giving you weird results.

orchid locust
#

I dont understand why the professor would explicitly tell us to use %g for the integer division instead of %d

empty stream
#

Me neither. It's a programming mistake.

sleek crescent
#

the professor probably also told you to do it using double or smth

#

not int

orchid locust
#

(╯°□°)╯︵ ┻━┻

#

I can provide the instructions if necessary

#

thanks guys for helping me out on this

sleek crescent
#

np ^^

orchid locust
#

!solved