how are float values exactly represented in memory......

Dani AI

Generated

asked where the mantissa and other parts actually live in memory. 's pointer to the common format is right in practice; 's "not that simple" hints at the platform/compiler details that matter.

On common implementations that use IEEE‑754 single precision the 32 bits are laid out as: 1 sign bit, 8 exponent bits (biased by 127), and 23 fraction bits (the stored mantissa). The stored fraction occupies bits 22..0 of the 32‑bit word; for normalized values there is an implicit leading 1 (the "hidden bit") that is not stored. Exponent==0 encodes subnormals (no implicit 1), and the exponent all‑ones value encodes infinities/NaNs. C itself does not mandate IEEE‑754 — compilers and targets do — and registers (especially x87 vs SSE on x86) can hold wider formats, so the in‑memory representation only appears when the value is actually spilled to memory.

A reliable way to observe the bit pattern in C is to memcpy the float to a uint32_t and print the integer/hex. That avoids strict‑aliasing UB. For example, on an IEEE‑754 machine the nearest single‑precision encoding for decimal 1.2 is 0x3F99999A (sign=0, exponent=127, fraction=0x199999A). When inspecting raw bytes, remember endianness: little‑endian machines (x86/x64) will show the least significant byte first.

Practical notes: comparing floats for equality is unreliable — use an epsilon (FLT_EPSILON from <float.h>) or integer bit checks for exact‑bit comparisons. To avoid surprises from extended precision in registers, force storage or use compiler flags (for example, modern compilers commonly use SSE for predictable IEEE‑754 behavior). For deeper background and pitfalls, see the classic explanation by David Goldberg and the C floating‑type reference on cppreference: What Every Computer Scientist Should Know About Floating-Point and .

Recommended Answers

All 5 Replies

Floats in C are usually stored as 32-bit (i.e. single precision) IEEE floating point numbers.

I want to know how is a floating point no. stored in memory i.e. if I enter a no. 1.2 ....how exactly would it be stored in memory in form of bits....where would the mantissa get stored..

The how is already sufficiently answered in the article linked in my first post. As to where: If it's a local variable, it will be stored on the stack - same as ints or any other type of value.

Its not that simple -- see this article

Its not that simple

What was that in reply to? What's not how simple? You're not saying, that floats are not stored on the stack, are you (nothing in the linked article would support that of course, but since your post came directly after mine, it kinda sounds like that might be what you're saying)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.