0% found this document useful (0 votes)
9 views

Input & Output in C Language (Functions & Operations With Example)

Uploaded by

brothers.mullai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Input & Output in C Language (Functions & Operations With Example)

Uploaded by

brothers.mullai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Home C Tutorial | Learn C Programming Language Input &…

Input & Output in C Language (Functions &


Operations With Example)
30 mins read Last updated: 05 Jan 2025 259 views

Share 2.56%

Table of Contents

Introduction
Input in C
Output in C
Input and Output Functions in C Programming
Input Functions in C
Output Functions in C
What is File Input/Output in C?
File Input Output in C (Functions to Handle Files)
Formatted and Unformatted Input/Output in C
Difference Between Formatted and Unformatted Input Output in C

Introduction
Input-output operations in C are essential for interacting with users and
handling data. These operations allow a program to take input from the user
or external files and display results.

The input-output statements in C are part of the standard library, enabling


programmers to perform tasks like reading data from the console, writing to
the console, and managing files efficiently.

Learning these functions is crucial for creating interactive and data-driven


programs. So here, we’ll explore various input and output operations, their
functions, and examples to help you use them effectively in your C
programs.

Input in C
Input in C programming means gathering data from the user or external
sources for processing. The most common way is through the console,
where functions like scanf() are used for formatted input, and getchar()
reads a single character.

Additionally, data can be read from files using functions like fscanf() and
fgets().

For example, scanf("%d", &num); takes an integer input from the


user.
Output in C
Output in C programming means displaying results or information from a
program on the console or writing it to a file. Console output functions like
printf() allow formatted data to be printed, while putchar() is used for single
characters. File output operations in C involve writing data to a file using
functions like fprintf() and fputs().

For example, printf("Result: %d", result); displays the value of a


variable.
Input and Output Functions in C Programming
Below is the list of input and output functions in C language:

Function Category Description


scanf() Input Reads formatted input (e.g., integers, floats,
(Console) strings) from the user.
getchar() Input Reads a single character from the user.
(Console)
gets() Input Reads a string from the user (deprecated due
(Console) to safety issues).
fscanf() Input (File) Reads formatted data from a file.
fgets() Input (File) Reads a line of text from a file.
printf() Output Prints formatted output to the console.
(Console)
putchar() Output Outputs a single character to the console.
(Console)
puts() Output Outputs a string to the console.
(Console)
fprintf() Output (File) Writes formatted data to a file.
fputs() Output (File) Writes a string to a file.
fopen() File Operation Opens a file for reading, writing, or
appending.
fclose() File Operation Closes an opened file.
fread() Input (File) Reads binary data from a file.
fwrite() Output (File) Writes binary data to a file.
feof() File Operation Checks if the end of a file has been reached.
perror() Output (Error) Prints an error message to the standard error
stream

Input Functions in C
Below is a detailed explanation of common input functions in C
programming:
1. scanf()
Reads formatted input from the user, such as integers, floats, and strings.
Example:

int num;
printf("Enter a number: ");
scanf("%d", &num); // Takes integer input
printf("You entered: %d\n", num);

2. getchar()
Reads a single character from the user.
Example:

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

3. gets()
Reads a string from the user, including spaces (deprecated due to safety
concerns).
Example:
char str[50];
printf("Enter a string: ");
gets(str); // Reads the entire string
printf("You entered: %s\n", str);

4. fscanf()
Reads formatted data from a file.
Example:

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


int num;
fscanf(file, "%d", &num); // Reads an integer from the fil
printf("The number in the file is: %d\n", num);
fclose(file);

5. fgets()
Reads a line of text from a file or the console.
Example:

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


char line[100];
fgets(line, 100, file); // Reads a line from the file
printf("The line in the file is: %s\n", line);
fclose(file);

Output Functions in C
Below is the explanation of common output functions in C programming:
1. printf()
Prints formatted output to the console.
Example:

int num = 10;


printf("The number is: %d\n", num); // Prints the value of

2. putchar()
Outputs a single character to the console.
Example:
char ch = 'A';
putchar(ch); // Outputs the character 'A'
printf("\n");

3. puts()
Outputs a string to the console with a newline at the end.
Example:

char str[] = "Hello, World!";


puts(str); // Prints the string with a newline

4. fprintf()
Writes formatted data to a file.
Example:

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


int num = 20;
fprintf(file, "The number is: %d\n", num); // Writes to th
fclose(file);

5. fputs()
Writes a string to a file.
Example:

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


fputs("This is a test string.", file); // Writes the strin
fclose(file);

Key Notes:

Input functions like scanf() and fgets() are used to take data from the
user or files.

Output functions like printf() and fprintf() are used to display or save
data.
Always use fopen() and fclose() when working with files to ensure
proper file handling.

What is File Input/Output in C?


File Input/Output in C allows programs to interact with files for reading and
writing data. Using file handling functions like fopen(), fclose(), fscanf(), and
fprintf(), you can store data persistently or retrieve it for processing.

File input-output in C language is crucial for managing large datasets and


ensuring data can be saved beyond program execution.

File Input Output in C (Functions to Handle Files)


1. fopen()
Opens a file for reading, writing, or appending.

FILE *file = fopen("example.txt", "w"); // Opens a file fo


if (file == NULL) {
printf("Error opening file.\n");
} else {
printf("File opened successfully.\n");
fclose(file); // Close the file
}

2. fclose()
Closes an opened file to free resources.

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


if (file != NULL) {
fclose(file); // Closes the file
}

3. fprintf()
Writes formatted data to a file.

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


fprintf(file, "Hello, File I/O in C!\n"); // Write data
fclose(file);

4. fscanf()
Reads formatted data from a file.
FILE *file = fopen("example.txt", "r");
char str[50];
fscanf(file, "%s", str); // Read data
printf("File content: %s\n", str);
fclose(file);

5. fgets() and fputs()


Reads and writes strings.

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


fputs("This is a test string.", file); // Write string
fclose(file);

file = fopen("example.txt", "r");


char line[100];
fgets(line, 100, file); // Read string
printf("File content: %s\n", line);
fclose(file);

Formatted and Unformatted Input/Output in C


Formatted and Unformatted Input/Output in C programming are two
categories of I/O operations. They differ in how data is read or written and
the level of control over the formatting.
1. Formatted Input/Output in C
Formatted Input/Output allows you to control how data is read or displayed
using format specifiers. You can format numbers, strings, or characters with
specific rules, making it versatile for structured data.
Functions Used:

Input - scanf()
- fscanf()
Output - printf()
- fprintf()

Example of Formatted Input in C:

int age;
float salary;
printf("Enter your age and salary: ");

You might also like