Input / Output statements -\
Input refers to the values that we insert in our program (user defined values). Output refers to the value that the compiler displays after the compilation.
We can insert input and output commands through various operators in C.
1. SCANF() and PRINTF()
syntax -
int a;
scanf("%d",&a); ( %d = data type
& = assign the address of the file.)
Printf is used to display the outcome -
syntax -
printf("%d",a);
suppose we have to enter a value and print that.
#include<stdio.h>
int main()
{
int a;
printf("Enter the value = ") //this will simply print the text.
scanf("%d",&a); //For accepting the value
printf("%d",a); //Prints the inserted value
return 0; // For ending the program
}
sample input -
10
sample output -
Enter the value = 10
10
2. getchar() and putchar() -
The getchar() reads the variable entered . It only reads one character at a time.The putchar() displays the value entered.
program syntax -
#include<stdio.h>
int main()
{
int a;
printf("Enter the character") // Print the statement
a = getchar () // Reads the value entered
putchar (a);
return 0;
}
3. gets() and puts() -
It is like getchar and putchar. It just works on strings rather than character.So it can read more than a single character.
program syntax -
#include<stdio.h>
int main()
{
char a[100]; //100 is the no of characters that can be inserted.
printf("Enter the value ");
gets (a);
puts (a);
getch();
}
Difference between scanf() and gets() -
scanf() reads till any space comes then it breaks but gets() reads the space too.for example - scanf("%d",&a);
input - Beginner Programmer
output - Beginner
gets(a)
input - Beginner Programmer
output - Beginner programmer
0 Comments