Why should we use 'f' with float literal in C?

By: IncludeHelp, on 23 JAN 2017

Consider the following statement

float x=10.23;

Here, 10.23 is not a float value it's double type, which is converting into float type implicitly while assigning in x.

Let's prove it, consider the following statement

printf("%d",sizeof(10.23));

This statement will return 8, while float takes 4 bytes in the memory. So, it's double because double takes 8 bytes in the memory.

Correct form of float literal

Consider the following statement

float x=10.23f;

Here, 10.23f is a correct float literal representation and if we print its size it will be 4.

Consider the following program; here we are printing size of 10.23 and 10.23f.

#include <stdio.h>
int main()
{
	printf("size of double literal: %d\n",sizeof(10.23));
	printf("size of float literal: %d\n",sizeof(10.23f));
	
	return 0;
}

Output

    size of double literal: 8 
    size of float literal: 4

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.