sizeof(3.14) GIVES 8 AS THE OUTPUT. Where sizeof(3.0f) gives 4 as output. Is it because default datatype alloted to 3.14 is double and attaching f changes it to float??

Dani AI

Generated

A few practical notes that expand on 's answer and clear up related gotchas.

Suffixes and types

  • Unsuffixed decimal floating constants are of type double. Add f or F to force float, and L (or l) to request long double. These suffixes change the literal's type at compile time; they do not change how many decimal digits you write.

Checking sizes (quick test)

  • sizeof yields a size_t value (number of bytes) determined by the literal's type. A simple program shows the sizes on your platform:
#include <stdio.h>

int main(void) {
    printf("sizeof 3.14   = %zu\n", sizeof(3.14));
    printf("sizeof 3.14f  = %zu\n", sizeof(3.14f));
    printf("sizeof 3.14L = %zu\n", sizeof(3.14L));
    return 0;
}

Format and promotion rules (common pitfall)

  • When passing floats to variadic functions like printf, float arguments are promoted to double, so printf("%f", 3.14f) works. For scanf, the format matters: use %f for float*, %lf for double*, and %Lf for long double*. Mixing these leads to subtle runtime bugs.

Precision and conversions

  • Using 3.14 (double) in expressions can silently convert or promote other operands. If you need single-precision throughout, use 3.14f or cast explicitly. Loss of precision can be the cause of surprising results.

About hex (reply to )

  • Integer hex literals use 0x. C99 also supports hexadecimal floating literals (e.g., 0x1.8p+1) but that is an advanced form and not commonly needed for everyday code.

Useful rule: use sizeof to confirm what your compiler/ABI actually uses, and pick the literal suffix that matches the precision you need.

Recommended Answers

All 3 Replies

I believe that you need to put 0x in front of a number if it needs to be hexadecimal (or was it $??)


Greetz, Eddy

Um... what's your point?

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.