When coding in C, you need to call the right standard library functions for basic I/O. You must always include the <stdio.h> header file to ensure that these functions are loaded into your program.
Output
The standard output stream in C is the PC screen. That is, when you run a C program that has information to be output, it will be displayed on the screen. A stream is a series of characters flowing from one place to another.
It’s also possible to use another output stream such as a file. However, this is an advanced topic for another day.
The C language uses the printf() function to print a string of characters to the screen. This string of characters (sometimes called a literal) is placed between double quotes inside the printf() function.
From line 1, #include <stdio.h> is a preprocessor directive. It tells the preprocessor to include the contents of the I/O header (<stdio.h>) before the program is compiled.
Notice that the program output doesn’t include \n. This is because it’s an escape sequence. An escape sequence is a combination of characters that has a special meaning, other than simply the characters contained in them.
The backslash () is a character that tells the compiler that it’s going to perform a special output. For example, \n means that a new line is going to be printed. The next program output (if any) will begin from that new line.
The table below summarizes some of the common escape sequences.
In the essence of space, you may sometimes need to break up long laterals in your text editor. You can comfortably do this using multiple printf() functions to print your message.
See the example below:
Input
The standard input stream in C is the keyboard. This means that when your program prompts an input, it expects that data to come from the keyboard by default.
It’s worth knowing that the input stream can be directed to something else, like a file.
C language uses the scanf() function to get user input. See the example below:
The scanf() function takes in two arguments: a conversion specifier and a memory address. From the example above, %d is the conversion specifier. It tells scanf() to input an integer. The d in %d stands for “decimal integer.”
The second argument begins with an ampersand (&), which is called an “address operator” in C. The argument &integer1 tells the compiler which memory address the value got from the user should be stored.
After the scanf() statement has been executed in a program, the compiler waits for you to input a value. You submit a value by typing it and then pressing the Enter key (or Return key). When this value is assigned to your variable, any other reference to it in the program will use the same value.
Learning C With a Beginner Program
Beginning your programming journey is a very exciting endeavor. If done incorrectly, it can instead turn out to be a daunting challenge.
Learning things without applying them to practical situations is usually the problem. Think outside the box; try putting yourself in some interesting scenarios where you can apply your knowledge. Practicing with some beginner programs is one of the best ways to retain your newly gained knowledge.