Explain Input and Output Statement in c
Explain Input and Output Statement in c
current context (Tuesday, April 15, 2025, 1:49 PM IST, India). Input and output operations are
essential for any program to interact with the outside world, whether it's receiving data from the
user, reading from files, or displaying results.
In C, the standard input/output library (stdio.h) provides a set of functions for performing these
operations. Here are the most commonly used input and output functions:
Output Functions:
1. printf() (Formatted Output):
○ Header File: <stdio.h>
○ Purpose: Sends formatted output to the standard output stream (usually the
console).
○ Syntax:
printf("format string", argument1, argument2, ...);
○ Argument: The character to be written (passed as an int, but only the lower byte is
used).
○ Return Value: Returns the character written on success, or EOF (End Of File) on
error.
○ Example:
#include <stdio.h>
int main() {
putchar('H');
putchar('i');
putchar('\n'); // Newline character
return 0;
}
Output:
Hi
Input Functions:
1. scanf() (Formatted Input):
○ Header File: <stdio.h>
○ Purpose: Reads formatted input from the standard input stream (usually the
keyboard) and stores it in the provided variables.
○ Syntax:
scanf("format string", &variable1, &variable2, ...);
○ Format String: Similar to printf(), it contains format specifiers that indicate the type
of data to be read.
○ Arguments: Pointers to the variables where the read values should be stored. It's
crucial to use the address-of operator & before the variable names for basic
data types.
○ Common Format Specifiers (for input):
■ %d or %i: Integer
■ %f: Float
■ %lf: Double
■ %c: Character (reads the next character, including whitespace)
■ %s: String (reads a sequence of non-whitespace characters up to a
whitespace, null-terminates the string)
■ %u: Unsigned integer
■ %x: Hexadecimal integer
■ %o: Octal integer
○ Important Notes:
■ scanf() stops reading input for a format specifier when it encounters
whitespace or a character that does not match the expected format.
■ Be cautious with %s as it can lead to buffer overflows if the input string is
longer than the allocated size of the character array. Consider using field
width limits (e.g., %19s for a character array of size 20).
■ scanf() returns the number of input items successfully matched and assigned,
or EOF if an input failure occurs before any conversion.
○ Examples:
#include <stdio.h>
int main() {
int age;
float height;
char initial;
char name[20];
printf("Enter your age, height, initial, and name: ");
int result = scanf("%d %f %c %19s", &age, &height,
&initial, name);
if (result == 4) {
printf("Age: %d, Height: %.2f, Initial: %c, Name:
%s\n", age, height, initial, name);
} else {
printf("Input format error.\n");
}
return 0;
}
○ Return Value: Returns the character read (as an int), or EOF if the end of file is
encountered or an error occurs.
○ Example:
#include <stdio.h>
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c (ASCII value: %d)\n", ch, ch);
return 0;
}
○ Argument: A pointer to a character array where the input string will be stored.
○ Return Value: Returns the pointer to the string on success, or NULL on error or if
end-of-file is encountered before any characters are read.
○ **Security Risk: gets() does not perform any bounds checking. If the input line is
longer than the size of the character array, it can lead to buffer overflows, which are
serious security vulnerabilities. Therefore, gets() is highly discouraged and
should generally be avoided. Use safer alternatives like fgets() instead.
4. fgets() (Safer String Input):
○ Header File: <stdio.h>
○ Purpose: Reads at most n-1 characters from the standard input into the character
array pointed to by s. It stops reading if a newline character is encountered, which
is stored in the array, or if end-of-file is reached. The array is always
null-terminated.
○ Syntax:
char *fgets(char *s, int n, FILE *stream);
○ Arguments:
■ s: Pointer to the character array to store the input.
■ n: Maximum number of characters to read (including the null terminator).
■ stream: The input stream to read from (for standard input, use stdin).
○ Return Value: Returns s on success, or NULL on error or if end-of-file is
encountered while no characters have been read.
○ Example:
#include <stdio.h>
int main() {
char buffer[50];
printf("Enter a line of text: ");
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
printf("You entered: %s", buffer);
} else {
printf("Error reading input or end of file.\n");
}
return 0;
}