Skip to content

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:

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:

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:

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

→ Get my C Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about clang: