Reading Data from the Keyboard
A program is nothing if it is not accepting any data from the user or outputs some data to the user. A program is interactive only if it accepts data's from the user at run time. It should accepts data needed for the program and output the result based on the user input. For example a user may want to input basic salary of an employee to calculate his net salary, he can use the same program to calculate the net salary of another employee by inputting different values upon his requirement.
C provides a library function scanf( ) that accepts data's from the keyboard. It uses the following syntax :
scanf("control string",&variable_name");
control string uses the control string for the type of variable_name, i.e. if we are reading an integer value then control string will be %d, if we are reading a character value then the control string is %c. Variable name preceded by an ampersand (&) symbol. Ampersand (&) is also known as "address of operator", address of operator instructs the computer where to store the data read from the keyboard. Ampersand operator provides the memory address of the variable we specified, and the value just inputted stored to that memory location, we can access the value using the variable name.
scanf("%d",&age); //reading an integer value;
scanf("%f",&avg);//reading a float value;
scanf("%c%d",&grade,&roll); //reading a character and an integer.
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);//reading 5 integers.
C provides a library function scanf( ) that accepts data's from the keyboard. It uses the following syntax :
scanf("control string",&variable_name");
control string uses the control string for the type of variable_name, i.e. if we are reading an integer value then control string will be %d, if we are reading a character value then the control string is %c. Variable name preceded by an ampersand (&) symbol. Ampersand (&) is also known as "address of operator", address of operator instructs the computer where to store the data read from the keyboard. Ampersand operator provides the memory address of the variable we specified, and the value just inputted stored to that memory location, we can access the value using the variable name.
scanf("%d",&age); //reading an integer value;
scanf("%f",&avg);//reading a float value;
scanf("%c%d",&grade,&roll); //reading a character and an integer.
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);//reading 5 integers.
Learning by Example 2
/* Program to read two numbers and find their sum */
/* Program to read two numbers and find their sum */
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,sum; /* declare three integer variables */
clrscr( );
printf("Enter two numbers : "); /* Prompt the user to enter two numbers */
scanf("%d%d",&a,&b); /* Read two integers from keyboard values in a and b */
sum=a+b; /* add a and b, store result in sum */
printf("Sum=%d",sum); /* Print the value in sum */
/*printf("%d+%d=%d,a,b,sum); */ /* Try using this to print sum*/
getch( );
}
int a,b,sum; will declare three integer variables a,b,sum each variable will get 2 bytes memory space allocated, a,b and sum now may contain some garbage values.
Next we are prompting the user to enter two numbers using the printf( ), it is necessary in our programs to instruct the user what to do next, or what to do from his side for program execution.
While executing scanf("%d%d",&a,&b); the control will wait for two values from keyboard, input two numbers and that numbers will be stored in the variable a and b. Here first number will be stored in variable 'a' and second will be in variable 'b'.
sum=a+b; will add the values within 'a' and 'b' and assignment operator (=) will assign the value thus results to the variable 'sum'.
printf("Sum=%d",sum); will print the sum in the format Sum=250 (If we input two number 175 and 75).



0 comments:
Post a Comment