C INPUT/OUTPUT
In this tutorial, We learn about c input and output with printf() and scanf() functions, covers topics like syntax, c library in which these functions present, also take the program.
To use both functions printf() & scanf(), we have to include stdlib.h header file because these functions are predefined in stdlib.h header file. We use #include<stdlib.h> statement.
C Output
We can use output function when we have to display data on screen, write in file, or printer.
C OUTPUT |
Integer Output
#include<stdlib.h>
int main() {
int x=10;
printf("int value=%d",x);
return 0;
}
We write anything inside quotations but when we use format specifier like %d, %f, %c, %s, etc. i.e. format specifier replaced with the value of variable.
In this case, printf("int value=%d",x); x is variable which store value in it then value is replaced with %d. From the above example, the output will be
int value=10
int main() {
int x=10;
printf("int value=%d",x);
return 0;
}
int value=10
Float Input
#include<stdlib.h>
int main() {
float x=2.5;
printf("float value=%f",x);
return 0;
}
In this case, printf("float value=%f",x); x is variable which store value in it then value is replaced with %f.
int main() {
float x=2.5;
printf("float value=%f",x);
return 0;
}
In this case, printf("float value=%f",x); x is variable which store value in it then value is replaced with %f.
Char Input
#include<stdlib.h>
int main() {
char ch='y';
printf("char vlaue=%c",ch);
return 0;
}
In this case, printf("char value=%c",ch); ch is variable which store value in it then value is replaced with %c.
int main() {
char ch='y';
printf("char vlaue=%c",ch);
return 0;
}
In this case, printf("char value=%c",ch); ch is variable which store value in it then value is replaced with %c.
C Input
We can use input function when we have to take data from keyboard, files, or command line to feed in the program for further processing.As we learn in C Data Types that it takes different types of data from users as the program required. So, Input takes with a different format specifier for different data types like int, float, char, etc.
C INPUT |
Integer Input
#include<stdlib.h>int main() {
int x;
scanf("%d",&x);
printf("int value=%d",x);
return 0;
}
we use %d format specifier in double quotations which means it stores integer data and &x contains the address of x and enter value stored in that address.
Float Input
#include<stdlib.h>int main() {
float x;
scanf("%f",&x);
printf("float value=%f",x);
return 0;
}
we use %f format specifier in double quotations which means it takes float data.
Char Input
#include<stdlib.h>int main() {
char ch;
scanf("%c",&ch);
printf("char vlaue=%c",ch);
return 0;
}
we use %c format specifier in quotations which means it stores character data.
Format Specifiers for Input/Output
Here I provide a list of some format specifiers
- %d for integer
- %f for float
- %lf for double
- %c for character
- %s for string
Special Case Of String Input Output
Below code, you notice that I will not take &str with scanf() in case of string only.
#include<stdlib.h>
int main(){
char str[10];
printf("Enter String str:");
scanf("%s",str);
printf("String str: %s",str);
return 0;
}
Because C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.
Tags:
C