Controlling the Output Field Width - C Data Type

C examples for Data Type:float

Introduction

The general form of the format specifier for floating-point values is like this:

%[width][.precision][modifier]f

The square brackets represents the optional specification.

You can omit width or .precision or modifier or any combination of these.

The width value is an integer specifying the total number of characters including spaces.

The precision value is an integer specifying the number of decimal places after the decimal point.

The modifier part is L when the value you are outputting is type long double, otherwise you omit it.

Demo Code

#include <stdio.h>

int main(void)
{
  float total_length = 10.0f;               // In feet
  float count = 4.0f;                 // Number of equal pieces
  float piece_length = 0.0f;                // Length of a piece in feet
  piece_length = total_length/count;//from   w  w w .j a  v a  2 s .co m
  
  printf("%f feet %f pieces %f feet.\n",total_length, count, piece_length);

  printf("%8.2f foot %5.0f pieces %6.2f feet.\n",total_length, count, piece_length);
  return 0;
}

Result


Related Tutorials