The most important thing in programming is input and output. A suitable program get user input dynamically. Suppose you create a program that calculate sum of two number. Then you compile it and generate .exe file.
Now the problem is if users can’t input the value they want then this program calculate same calculation again and again which have no mining. So we need to get input from users. Now in c language we use “scanf()” function to get data from users through keyboard. “scanf()” is a counter part of “printf()” function. As we know that “printf()” print the value as output where as “scanf()” get the value for calculation. Both function “printf()” and “scanf()” belongs to same header file that is standard input output - “stdio.h”.
Now let’s write the code for it
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”, variable name);”
in “scanf()” function “&” before variable is must. “&” work as a “address of operator” in c language. It gives the location number used by the variable in memory. When we enter any input value, the value is stored in the location that variable addressed.
Now let’s see the output of these program
Now let’s see the output of above program
Now the problem is if users can’t input the value they want then this program calculate same calculation again and again which have no mining. So we need to get input from users. Now in c language we use “scanf()” function to get data from users through keyboard. “scanf()” is a counter part of “printf()” function. As we know that “printf()” print the value as output where as “scanf()” get the value for calculation. Both function “printf()” and “scanf()” belongs to same header file that is standard input output - “stdio.h”.
Now let’s write the code for it
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the value of a \n");
scanf("%d",&a);
printf("Enter the value of b \n");
scanf("%d",&b);
c=a+b;
printf("The sum of a and b is %d",c);
}
The above program is about sum of two inputs. “a” and “b” hold the value of inputs, and “c” hold the sum of two numbers by “+” operation. void main()
{
int a,b,c;
printf("Enter the value of a \n");
scanf("%d",&a);
printf("Enter the value of b \n");
scanf("%d",&b);
c=a+b;
printf("The sum of a and b is %d",c);
}
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”, variable name);”
in “scanf()” function “&” before variable is must. “&” work as a “address of operator” in c language. It gives the location number used by the variable in memory. When we enter any input value, the value is stored in the location that variable addressed.
Now let’s see the output of these program
Enter the value of a
12
Enter the value of b
16
The sum of a and b is 28
Now what if we want to get both input in same line, to describe these we will write a program12
Enter the value of b
16
The sum of a and b is 28
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the value of a and b\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("The sum of a and b is %d",c);
}
As we see the format in these program that in a single “scanf()” we can get more than one input value. As many input we want we must write format specifier for each and every variable.void main()
{
int a,b,c;
printf("Enter the value of a and b\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("The sum of a and b is %d",c);
}
Now let’s see the output of above program
Enter the value of a and b
23 12
The sum of a and b is 35
As we can see here that when we entering the value inside the program, each value must be separated by space or enter or tabs. 23 12
The sum of a and b is 35


0 comments:
Post a Comment