Thursday, 3 September 2020

Use of Printf and Scanf

Use of Printf and Scanf

Use of ''printf" and ''scanf" function with all formating facilities :

In c programming language, "printf" and "scanf" are inbuilt  library function.

These functions are declared and defined in the header file ' ' stdio.h".


Printf

It is used to print the output on to the screen

We cab use printf() function with different format specifier,Following are some of them:-

(1)    %d is used to display the valu of an integer variable.

(2)    %c is used to display chracter.

(3)    %f is used for float variable.

(4)    %s for string variable.

(5)    %if for double.

(6)    %x for hexadecimal variable.

(7)     \n to generate a newline.


scanf :-

It is used to read different type of input from keyboard.

we can use scanf () function with different format specifier.

Following we some of them:-

(1)     %d - the value entered is received as an integer.

(2)     %s - the value entered is received as an string.


program to print sum of  two numbers

Let's see a simple example:-

  1. #include<stdio.h> 
  2. #include<conio.h>   
  3. int main()
  4. {    
  5. int x=0,y=0,result=0;  
  6.   
  7. printf("enter first number:");  
  8. scanf("%d",&x);  
  9. printf("enter second number:");  
  10. scanf("%d",&y);  
  11.   
  12. result=x+y;  
  13. printf("sum of two numbers:%d ",result);  
  14.   
  15. getch();  
  16. }    
enter first number:3                                                                                                          
enter second number:5                                                                                                         
sum of two numbers:8                                                                                                          
                                                                                                                              
...Program finished with exit code 0                                                                                          
Press ENTER to exit console.    






 

No comments:

Post a Comment

Variables in C

What is variables:- A  variable  is nothing but a name given to a storage area that our programs can manipulate. Each  variable in C  has a ...