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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.