Learning C, Part 2: More about the stdio.h header file ON POSIX SYSTEMS
Hi again!
It has been a couple days since the last tutorial, and I figured I would make another tutorial looking further into the standard I/O library C has to offer!
This tutorial will supply you with the common knowledge you need when using the stdio.h libary.
In this tutorial you will learn how to:
- Use the printf function
- Use the fprintf function
You will also learn:
- What stdout is, and when it needs to be used
- What stdin is, and when it needs to be used
- What stderr is, and when it needs to be used
So, Without further ado, lets get started!
What other ideal functions does the standard I/O library offer us?
So, before we start, we should probably cover something first.
The stdio.h header file that C offers to us does not only enable us to print and get input, but it also enables us to open files!
I will explain this in the next tutorial when I cover types in C, and I will give you a brief explanation in that tutorial as to why I am including file reading as a part of learning types!
Ok, lets get started now.
The first function, and most commonly used when including the stdio.h header file in C, is printf
.
printf
is a function that the stdio.h header file holds. This function is used to print things to the terminal.
I am sure you already know how it works, but just for an example for you visual learners, here is how you would use a simple printf
statement:
#include <stdio.h> // required if you're wanting to print or get input // Remember, every C project needs a main function! int main() { printf("Hello World"); // "Hello World" will now show on our terminal! return 0; // remember, return 0 in the main function means // the project compiled/executed successfully }
That is a basic example of using the printf
function.
What if we want to print a number? What should we do?
printf(32)
?
No!
Printing different types with the printf
function
We should all know what integers and floats(being decimal).
With this knowledge, we can implement it when printing those types!
In a printf
function, within the double quotes(or single if you are printing just ONE thing), you are supposed to declare what type is going to be printed.
I will only go over 4 type:
- String
- Integer
- Float
- Character
Note: The printf
function is printing a string by default. But, lets say you want to print a variable that is holding a string, you would then declare within the printf
function what type is being printed!
So, how do you print certain types in C?
- String - '%s'
- Integer - '%d'
- Float - '%f'
- Character - '%c'
P.S: Floats in C is just a decimal number
Here is an example:
#include <stdio.h> int main() { printf("%s","HEY"); // printing a string "HEY" printf("%d",44); // printing an integer 44 printf("%f", 44.44); // printing a decimal printf("%c",'h'); // printing a character return 0; }
Before we move on, remember that when you use the printf
function, the outcome is by default a string. So, if you want to print a string but it isn't stored inside a varibale, no need to do
printf("%s","Hello World)
just do
printf("Hello World")
Ok, onto the next thing!
The fprintf
function!
Aww yes! This function is basically just like the printf function, only it has a specified stream(so stdin, stdout and stderr).
Here is what each stands for:
- stdin - getting input. Use this if you want input
- stdout - Sending output. Use this if you want to display text
- stderr - There is an error and that is what you're printing
stdin won't be used when using the fprintf
function by the way!
Also, just because you use stderr does not mean the execution will stop. It simply will not
So, if we want to print to the terminal, guess what the code would be?
#include <stdio.h> int main() { fprintf(stdout,"Hey, I am %d years old", 16); return 0; }
Remember: '%d' means we are printing off an integer!
Quick Note: stdin, stdout and stderr acts as a 'file'. We will cover this most likely in the next tutorial!
Now, this isn't everything that is needed to cover up the standard I/O library C offers. Within the next tutorial we will cover many many things from type declarations, to reading and writing to files, to getting user input!
I hope this tutorial gives you all some type of insight on printing things/displaying text to the terminal in C.
If not, please PLEASE comment below and give me some suggestions to make the tutorial(s) better and I will re-do it!
What's the actual difference between stdin
and stderr
?
ummm, the spelling, lol.
@DynamicSquidEither that or I can just edit each tutorial and add some information that co-relates to window-users...
And, if you could, can you give me some type of source/information of the differences with using Windows?
This is great! Thank you so much for all this mate.
Sure! give me a few days to process and practice this and then i'll come to you.
@MocaCDeveloperGreat!
@MocaCDeveloperHey I am so sorry for not posting a new tutorial. I have been really busy and working overtime at my job this past week. I should be able to post another tutorial either later on tonight or tomorrow! Again, I apologize very deeply!!
@JosephSanthoshOh noo, please don't be sorry! I'm so grateful for what u have done, please take ur time and do it at ur own pace. Have a great day and I hope i'm not disturbing u.
@MocaCDeveloper