hot post

input/output instruction in c (part 9)

input/output instruction in c (part 9)

input/output instruction in c (part 9)
input/output instruction in c 

input/output instruction in c

hello friends,
In the c language learning series, we are seeing the use of printf() from the last three parts. From this we should understand how important the printf() is for us. And now we will know about a new function which is also very important function like printf() which is scanf().

So let's start.

what is scanf()? 


scanf() is also a predefined function which is use in programming .


is scanf() is a keyword?


no scanf() is not a keyword it is predefined function which is use in programming 

general formula ("format specifier",variable adress);
[Please check the previous part if you check about the format specification ]

work of scanf() :



  • The work of scanf() is to take date from the keyboard and store in the variable. 


  • Eco -  we take data from the keyboard and scanf() print the data on the output screen this processes is called eco.


now let take an example and see this example very carefully-

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
scanf("%d",&x);
getch();
}

when we run this program, the output screen become black and blank and now on the output screen we can write any digit.
the syntax of scanf() is different from the syntex of printf(). we use an important syntax which is adders of(&). It is very necessary when we use scanf() function. the use of &, we study later in very detail. We  use  this syntax before the variable x.


Difference between scanf() and printf();


scanf() -

  • when we take data from keyboard and the data is stored in variable then we use scanf()


printf() - 
  • In this function we  take  data from the  keyboard and it does not store data into variable. 
  • when we print any input instruction on output screen then we use printf().
If we want the square of any number then how we can do this with the help of scanf()
lets start please see the codding carefully -


#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
scanf("%d",&x);
printf("square of %d is %d",x*x);
getch();
}

when we run this program, the square of any number which you type on output screen will show on output screen.
there is a problem in this program, when we run this program the output screen become black and the person who make this program know that he  have to type a number but the person who do not make this program then he do not know that he type the number so he get confuse so the solution of this problem is given below please see carefully this coding and follow me


#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("please enter the number");
scanf("%d",&x);
printf("square of %d is %d",x*x);
getch();
}


due to this coding the output screen show first please enter the number and everyone understand What does he have to do.
we can also add  two numbers
for example 56+89=?
enter this code  and we find the answer 


#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("please enter two number");
scanf("%d %d",&x,&y);
printf("sum of %d and %d is %d",x,y,x+y);
getch();
}

I hope you will like this post and this post help you very much.

 I am trying my best to explain the c language to you, please stay with me and share this post and if there is any mistake then you comment on comment box thanks for support
    


part 8 (
inputoutput-instruction-in-cpart-8)

Post a Comment

0 Comments