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).
The printf() function is used for output. It prints the given statement to the console.
The syntax of printf() function is given below:
printf("format string",argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
Let's see a simple example of c language that gets input from the user and prints the cube of the given number.
#includeint main(){ int number; printf("enter a number:"); scanf("%d",&number); printf("cube of number is:%d ",number*number*number); return 0; }
Output
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.
Let's see a simple example of input and output in C language that prints addition of 2 numbers.
#includeint main(){ int x=0,y=0,result=0; printf("enter first number:"); scanf("%d",&x); printf("enter second number:"); scanf("%d",&y); result=x+y; printf("sum of 2 numbers:%d ",result); return 0; }
Output