×

2f in C language

Float data type in c:

Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE 4-byte floating-point.

The FLOAT data type's value range is identical to your computer's C double data type's value range. Integer and double are further prevalent data types. Now let us see the syntax that is used to define float data type.

Syntax of float data type:

There is a particular syntax to define each and every variable or data type or any of the function. So the syntax to define a float variable is as shown below:

float  var_name = value;

So we can declare a float variable by using the above syntax. Now let us see an example to define a float data type using the following code as shown below :

Example:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a = 10;
float f = 55.63847;
printf ( “ % d \ n “ , a );
printf ( “ % f \ n “ , f );
} // main method

Output:

10
55.63847

Now let us see another example to define a float data type using the following code as shown below:

Example:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a;
float f;
scanf ( “ % d “ , & a );
scanf ( “ % f “ , & f );
printf ( “ % d \ n “ , a );
printf ( “ % f \ n “ , f );
} // main method

Input and Output:

// Input
a = 25
f = 84.518792
// Output
25
84.518792

We can also round up the float values using certain kind of syntax while printing the float value. Now let us see rounding off float value upto two decimal places using . 2 f.

2f in C language:

The "% . 2" instructs "print" to print only the first two digits following the point. "print" treats the % as a special character you need to include so it can know that when you enter "f", the number (result) that will be printed will be a floating point type.

The instruction "2f" instructs the printf method to print a floating point value (in this case, the double, x) with two decimal places. Similar to how, if we had used "%. 3f," x would have printed with three decimal places.

 Now let us see few more examples using the format specifier “ % .2f “ using the following code as shown below :

Example:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a = 42;
float f = 79.35423;
printf ( “ % d \ n “ , a );
printf ( “ % 0 . 2f \ n “ , f );
} // main method

Output:

42
79.35

Now let us see another example to define a float data type using the following code as shown below:

Example:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a;
float f;
scanf ( “ % d “ , & a );
scanf ( “ % f “ , & f );
printf ( “ % d \ n “ , a );
printf ( “ % 0 . 2f \ n “ , f );
} // main method

Input and Output:

// Input
a = 97
f = 572.264786
// Output
97
572.26

We can also round off the given float variable or value to the particular number by using the format specifiers such as “% 0 . 3f “to round the values upto 3 decimal places , “ % 0 . 4f “to round the values upto 4 decimal places and so on.

We will round off the values to predict the result in accurate manner.


Related Topics

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.