This tutorial is for people new to C or programming.
In this tutorial we will start from the very basics and create a
simple program (executable) that will display a message to our
screen.
1. Download a development enviroment that will compile our code from
simple text to and executable. A free one is Dev-C++.
2. Install it, and in our case (Dev-C++) press CTRL+N to create
a new source file (its nothing but a white page to write in)
Type the following (Dont just copy-paste the code, you need to
type in order to memorize faster and better):
#include <stdio.h>
#include <stdlib.h>
main()
{
printf("Hello World\n");
system("pause");
}
Compile and run it (In Dev-C++ press F9)
1.Lets talk about C in general(you have to read this first and after scroll down to
see what the code does):
Info: A C program contains functions. The functions specify the
tasks to be performed by the program. The "main" function
establishes the overall logic of the code. It is normally
kept short and calls *different functions to perform the
necessary sub-tasks. All C codes must have a "main" function.
2. A function's structure:
type name(parameters){code}
Example:
int main(a,b){printf("Hello World");}
But for now (in simple programs like this one) forget about
type and parametres, and lets keep it simple.
3. *different functions: in our code the commands printf and
system are nothing else but "precooked" functions. They are just
code that has already been written to help us do basic stuff
like in our code, so we dont have to reinvent the wheel everytime
we want to print a message.
Someone, someday was sitting at his computer (lets call him Dennis)
creating c and said to himself.. "hmm all this "precooked" code must be available
to everyone using c" and he decided to create a "package or officially a library" called
stdio.h which means standard input output and it contains
code that helps you to to print messages(output) and to scan
for keyboard buttons being pressed(input). So when you see things like
printf("x"),system("x") dont get confused.. He just wanted to name
his functions like that and give them this stracture. We just need
to memorize it. Dennis also created many other libraries!
Now we are really ready to understand the commands be explaing them
one by one..
A) #include <stdio.h>
#include <> is standard. It informs the computer that
we are going to use some code from the libray. If we type
stdio.h in it, like that #include <stdio.h>, we inform the computer
that we are going to use the library stdio.h which is nothing
else but the "precooked" code of Dennis(remember?).
The reason we use it is because we used the command
printf("Hello World")
B) #include <stdlib.h>
stdlib.h contains functions involving memory allocation,
process control, conversions and others. The reason we use it is
because we used the command system("pause")
After we are done with the libraries we are going to use we
create our main function. And we start writing the heart of our program.
C)printf("Hello World\n");
printf("") is standard. In "Hello world" goes your text.
e.g printf("forza motorsport"). The \n right after our text
is not printed to the screen and it works just like when
you are typing and you hit enter to move one line down.
So it moves the cursor by one line. Try removing it and compile
your program and you well see the results. You can put as many
as you like e.g printf("\nalcohol\n\n\n\n\n\). The ; MUST be used
everytime right after a command to inform the computer that
this command ends exactly where the ; is. The only times you dont
need to use ; is when you include your libraries and when your
function ends because in both cases the ending of each one is obvious,
in our code it might seem like it easy but as we progress you will see
that unfortunatly is not.
D)system("pause");
Although we used this command dont pay too much attention. What
it does is stop the execution of the code and print a message
to the screen "press any key to continue.." and only if you
press a key the code progresses. We used it because in many computers
when the commands are executed the program closes (it's normal, that's
what computers do, they just execute commands and they doesn't "care"
if you will see what they did). System("pause") pauses our
system so we can see the message we typed in printf, "Hello World".
Otherwise it would be executed so fast that you would see just a
quick flash and a programm closing. System("pause") helps us in that case.
As i said dont pay to much attention to it yet.
This is the end of the tutorial. Yes, just this little piece of code
nothing else. I focused and getting you to undestand what is happening
and not just tell you about it. If you liked the tutorial and i make
others then you will see that although there will be more
and more code in them and fewer talking you will feel much
more confident when looking at it. This mostly theoretical
tutorial is necessary.









1Likes
LinkBack URL
About LinkBacks


Reply With Quote



.




