Input function:
In the C programing language, the input is directed by the scanf function which is used to take input from the user. It basically reads formatted input from standard input for ex: keyboard.
Syntax Of scanf():
scanf("format-string",&reference-variable);
So, to take input from the user there are certain format specifiers provided which works in the scanf function, they are:
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
unsigned int | %u |
long int | %li |
long long int | %lli |
unsigned long int | %lu |
unsigned long long int | %llu |
signed char | %c |
unsigned char | %c |
long double | %lf |
Output function:
In the C programing language, the output function is directed by the printf function which returns an output as directed in the program. The printf function also takes the format specifiers to show the value of the variable in the output section.
Syntax of printf():
//For print statements printf("statements without variable."); //For print variables printf("format-strings",name-of-variable-to-print);
Examples for input and output functions:
Integer:
#include<stdio.h> int main() { int a; printf("Enter an Integer value"); scanf("%d",&a); printf("The value of the Integer %d",a); return 0; }
Here, integer input and output function works on the %d format specifier.
Screenshot Provided By Soham Malakar
Float:
#include<stdio.h> int main() { float a; printf("Enter an float value"); scanf("%f",&a); printf("The value of the float %f",a); return 0; }
Here, float input and output function works with %f format specifier.
Screenshot Provided By Soham Malakar
Char:
#include<stdio.h> int main() { char a; printf("Enter an char value"); scanf("%c",&a); printf("The value of the char %c",a); return 0; }
Here, the char input and output function works with %c format specifier.
Screenshot Provided By Soham Malakar
Double:
#include<stdio.h> int main() { double a; printf("Enter an double value"); scanf("%lf",&a); printf("The value of the double %lf",a); return 0; }
Here, the double input and output function works with %lf format specifier.
Screenshot Provided By Soham Malakar
We can also use the above data type in a single program i.e. we can use multiple input and output functions. For example:
#include<stdio.h> int main() { int a; char b; double c; float d; printf("Enter the value of char variable"); scanf("%c",&b); printf("Enter the value of integer variable"); scanf("%d",&a); printf("Enter the value of double variable"); scanf("%lf",&c); printf("Enter the value of float variable"); scanf("%f",&d); printf("\nThe value of integer variable %d",a); printf("\nThe value of char variable %c",b); printf("\nThe value of double variable %lf",c); printf("\nThe value of float variable %f",d); return 0; }
Screenshot Provided By Soham Malakar
Hence we come to the end of the discussion on input and output functions in the C language. Hope your doubts have been cleared.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.