C Basic I/O Functions

Basic Input and Output func.

I/O Functions

In C, "I/O" stands for Input/Output, which refers to the process of transferring data between a program and the external world, such as reading data from a file, keyboard, or network, and writing data to a file, screen, or network.

Input and output operations are typically performed using a set of standard library functions. These functions allow you to read data from the keyboard, display information on the screen, and perform file operations. Here are some of the basic input and output functions in C:

printf()

This function is used for formatted output. It allows you to print text and data to the standard output (usually the console). You can use format specifiers to specify how data should be formatted.

printf("Hello, world!\n");
int num = 42;
printf("The value of num is %d\n", num);

scanf()

This function is used for formatted input. It allows you to read data from the standard input (usually the keyboard) and store it in variables based on format specifiers.

int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);

getchar()

This function reads a single character from the standard input. It's often used for simple character input.

char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n", ch);

putchar()

This function is used to write a single character to the standard output.

char ch = 'A';
putchar(ch);

gets()

This function reads a line of text from the standard input.

char str[50];
printf("Enter a string: ");
gets(str);
printf("You entered: %s\n", str);

puts()

This function function writes a string to the standard output.

char str[] = "Hello, C programming!";
puts(str);

fgets()

This function used for reading strings along with files.

char str[50];
FILE *file = fopen("example.txt", "r");
if (file != NULL) {
    fgets(str, sizeof(str), file);
    fclose(file);
}

fputs()

This function used for writing strings along with files.

FILE *file = fopen("output.txt", "w");
if (file != NULL) {
    fputs("Hello, file handling!", file);
    fclose(file);
}

* Those are commonly used for input and output (I/O) functions in modern c programming.

exit();

The exit() function is used to terminate a program or process explicitly. It is part of the C standard library, and it is declared in the stdlib.h header file. exit() takes an integer argument, which is used as the exit status of the program. A return value of 0 typically indicates a successful execution, while a non-zero value is often used to indicate an error or abnormal termination.

When exit() is called, it performs some cleanup tasks (such as closing open files) and then terminates the program, returning control to the operating system.

Example of using exit():

c Copy Code
#include<stdio.h>
#include<stdlib.h>

int main() {
    // Perform some operations
    // ...

    // Terminate the program with exit status 0 (success)
    exit(0);
}

clrscr();

clrscr() is not a standard C library function. It is a function often used in some older compilers and systems (like Borland's Turbo C/C++ IDE) to clear the screen or console window.

It is not a part of the C standard, and its behavior may vary between different systems and compilers.

Example of using clrscr() (in non-standard systems):

c Copy Code
#include<stdio.h>
#include<conio.h>

int main() {
    // Perform some operations
    // ...

    // Clear the screen (non-standard)
    clrscr();
    
    // Continue with other operations
    // ...

    return 0;
}

Note: clrscr() is not portable and should be avoided in modern C programming. Instead, if you need to clear the screen, consider using platform-specific functions or libraries or using ANSI escape sequences to clear the terminal/console, which are more standard and portable.

printf("\033[2J\033[H"); // Clear the screen

What is New?

We have just updated this website & still working on adding new content & updating existing features.