AIM: Write a C program to demonstrate various input and output functions like printf,
scanf, gets, getchar, and putchar.
THEORY
C is a general-purpose, procedural programming language developed by Dennis Ritchie
at Bell Labs. It's known for its efficiency, portability, and low-level memory manipulation
capabilities. The language provides a set of standard library functions for interacting
with the user, which are known as input/output (I/O) functions. These functions are
defined in the <stdio.h> header file, which stands for Standard Input-Output.
• printf: This is a formatted output function used to display text, numbers, and
other values on the console. It can be used with format specifiers (e.g., %d for
integers, %f for floats, %c for characters) to control how data is presented.
• scanf: This is a formatted input function used to read data from the keyboard.
Like printf, it uses format specifiers to read and store different types of data into
variables.
• gets: This function reads a line of text from the standard input (keyboard) and
stores it as a string in a character array. Note: The gets function is considered
unsafe and deprecated because it doesn't perform bounds checking, which can
lead to buffer overflow vulnerabilities. It's better to use fgets instead.
• getchar: This function reads a single character from the standard input. It
returns the character read as an integer, or EOF (End of File) if an error occurs.
• putchar: This function prints a single character to the standard output.
PROCEDURE
1. Open a text editor or an Integrated Development Environment (IDE) like VS Code.
2. Create a new file and save it with a .c extension (e.g., Practical.c).
3. Write the provided C code in the file.
4. Compile and run the program.
5. Observe the output and interact with the program by providing the requested
inputs.
PROGRAM
#include <stdio.h>
int main() {
char name[50];
char ch;
// Demonstrate printf
printf("Demonstration of printf:\n");
printf("Hello! Welcome to the C programming demo.\n");
// Demonstrate scanf
printf("\nDemonstration of scanf:\n");
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You entered: %d\n", age);
// Clear input buffer before using gets
while ((getchar()) != '\n'); // Consume newline left by scanf
// Demonstrate gets
printf("\nDemonstration of gets:\n");
printf("Enter your name: ");
gets(name); // Warning: gets is unsafe and deprecated, better to use fgets
printf("You entered: ");
puts(name);
// Demonstrate getchar and putchar
printf("\nDemonstration of getchar and putchar:\n");
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
printf("\n");
return 0;
OUTPUT
Demonstration of printf:
Hello! Welcome to the C programming demo.
Demonstration of scanf:
Enter your age: 25
You entered: 25
Demonstration of gets:
Enter your name: Jane Doe
You entered: Jane Doe
Demonstration of getchar and putchar:
Enter a character: A
You entered: A
CONCLUSION
The program successfully demonstrates the use of common C I/O functions. It
showcases how to use printf for displaying formatted output, scanf for reading
formatted input, and the character-oriented functions getchar and putchar for single-
character I/O. It also illustrates the use of gets for reading strings, while highlighting its
security risks.