I/O
FUNCTIONS IN C
Input/Output (I/O)
functions in C are used to perform input and output operations between a
program and the user or the system. Some of the commonly used I/O functions in
C are:
printf(): This
function is used to print formatted output to the console or terminal. It takes
a format string and a list of arguments, which are used to format the output.
Example 01:
#include
<stdio.h>
int main() {
char name[] = "John";
int age = 25;
printf("My name is %s and I am %d years
old.\n", name, age);
return 0;
}
Output: My name is John and I am 25
years old.
scanf():
This function is used to read formatted input from the console or terminal. It
takes a format string and a list of pointers to the variables where the input
should be stored.
Example 02:
#include
<stdio.h>
int main() {
char name[20];
int age;
printf("Enter your name and age:
");
scanf("%s %d", name, &age);
printf("Your name is %s and you are %d
years old.\n", name, age);
return 0;
}
Output:
Enter your name and
age: John 25
Your name is John and
you are 25 years old.
getchar():
This function is used to read a single character from the console or terminal.
It waits for the user to press a key and returns the ASCII code of the
character.
Example 03:
#include
<stdio.h>
int main() {
char c;
printf("Enter a character: ");
c = getchar();
printf("You entered the character %c,
which has ASCII code %d.\n", c, c);
return 0;
}
Output:
Enter a character: A
You entered the
character A, which has ASCII code 65.
putchar():
This function is used to write a single character to the console or terminal.
It takes an ASCII code as an argument and writes the corresponding character.
Example 04:
#include
<stdio.h>
int main() {
char c = 'A';
putchar(c);
return 0;
}
Output: A
gets():
This function is used to read a string of characters from the console or
terminal. It reads characters from the input until it encounters a newline
character or the end of the input.
Example 05:
#include
<stdio.h>
int main() {
char name[20];
printf("Enter your name: ");
gets(name);
printf("Your name is %s.\n",
name);
return 0;
}
Output:
Enter your name: John
Your name is John.
Note: The gets()
function is considered unsafe because it can cause a buffer overflow if the
input string is longer than the size of the buffer.
It is recommended to
use the fgets() function instead.
#include
<stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt",
"w");
fprintf(fp, "This is some text in the
file.\n");
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in write mode ("w") using the
fopen() function. The fprintf()
function is then used to write some text to the file. Finally, the fclose()
function is used to close the file.
Perform input/output
operations: Once the file is opened, you can perform input/output operations on
the file using functions such as fgetc(), fgets(), fputc(), fputs(),
fprintf(), and fscanf().
Example 07:
#include
<stdio.h>
int main() {
FILE *fp;
char buffer[20];
fp = fopen("file.txt",
"r");
fgets(buffer, 20, fp);
printf("The text in the file is:
%s\n", buffer);
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in read mode ("r") using the
fopen() function. The fgets() function is then used to read a line of text from
the file into the buffer. Finally, the printf() function is used to print the text
that was read from the file.
Close the file:
Once you have finished performing input/output operations on the file, you
should close the file using the fclose() function.
Example 08:
#include
<stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt",
"a");
fprintf(fp, "This text is appended to
the file.\n");
fclose(fp);
return 0;
}
In this example, the
file "file.txt" is opened in append mode ("a") using the
fopen() function. The fprintf() function is then used to append some text to
the end of the file. Finally, the fclose() function is used to close the file.
Note: It is important
to check whether the file was opened successfully before performing
input/output operations on the file. The fopen() function returns NULL if it is
unable to open the file, for example if the file does not exist or if the user
does not have permission to access the file.
0 Comments