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:
%cfor a char%sfor a string%ffor floating point numbers%pfor 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);
}
Lessons in this unit:
| 0: | Introduction |
| 1: | ▶︎ Input and output |
| 2: | Variable scope |
| 3: | Static variables |
| 4: | Global variables |
| 5: | Type definitions |
| 6: | Enumerations |
| 7: | Structures |
| 8: | Command line parameters |
| 9: | Header files |
| 10: | The preprocessor |
| 11: | NULL values |
| 12: | Boolean values |
| 13: | Nesting functions |
| 14: | Conversion specifiers |
| 15: | Using quotes |
| 16: | String length |
| 17: | Returning strings |
| 18: | Array length |
| 19: | Looping through arrays |
| 20: | Checking character values |
| 21: | Printing percentage signs |
| 22: | Troubleshooting: Implicit function declarations |