The purpose of this tutorial is to teach users how to print a line of words to the output environment and format them.
The code is as follows:
#include <stdio.h>
int main ( )
{
printf("Hello World!\n");
return 0;}
{
printf("Hello World!\n");
return 0;}
First, we include the standard header, #include <stdio.h>. If you're new to programming, it'll take a while to get the hang of remembering the names of the headers to include, so you could remember it is "standard in-out". This header file basically stores a number of functions, such as the printf function as you would see later. This header is required to work most C code. Other headers are math.h, time.h, stdlib.h, string.h and so on, which you would discover later.
We have a main function which returns and integer 0. This is denoted by the int main ( ) and return 0; portions. The main function does not take in any arguments, so what's between the brackets are left void.
Now we use curly braces { } to enclose our codes. Anything within a brace is a code block, meaning they will be executed together in a round. In this case, those within the { } braces will be executed during one round of the run of the main function.
Now let me side step a little to explain a function. A function is something that takes an input and returns something as an output. In our main function here, it does not take any input, so it is left void and returns 0 as an output at the end of its execution. Sometimes, it may be written as:
int main (void)
{
//Code here
return 0;
}
{
//Code here
return 0;
}
Main is the name of the function. The return type is integer, denotated by 'int'. You could think of a function as a blender. You pass in oranges and apples in the brackets (oranges, apples) to a Blender. What you get out is a orange-apple-juice. This is what a function does. A blender function might go like this:
juice blender (fruit orange, fruit apple)
{
blend fruits;
return orange-apple-juice;
}
{
blend fruits;
return orange-apple-juice;
}
Okay, silly example. Let's move on. printf is the function call used to print a line of output. The text is formatted within " ". The \n means a new line. One very important thing is the semicolon ; behind each line. That basically tells the compiler that it's the end of a command. It works just like a full-stop in english writing. Sometimes you might get compiler errors, then check if there's a semicolon behind each statement as you want it to be; else you'll get errors!
There you go! Type these in and you get a nice little output!
More on printf function can be found here:
This is a good reference for any type of codes you want. Some summaries you might want to take note of:
- "\n" makes a new line
- "\t" makes a new tab (4 spaces)
- " " makes a space
- More on formatting numbers in output later :)
Here, I've tried to explain how to print an output to a console. I hope it is useful. Any questions, please leave a comment or email me. Thank you!
No comments:
Post a Comment