printf() and scanf() function in C programming
printf() : printf function is used
in the c program when the programmer wants to print anything as it is in the
output console. For this, the complete text needs to be written inside
double-quotes. You can also use format specifiers such as %d, %c, %p, %f to
display the values assigned to the variables and pointers using printf
function.
Syntax of printf function:
printf("format
string",argument_list);
Ex. printf(“\nHello World”);
printf(“\n addition of
two number is= %d”,c)
printf is an inbuilt library function that was
defined in #include< stdio.h> as includes the standard input-output
library functions, so before use printf() in the program, we need to declare in
the header file section.
scanf(): scanf() function is
used in the c program to take input from the user. when you execute the program
waits for user input and once the user provides values, it does the processing
of the rest of the program which based on input values provided by the user.
For this, the complete text needs to be written inside double-quotes. You can
also use %d(integer), %c(character), %f(float) we also call it format strings
to display the values assigned to the variables and pointers using scanf()
function.
Syntax of scanf function:
scanf("format
string",argument_list);
printf () and scanf() is inbuilt
library function was defined in #include< stdio.h> as includes the
standard input output library functions , so before use printf() and
scanf() in program we need to declare at beginning of program .
Example : Use of printf() and scanf() function.
/*program to calculate the square of number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
clrscr();
printf("Enter a number");
scanf("%d",&i);
printf("Square of number is:%d",i*i);
return 0;
getch();
}
Out Put:
Enter a number 4
Square of number is:16