Last Updated on July 12, 2021 by Rahul
C Input Output I/O In C
‘Welcome in Study Learner‘
Today i will teach you about Input Output(I/O) in c
Here we will write C program to use scanf() function to take input from the user, printf() function to display output to the user.
Syntax:
#include<stdio.h> //include header file
int main() //main function
{ //body open
scanf() //scan value from the user
printf() // print or display value to the user
} //body close
- Intger Input/Output
#include<stdio.h>
int main()
{
int testvalue;
printf("Enter a value:");
scanf("%d", &testvalue);
printf("Entered value = %d", testvalue);
return 0;
}
Output:-
Output
Enter a value:45
Entered value = 45
How will the program work? (C Input Output I/O In C)
(C Input Output I/O In C)
- include :- The #include is a preprocessor command that tells the compiler to include the contents of stdio.h(standred input and output) file in program.
- int main :- Here main() is the function name and int is return type of this function. Every C program must have this function because the executioin of program begins with the main() function.
- printf() :- printf() is a library function to send formatted output to the screen.
- scanf() :- scanf() is a another library function to take input from user.
- scanf(“%d”) :- %d is format specifier inside the scanf() function to take intger input from the user. When the user enters an integer, it is stored in the ‘testvalue’ variable.
- &testvalue :- &testvalue gets the address of ‘testvalue’ variable , and the value entered by the user is stored in that address.
- printf(“%d”) :- %d is format specifier inside the printf() which display the value stored in the ‘testvalue’ variable.
- return 0; :- return 0 means successful execution of main() function.
2. Float Input/Output:
#include<stdio.h>
int main()
{
float testvalue;
printf("Enter a float value:");
scanf("%f", &testvalue);
printf("Float value is %f", testvalue);
return 0;
}
Output:-
Output
Enter a float value:45.34
Float value is 45.340000
Here %f is format specifier for float
3. Double Input/Output:
#include <stdio.h>
int main()
{
double testvalue;
printf("Enter a value: ");
scanf("%lf", &testvalue);
printf("Double value is %lf", testvalue);
return 0;
}
Output:-
Output
Enter a value: 32.34
Double value is 32.340000
Here %f is format specifier for double
4. Char Input/Outpu:
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character:");
scanf("%c", &ch);
printf("character is %c", ch);
return 0;
}
Output:-
Output
Enter a character:A
character is A
Here %c is format specifier for character
5. String Input/Output
#include<stdio.h>
int main()
{
char name[20];
printf("Enter a string:");
scanf("%s", name);
printf("String is %s", name);
return 0;
}
Output:-
Output
Enter a string:Study_Learner
String is Study_Learner
Here %s is format specifier for string
(C Input Output I/O In C)