Home » Programming Tips & Tricks » C - Tips & Tricks

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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.