C প্রোগ্রামিংয়ের float Data Type সম্পর্কে আলোচনা

Submitted by tushar pramanick on Tue, 03/05/2013 - 14:48

The float Data Type
The floating-point number is another data type in the C language. Unlike an integer number, a floating-point number contains a decimal point. For instance, 7.01 is a floating-point number; so are 5.71 and -3.14. A floating-point number is also called a real number.

 

A floating-point number is specified by the float keyword in the C language. Floating-point numbers can be suffixed with f or F to specify float. A floating-point number without a suffix is double by default. The double data type is introduced later in this lesson.

 

Like an integer number, a floating-point number has a limited range. The ANSI standard requires that the range be at least plus or minus 1.0*1037. Normally, a floating-point number is represented by taking 32 bits. Therefore, a floating-point number in C is of at least six digits of precision. That is, for a floating-point number, there are at least six digits (or decimal places) on the right side of the decimal point.

 

Not like an integer division from which the result is truncated and the fraction part is discarded, a floating-point division produces another floating-point number. A floating-point division is carried out if both the divisor and the dividend, or one of them, are floating-point numbers.

For instance, 571.2 / 10.0 produces another floating-point number, 57.12. So do 571.2 / 10 and 5712 / 10.0.

 

Declaring Floating-Point Variables
The following shows the declaration format for a floating-point variable:

 

float  variablename;

 

Similar to the character or integer declaration, if you have more than one variable to declare, you can either use the format like this:

 

float  variablename1;
float  variablename2;
float  variablename3;

 

or like the following one:

 

float  variablename1, variablename2, variablename3;

 

The Floating-Point Format Specifier (%f)
Also, in C, you can use the floating-point format specifier (%f) to format your output. Listing 4.4 gives an example showing how to use the format specifier %f with the printf() function.

 

TYPE
Listing 4.4. Printing out results of integer and floating-point divisions.


1:  /* 04L04.c: Integer vs. floating-point divisions */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int int_num1, int_num2, int_num3;   /* Declare integer variables */
7:     float flt_num1, flt_num2, flt_num3; /* Declare floating-point variables */
8:
9:     int_num1 = 32 / 10;    /* Both divisor and dividend are integers */
10:    flt_num1 = 32 / 10;
11:    int_num2 = 32.0 / 10;  /* The divisor is an integer */
12:    flt_num2 = 32.0 / 10;
13:    int_num3 = 32 / 10.0;  /* The dividend is an integer */
14:    flt_num3 = 32 / 10.0;
15:
16:    printf("The integer divis. of 32/10 is: %d\n", int_num1);
17:    printf("The floating-point divis. of 32/10 is: %f\n", flt_num1);
18:    printf("The integer divis. of 32.0/10 is: %d\n", int_num2);
19:    printf("The floating-point divis. of 32.0/10 is: %f\n", flt_num2);
20:    printf("The integer divis. of 32/10.0 is: %d\n", int_num3);
21:    printf("The floating-point divis. of 32/10.0 is: %f\n", flt_num3);
22:    return 0;
23: }


 OUTPUT
 The following output is a copy from the screen after the executable file, 04L04.exe, is run on my machine (I did get several warning messages about type conversions while I was compiling the program in Listing 4.4, but I ignored them all because I'd like to create an executable file and show you the differences between the int data type and the float data type.):

 

C:\app> 04L04
The integer divis. of 32/10 is: 3
The floating-point divis. of 32/10 is: 3.000000
The integer divis. of 32.0/10 is: 3
The floating-point divis. of 32.0/10 is: 3.200000
The integer divis. of 32/10.0 is: 3
The floating-point divis. of 32/10.0 is: 3.200000
C:\app>

 

ANALYSIS
Inside the main() function, the two statements in lines 6 and 7 declare three integer variables, int_num1, int_num2, and int_num3, and three floating-point variables, flt_num1, flt_num2, and flt_num3.

 

Lines 9 and 10 assign the result of 32/10 to int_num1 and flt_num1, respectively; 32.0 / 10 to int_num2 and flt_num2 in lines 11 and 12, and 32 / 10.0 to int_num3 and flt_num3 in lines 13 and 14.

 

Then, lines 16_21 print out the values contained by the three int variables and the three floating-point variables. Note that, %d is used for the integer variables, and the floating-point specifier (%f) is used for formatting the floating-point variables in the printf() function.

 

Because the truncation occurs in the integer division of 32 / 10, flt_num1 contains 3.000000, not 3.200000, which you can see from the second line of the output. However, flt_num2 and flt_num3 are assigned 3.200000, because both 32.0 / 10 and 32 / 10.0 are considered as the floating-point division.

 

But int_num2 and int_num3, as integer variables, discard respectively the fraction parts of the floating-point divisions of 32.0 / 10 and 32 / 10.0. Therefore, you just see the integer 3 in both the third and fifth lines of the output.

Comments

Related Items

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.