Data types in C language
input/output (I/O) functions are used for reading input data from the user and displaying output data to the user. There are several types of I/O functions in C language that are used to handle different types of input and output operations.
Standard Input/Output Functions:
Standard Input/Output functions are used to handle input and output operations from the keyboard and screen respectively. These functions are defined in the standard library “stdio.h”.
Example:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is: %d", age);
return 0;
}
Output:
Enter your age: 16
Your age is: 16
In this example, the “scanf()” function is used to take input from the user and the “printf()” function is used to display output to the user.
File Input/Output Functions:
File I/O functions are used to handle input and output operations from files. These functions are defined in the standard library “stdio.h”. The common file I/O functions are “fopen()”, “fclose()”, “fread()”, “fwrite()”, “fgets()”, “fputs()” etc.
Example:
#include <stdio.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen("test.txt", "w");
printf("Enter a string: ");
gets(str);
fprintf(fp, "%s", str);
fclose(fp);
printf("Data written successfully!");
return 0;
}
Output:
Enter a string: Hello, world!
Data written successfully!
In this example, the “fopen()” function is used to open a file in write mode, “fprintf()” function is used to write data to the file, and “fclose()” function is used to close the file.
Console Input/Output Functions:
Console I/O functions are used to handle input and output operations from the console. These functions are defined in the standard library “conio.h”. The common console I/O functions are “getch()”, “getche()”, “putch()”, “cprintf()”, “cscanf()” etc.
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Press any key to continue: ");
ch = getch();
printf("\nYou have pressed: %c", ch);
return 0;
}
Output:
Press any key to continue:
You have pressed: a
In this example, the “getch()” function is used to read a single character from the console without echoing to the screen.
Formatted Input/Output Functions:
Formatted I/O functions are used to format the output data and input data from the user. The common formatted I/O functions are “printf()”, “scanf()”, “sprintf()”, “sscanf()”, “fprintf()”, “fscanf()” etc.
Example:
#include <stdio.h>
int main()
{
int num = 123;
printf("The value of num is: %d", num);
return 0;
}
Output:
The value of num is: 123
In this example, the “printf()” function is used to display the value of the variable “num” on the screen in decimal format.
Unformatted Input/Output Functions:
Unformatted I/O functions are used to read or write binary data from a file or device. The common unformatted I/O functions are “fread()”, “fwrite()”, “getc()”, “putc()”, “feof()”, “ferror()” etc.
Example:
#include <stdio.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen("test.txt", "rb");
fread(str, sizeof(str), 1, fp);
fclose(fp);
printf("Data read from the file: %s", str);
return 0;
}
Output:
Hello World
Assuming the “test.txt” file exists and contains some data, the program would open the file in binary mode using the fopen() function and assign the file pointer to the fp variable. The program would then read the contents of the file using the fread() function, which takes four arguments: a pointer to the data buffer, the size of each element to be read, the number of elements to read, and the file pointer. In this case, the program would read one element of size sizeof(str) from the file into the str buffer. Finally, the program would close the file using the fclose() function and display the data read from the file using the printf() function.
Overall, input/output functions are essential in any programming language, and in C they provide a powerful way to interact with the user and with files.