Scanf() : Receiving input in C program

In the First C Program discussed earlier (same has been also mentioned below for the reference) we assumed the values of p, n and r to be 1000, 3 and 8.5. Every time we run the program we would get the same value for simple interest. If we want to calculate simple interest for some other set of values then we are required to make the relevant change in the program, and again compile and execute it. Thus the program is not general enough to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able to make changes in the program. Hence it is a good practice to create a program that is general enough to work for any set of values.

 

first c program
first c program

To make the program general the program itself should ask the user to supply the values of p, n and r through the keyboard during execution. This can be achieved using a function called scanf( ). This function is a counter-part of the printf( ) function. printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below.

/* Calculation of simple interest */
main( )
{
int p, n ;
float r, si ;
printf ( “Enter values of p, n, r” ) ;
scanf ( “%d %d %f”, &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( “%f” , si ) ;
}

  • The first printf( ) outputs the message ‘Enter values of p, n, r’ on the screen. Here we have not used any expression in printf( ) which means that using expressions in printf( ) is optional.
  • Note that the ampersand (&) before the variables in the scanf( ) function is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory.
  • When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard.

Point to be Noted

A blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key. This is shown below:

Ex.: The three values separated by blank
1000 5 15.5
Ex.: The three values separated by tab.
1000 5 15.5
Ex.: The three values separated by newline.
1000
5
15.5

So much for the tips. How about another program to give you a feel of things…

main( )
{
int num ;
printf ( “Enter a number” ) ;
Chapter 1: Getting Started 23
scanf ( “%d”, &num ) ;
printf ( “Now I am letting you on a secret…” ) ;
printf ( “You have just entered the number %d”, num ) ;
}

 

Related Posts

  • 69
    Welcome to the C Knowledge Base and Tutorial   Introduction to C Programming Start Learning C Programming What is C? Why We should learn C C Character Set : Constants, Variables and Keywords Constant and Variable in C: C Keywords: Types of C Constant Rules for Constructing Integer Constants Rules…
    Tags: program, tutorial, programming, receiving, set, learn, scanf, input
  • 51
    The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
    Tags: learn, program, programming, tutorial
  • 51
    Let's have an example to understand else if clause : /* else if ladder demo */ main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First…
    Tags: printf, program, learn, programming, tutorial
  • 48
    It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs. Let's have an example: /* A quick demo of nested if-else */ main( ) { int i ;…
    Tags: program, printf, learn, tutorial, programming
  • 48
    Problem Statement: If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. Answer: /* Sum of digits of a 5 digit number*/ #include<stdio.h> #include<conio.h> main() { int num,a,n; int sum=0; /* this has been initialised to zero otherwise it will contain…
    Tags: number, program, learn, programming, keyboard, tutorial, input

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: