square2.c

  #include <stdio.h>

  /*  A program to calculate a square area. */
  int main()
  {
     int width, length, area;

     printf("Width : ");
     scanf("%d", &width);
     printf("Length: ");
     scanf("%d", &length);
     area = width * length;
     printf("The dimension is %d by %d\n", width, length);
     printf("The area is %d.\n", area);
     return 0;
  }
  • This program does exactly the same as the previous one except the width and depth are taken from user input.
  • While printf output to the screen, scanf takes the input from keyboard and put it inside a variable.
  • Notice how we use it in the program. We got the same %d in scanf, but there is no \n there.
  • Notice also that we have to put the & sign before the variable names when using scanf.

 


Where to go

Chapter 2
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2001

Hosted by www.Geocities.ws

1