totn C Language

C Language: Float Variables

This C tutorial explains how to declare and use floating-point (float) variables with syntax and examples.

Syntax

The syntax for declaring a float variable is:

float variable_name1 [= value1];

Or the syntax for declaring multiple float variables is:

float variable_name1 [= value1] [, variable_name2 [= value2], ... variable_name_n [= value_n]];

Parameters or Arguments

variable_name1
The name of the first variable to declare.
value1
Optional. If provided, it is the value to assign to variable_name1.
variable_name2, ... variable_name_n
Optional. These are additional variables that will be declared with the same C type.
value2, ... value_n
Optional. These are the values that you wish to assign to variable_name2 through variable_name_n.

Example - Declaring a variable

Let's look at an example of how to declare a float variable in the C language.

For example:

float age;

In this example, the variable named age would be defined as a float.

Below is an example C program where we declare this variable:

#include <stdio.h>

int main()
{
   float age;

   age = 10.5;
   printf("TechOnTheNet.com is over %f years old.\n", age);

   return 0;
}

This C program would print "TechOnTheNet.com is over 10.500000 years old."

Example - Declaring a variable and assigning a value

You can define a variable as a float and assign a value to it in a single declaration.

For example:

float age = 10.5;

In this example, the variable named age would be defined as a float and assigned the value of 10.5.

Below is an example C program where we declare this variable and assign the value:

#include <stdio.h>

int main()
{
   float age = 10.5;

   printf("TechOnTheNet.com is over %f years old.\n", age);

   return 0;
}

This C program would print "TechOnTheNet.com is over 10.500000 years old."

Example - Declaring multiple variables in a statement

If your variables are the same type, you can define multiple variables in one declaration statement.

For example:

float age, load;

In this example, two variables called age and load would be defined as float.

Below is an example C program where we declare these two variables:

#include <stdio.h>

int main()
{
  float age, load;

   age = 10.5;
   load = 1.4;
   printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);

   return 0;
}

This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."

Example - Declaring multiple variables in a statement and assigning values

If your variables are the same type, you can define multiple variables in one declaration statement. You can also assign the variables a value in the declaration statement.

For example:

float age = 10.5, load = 1.4;

In this example, two variables called age and load would be defined as float and be assigned the values 10.5 and 1.4, respectively.

Below is an example C program where we declare these two variables and assign their values:

#include <stdio.h>

int main()
{
   float age = 10.5, load = 1.4;

   printf("TechOnTheNet.com is over %f years old and pages load in %f seconds.\n", age, load);

   return 0;
}

This C program would print "TechOnTheNet.com is over 10.500000 years old and pages load in 1.400000 seconds."