×

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

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 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.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.