Format Specifiers for Reading Data in scanf - C Language Basics

C examples for Language Basics:scanf

Introduction

ActionRequired control string
To read a value of type short %hd
To read a value of type int %d
To read a value of type long %ld
To read a value of type float %f or %e
To read a value of type double%lf or %le

In the %ld and %lf format specifiers, l is lowercased.

You must always prefix the name of the variable that's receiving the input value with &.

Demo Code

#include <stdio.h>

int main(void){
  float radius = 0.0f;
  float diameter = 0.0f;
  float circumference = 0.0f;
  float area = 0.0f;

  printf("Input the diameter of a circle:");
  scanf("%f", &diameter);

  radius = diameter/2.0f;/*from ww w.  j a va 2 s  .c  o m*/
  circumference = 2.0f * 3.14159f * radius;
  area = 3.14159f * radius * radius;

  printf("\nThe circumference is %.2f. ", circumference);
  printf("\nThe area is %.2f.\n", area);
  return 0;
}

Result


Related Tutorials