Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

In C, unformatted input/output functions deal with raw data without any specific formatting. These functions are part of the stdio.h (standard input-output) library and are generally more basic compared to the formatted counterparts. Here are some commonly used unformatted input/output functions in C:

Unformatted Output Functions (putchar, putc, puts, fwrite):

1. putchar: Writes a character to the standard output

#include <stdio.h>

int main() {
    char ch = ‘A’;
    putchar(ch);
    return 0;
}

Output:

A

2. putc: Writes a character to a specified stream.

#include <stdio.h>

int main() {
    char ch = ‘A’;
    putc(ch, stdout);  // Writes to the standard output
    return 0;
}

Output:

A

3. puts: Writes a string to the standard output, followed by a newline.

#include <stdio.h>

int main() {
    char str[] = “Hello, World!”;
    puts(str);
    return 0;
}

Output:

Hello, World!

4. fwrite: Writes data to a stream without any formatting.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    FILE *file = fopen(“output.bin”, “wb”);
    if (file != NULL) {
        fwrite(arr, sizeof(int), 5, file);
        fclose(file);
    }
    return 0;
}

Output:

This example writes an array of integers to a binary file (output.bin) without any formatting.

Unformatted Input Functions (getchar, getc, fgets, fread):

1. getchar: Reads a character from the standard input.

#include <stdio.h>

int main() {
    char ch = getchar();
    printf(“You entered: %cn”, ch);
    return 0;
}

Input:

A

Output:

You entered: A

2. getc:Reads a character from a specified stream.

#include <stdio.h>

int main() {
    char ch = getc(stdin);  // Reads from the standard input
    printf(“You entered: %cn”, ch);
    return 0;
}

Input:

B

Output:

You entered: B

3. fgets: Reads a line from a file or stream.

#include <stdio.h>

int main() {
    char buffer[100];
    FILE *file = fopen(“input.txt”, “r”);
    if (file != NULL) {
        fgets(buffer, sizeof(buffer), file);
        fclose(file);
        printf(“Read from file: %s”, buffer);
    }
    return 0;
}

Input (input.txt):

Hello, File Input!

Output:

Read from file: Hello, File Input!

4. fread: Reads data from a file or stream without any formatting.

#include <stdio.h>

int main() {
    int arr[5];
    FILE *file = fopen(“input.bin”, “rb”);
    if (file != NULL) {
        fread(arr, sizeof(int), 5, file);
        fclose(file);

        // Display the values read from the binary file
        for (int i = 0; i < 5; i++) {
            printf(“%d “, arr[i]);
        }
    }
    return 0;
}

Input (input.bin):

1 2 3 4 5

Output:

1 2 3 4 5