Basic I/O concepts in C
How to perform input/output using printf and scanf in C
C is a small language, and the “core” of C does not include any Input/Output (I/O) functionality.
This is not something unique to C, of course. It’s common for the language core to be agnostic of I/O.
In the case of C, Input/Output is provided to us by the C Standard Library via a set of functions defined in the stdio.h
header file.
You can import this library using:
#include <stdio.h>
on top of your C file.
This library provides us, among many other functions:
printf()
scanf()
sscanf()
fgets()
fprintf()
Before describing what those functions do, I want to take a minute to talk about I/O streams.
We have 3 kinds of I/O streams in C:
stdin
(standard input)stdout
(standard output)stderr
(standard error)
With I/O functions we always work with streams. A stream is a high level interface that can represent a device or a file. From the C standpoint, we don’t have any difference in reading from a file or reading from the command line: it’s an I/O stream in any case.
That’s one thing to keep in mind.
Some functions are designed to work with a specific stream, like printf()
, which we use to print characters to stdout
. Using its more general counterpart fprintf()
, we can specify the stream to write to.
Since I started talking about printf()
, let’s introduce it now.
printf()
printf()
is one of the first functions you’ll use when learning C programming.
In its simplest usage form, you pass it a string literal:
printf("hey!");
and the program will print the content of the string to the screen.
You can print the value of a variable, and it’s a bit tricky because you need to add a special character, a placeholder, which changes depending on the type of the variable. For example we use %d
for a signed decimal integer digit:
int age = 37;
printf("My age is %d", age);
We can print more than one variable by using commas:
int age_yesterday = 36;
int age_today = 37;
printf("Yesterday my age was %d and today is %d", age_yesterday, age_today);
There are other format specifiers like %d
:
%c
for a char%s
for a string%f
for floating point numbers%p
for pointers
and many more.
We can use escape characters in printf()
, like \n
which we can use to make the output create a new line.
scanf()
printf()
is used as an output function. I want to introduce an input function now, so we can say we can do all the I/O thing: scanf()
.
This function is used to get a value from the user running the program, from the command line.
We must first define a variable that will hold the value we get from the input:
int age;
Then we call scanf()
with 2 arguments: the format (type) of the variable, and the address of the variable:
scanf("%d", &age);
If we want to get a string as input, remember that a string name is a pointer to the first character, so you don’t need the &
character before it:
char name[20];
scanf("%s", name);
Here’s a little program that uses both printf()
and scanf()
:
#include <stdio.h>
int main(void) {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("you entered %s", name);
}
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025