[go: up one dir, main page]

0% found this document useful (0 votes)
2 views5 pages

Explain Input and Output Statement in c

The document provides an overview of input and output (I/O) statements in C programming, detailing functions from the standard input/output library (stdio.h) such as printf(), scanf(), and fgets(). It emphasizes the importance of these functions for user interaction and data handling, while also highlighting security risks associated with certain functions like gets(). Additionally, it mentions file handling functions for reading and writing data to files.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Explain Input and Output Statement in c

The document provides an overview of input and output (I/O) statements in C programming, detailing functions from the standard input/output library (stdio.h) such as printf(), scanf(), and fgets(). It emphasizes the importance of these functions for user interaction and data handling, while also highlighting security risks associated with certain functions like gets(). Additionally, it mentions file handling functions for reading and writing data to files.

Uploaded by

kk1982johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Alright, let's discuss input and output (I/O) statements in C programming, keeping in mind our

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, ...);​

○​ Format String: A string literal that can contain:


■​ Plain text: Which is printed directly.
■​ Format specifiers: Placeholders that indicate how the corresponding
arguments should be formatted and printed.
○​ Arguments: Zero or more variables, constants, or expressions whose values are to
be printed according to the format specifiers.
○​ Common Format Specifiers:
■​ %d or %i: Integer (signed decimal)
■​ %f: Floating-point number (decimal notation)
■​ %lf: Double-precision floating-point number (decimal notation)
■​ %c: Single character
■​ %s: String (null-terminated character array)
■​ %u: Unsigned decimal integer
■​ %x or %X: Unsigned hexadecimal integer (lowercase or uppercase letters)
■​ %o: Unsigned octal integer
■​ %p: Pointer address
■​ %%: Prints a literal percent sign (%)
○​ Examples:​
#include <stdio.h>​

int main() {​
int age = 30;​
float salary = 50000.75;​
char initial = 'J';​
char name[] = "Alice";​

printf("Name: %s, Initial: %c, Age: %d, Salary: %.2f\n",
name, initial, age, salary);​
printf("Age in hexadecimal: %x\n", age);​
printf("Literal percent: %d%%\n", 90);​

return 0;​
}​
Output:​
Name: Alice, Initial: J, Age: 30, Salary: 50000.75​
Age in hexadecimal: 1e​
Literal percent: 90%​

2.​ putchar() (Character Output):


○​ Header File: <stdio.h>
○​ Purpose: Writes a single character to the standard output.
○​ Syntax:​
int putchar(int character);​

○​ 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​

3.​ puts() (String Output):


○​ Header File: <stdio.h>
○​ Purpose: Writes a string to the standard output, followed by a newline character.
○​ Syntax:​
int puts(const char *string);​

○​ Argument: A pointer to a null-terminated string.


○​ Return Value: Returns a non-negative value on success, or EOF on error.
○​ Example:​
#include <stdio.h>​

int main() {​
puts("Hello, World!");​
puts("This is another line.");​
return 0;​
}​
Output:​
Hello, World!​
This is another line.​

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;​
}​

2.​ getchar() (Character Input):


○​ Header File: <stdio.h>
○​ Purpose: Reads a single character from the standard input.
○​ Syntax:​
int getchar(void);​

○​ 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;​
}​

3.​ gets() (String Input - Generally Avoid):


○​ Header File: <stdio.h>
○​ Purpose: Reads a line from the standard input into a character array until a newline
character is encountered (which is then replaced by a null terminator).
○​ Syntax:​
char *gets(char *string);​

○​ 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;​
}​

Working with Files:


C also provides functions for reading from and writing to files, which involve concepts of file
pointers and streams. Some key functions include fopen(), fclose(), fprintf(), fscanf(), fgetc(),
fputc(), fgets(), and fputs(). These are also part of the <stdio.h> library.
Understanding these input and output functions is crucial for creating interactive programs and
programs that can handle data from various sources and display results effectively here in India
or anywhere else. Always be mindful of potential security risks, such as buffer overflows when
taking string input, and use safer alternatives when available.

You might also like