Unexpected output of printf

You are passing a float for the %d format string, but printf is expecting an int. printf is a variable argument list function: printf(const char *format_string,…); This means that the rest of the arguments after the format string can be of any type and number, and the compiler doesn’t know what they should be. It … Read more

What will be the value of uninitialized variable? [duplicate]

Technically, the value of an uninitialized non static local variable is Indeterminate[Ref 1]. In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.[Ref 2] [Ref 1] C99 section 6.7.8 Initialization: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [Ref 2] C99 … Read more

If a function returns no value, with a valid return type, is it okay to for the compiler to return garbage?

In C++, such code has undefined behaviour: [stmt.return]/2 … Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. … Most compilers will produce a warning for code similar to that in the question. The C++ standard does not require this … Read more

Is uninitialized local variable the fastest random number generator?

As others have noted, this is Undefined Behavior (UB). In practice, it will (probably) actually (kind of) work. Reading from an uninitialized register on x86[-64] architectures will indeed produce garbage results, and probably won’t do anything bad (as opposed to e.g. Itanium, where registers can be flagged as invalid, so that reads propagate errors like … Read more