The printf() function is a predefined standard 'c' function.
The printf() function is used for output. It prints the given statement to the console.
The printf() function prints all types of data values to the console. it requires conversion symbol and variable names to print the data.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 2;
float y = 3.14;
char z = "M";
printf("%d %f %c",x,y,z);
}
output:
2 3.14 M
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 65;
printf("%c %d",x,x);
}
output:
A 65
The scanf() function is used for input. The scanf() function reads all types of data values. it is used for runtime assignment of varilables. It reads the input data from the console.
scanf("%d %f %c",&a,&b,&c);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.The scanf() funtcion requires '&' operator called address operator. the address operator prints the memory location of the variable. the scanf() function also returns values. the return value is equal to the number of values correctly used.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter value of A : ");
scanf("%d", &a);
printf("%d",a);
getch();
}
It gets input from the user and prints the cube of the given number.
#include<stdio.h>
int main(){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.
#include<stdio.h>
int 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;
}
enter first number:9 enter second number:9 sum of 2 numbers:18
Trending Tutorials