Basic Input and Output in C
By Swann
![Picture of the author](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fauthor-profile-picture.8832aca3.jpg&w=1120&q=90)
- Published on
![Basic Input and Output in C](/_next/image?url=%2Fphotos%2Fblog-prototype.jpg&w=1120&q=90)
Sharing
Introduction
Effective input and output (I/O) handling is crucial in C programming to interact with users, gather data, and display results. The standard I/O functions - printf
for output and scanf
for input - are vital tools that every C programmer utilizes to facilitate interaction between the program and the outside world.
Output: The printf
Function
Syntax
printf("Format String", Argument List);
Example
#include<stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Format Specifiers
%d
or%i
: Integer%f
: Float%c
: Character%s
: String
Input: The scanf
Function
Syntax
scanf("Format String", Address List);
Example
#include<stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}
Handling Multiple Inputs
int age, year;
printf("Enter your age and birth year: ");
scanf("%d %d", &age, &year);
Handling Strings: gets
and puts
gets
: To read a string from the user.puts
: To display a string.
char name[30];
gets(name);
puts(name);
Conclusion
Understanding basic I/O functions in C, such as printf
and scanf
, is pivotal in developing interactive applications. As you explore further, you'll encounter more advanced I/O functions and techniques, but these foundational functions will remain a staple in your C programming endeavors, aiding in user interaction, data collection, and information display.