fastread homefastrread library fastread menu

C : printf scanf in C

Tutorial by:      Date: 2016-04-27 04:03:15

❰ Previous Next ❱

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

 
  1. printf("format string",argument_list);  

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.


scanf() function

The scanf() function is used for input. It reads the input data from the console.

 
  1. scanf("format string",argument_list);  

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given number.

 
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. printf("cube of number is:%d ",number*number*number);  
  11.   
  12. getch();  
  13. }  

Output

enter a number:5
cube of number is:125

The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.


Program to print sum of 2 numbers

Let's see a simple example of input and output in C language that prints addition of 2 numbers.

 
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int x=0,y=0,result=0;  
  5. clrscr();  
  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 2 numbers:%d ",result);  
  14.   
  15. getch();  
  16. }  

Output

enter first number:9
enter second number:9
sum of 2 numbers:18

❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website