C Programs Tutorials | IT Developer
IT Developer

C Programming - C Input Output



Share with a Friend

C Programming - C Input Output

C Input and Output (I/O)

In C programming, input and output (I/O) operations are performed using functions from the C standard library, mainly provided in the <stdio.h> header file. The functions for input and output handle the interaction between the program and the user or the system, allowing for data input (from the user, files, etc.) and output (to the screen, files, etc.).

Standard Input and Output Functions

  1. printf() - Output to the Console (Screen)
    • The printf() function is used to display output on the console (screen).

Syntax:

C

int printf(const char *format, ...);

    • format: A string that specifies how the output should be formatted. It can contain placeholders (format specifiers) like %d, %f, %s, etc.
    • The printf() function returns the number of characters printed.

Example:

C

#include <stdio.h>

int main() {

    printf("Hello, World!\n"); // Prints "Hello, World!"

    return 0;

}

Output:

Hello, World!

  1. scanf() - Input from the Console (Keyboard)
    • The scanf() function is used to read input from the user.

Syntax:

C

int scanf(const char *format, ...);

    • format: A string that specifies the expected type of input. Format specifiers are used to match the input type (e.g., %d for integers, %f for floats, %s for strings).
    • The scanf() function returns the number of successful inputs.

Example:

C

#include <stdio.h>

int main() {

    int age;

    printf("Enter your age: ");

    scanf("%d", &age); // Input an integer

    printf("Your age is: %d\n", age);

    return 0;

}

Output:

Enter your age: 25

Your age is: 25

Format Specifiers

  • %d: Integer (signed decimal)
  • %f: Floating-point numbers
  • %s: String of characters
  • %c: Single character
  • %x: Hexadecimal integer
  • %p: Pointer address
  • %lf: Double precision floating-point number

Formatted Input and Output

You can use the format specifiers with printf() and scanf() to control the formatting of the data being printed or read.

Example: Formatted Output

C

#include <stdio.h>

int main() {

    int age = 25;

    float height = 5.9;

    // Displaying formatted output

    printf("Age: %d years\n", age);

    printf("Height: %.2f feet\n", height);

    return 0;

}

Output:

Age: 25 years

Height: 5.90 feet

Example: Formatted Input

C

#include <stdio.h>

int main() {

    int num;

    float price;

    // Input with formatted specifiers

    printf("Enter an integer: ");

    scanf("%d", &num);

    printf("Enter the price: ");

    scanf("%f", &price);

    printf("Integer: %d, Price: %.2f\n", num, price);

    return 0;

}

Example Input:

Enter an integer: 10

Enter the price: 20.5

Output:

Integer: 10, Price: 20.50

Input and Output with Strings

  1. gets(): Reads a line of text (deprecated due to security risks like buffer overflow)
    • Syntax: char *gets(char *str);
    • Reads a string until a newline or the end of the file is encountered.
    • It's no longer recommended due to its vulnerability to buffer overflow attacks. Use fgets() instead.
  2. fgets(): Reads a line of text with a specified limit.
    • Syntax: char *fgets(char *str, int n, FILE *stream);
    • Safer alternative to gets(). It reads up to n-1 characters and appends a null character at the end.

Example of fgets() usage:

C

#include <stdio.h>

int main() {

    char name[100];

    printf("Enter your name: ");

    fgets(name, sizeof(name), stdin); // Safe input for strings

    printf("Hello, %s", name);

    return 0;

}

File Input and Output

In addition to standard input and output, C provides functions for file manipulation. These functions allow reading from and writing to files.

  1. fopen(): Opens a file.
    • Syntax: FILE *fopen(const char *filename, const char *mode);
    • Modes include:
      • "r": Open for reading.
      • "w": Open for writing (creates a new file or overwrites an existing file).
      • "a": Open for appending data to the end of the file.
  2. fprintf(): Writes formatted output to a file.
    • Syntax: int fprintf(FILE *stream, const char *format, ...);
  3. fscanf(): Reads formatted input from a file.
    • Syntax: int fscanf(FILE *stream, const char *format, ...);
  4. fclose(): Closes a file.
    • Syntax: int fclose(FILE *stream);

Example: Writing to a File

C

#include <stdio.h>

int main() {

    FILE *file = fopen("output.txt", "w");

    if (file != NULL) {

        fprintf(file, "This is written to the file.\n");

        fclose(file);

    } else {

        printf("File opening failed.\n");

    }

    return 0;

}

Example: Reading from a File

C

#include <stdio.h>

int main() {

    FILE *file = fopen("output.txt", "r");

    char buffer[100];

    if (file != NULL) {

        fscanf(file, "%s", buffer);

        printf("Read from file: %s\n", buffer);

        fclose(file);

    } else {

        printf("File opening failed.\n");

    }

    return 0;

}

Conclusion

C provides robust I/O functions, allowing interaction with the user and the system through standard and file-based input/output. Using functions like printf(), scanf(), and file handling functions, C allows for versatile and efficient data manipulation.

  • Standard I/O: printf(), scanf()
  • File I/O: fopen(), fclose(), fprintf(), fscanf()
  • String Input/Output: gets() (deprecated), fgets()
  • Formatted I/O: Using format specifiers like %d, %f, %s, etc.