Tuesday, May 20, 2014

4. Using scanf for dynamic input

Using scanf for dynamic input


When we assign a value to a variable using assignment statement like

a=10;

its value is hard-coded means can't changed by the computer operator at runtime(while executing). To make more dynamic program variables values can be read from console ( normally keyboard ) using scanf function, but proper care must be taken while selection which variables must be read from console and which are to be hard-coded.

Consider a program which reads radius of circle from the keyboard at run-time and calculates its area.

mathematical expression for calculation area of circle will be

Area = PI * radius * radius;

at there Area depends on value of PI and radius. How ever value of PI is fixed (3.14) and thus to be hard-coded and value of radius is to be read from keyboard. however special keyword (const can be used while declaring PI ) which prevents accidental change of value of PI.

The syntax of scanf function is


scanf("control string", list of variables with & sign with each variable);

let we want to read the value of  two float variables a,b the scanf statement will be

scanf("%f %f",&a,&b);

it is recommended that use scanf function for one variable at time. it will prevent computer operator form feeding invalid inputs.

Control String : used in scanf consists of only type specifiers and spaces only, how ever general text can be used to read more complex formatted input.

The program to read the radius of a circle which prints area can be written as

==================================================================
#include <stdio.h>
#include <conio.h>
    
void main()
  { float r,pi,a;                                         // defines two float variables r and pi
     clrscr();                                             //clears the screen
     pi=3.14;                                           // defining value of pi

     printf("Enter radius of circle  : ");       // printing a message for operator to input value of r however actual value is read by scanf statement);
     scanf("%f",&r);

     a=pi * r * r;                                    // calculating area of circle and storing it in a

     printf ("Area of circle  : %f",a);       // prints output for operator

     getch();
   }
=======================================================================

Some more words

    1.    ( & ) sign used with each variable in scanf function sends the address of corresponding variable to the scanf function which helps scanf function to place value read from keyboard at appropriate memory location.

    2.    For  more complex input general text can be used in control string.
           Let we want to read a date, it involves day,mon and year each as integer. program to read a formatted date and print formatted date will be.

#include <stdio.h>
#include <conio.h>

void main()
 { int d,m,y;
    clrscr();
    printf("Enter date of birth (dd/mm/yyyy) : "); // just a message to the operator no other function
   
    scanf("%d/%d/%d",&d,&m,&y);                // read value of d,m,y in the format d/m/y
                                                                      // at here / 's are used to enforce formatted input

    printf("Date of birth : %02d/%02d/%04d",d,m,y); //prints date in proper format

   getch();
 }



/'s are just to enforce input in format dd/mm/yyyy however we can use - 's to enforce the format as dd-mm-yyyy

%04d enforces the value of y in four spaces right aligned with leading 0;s on the left


code snippet 

int a=10;
printf("%5d",a); 

will print 00010 as output

========================================================================







No comments:

Post a Comment