I was sat here, in my room – fairly bored to be honest… :) – and i started thinking, wouldn’t it be cool to write a C guide to help people! :D
This is my first time ever to attempt something like this, so it could turn out reeally bad, or really well – who knows!
Introduction
C as you should hopefully know is a programming language, and until C++ etc came out, it was on of the most powerful languages out there because of one feature – pointers.
Unlike Java and C++, C is a procedural language and not an Object-Oriented one.
Hello World
So then, enough babble, lets have a look at a working program! And as usual i thought i go with tradition and show you the oh-so-famous “Hello World” program! :)
If you’ve used C++ before, this will look very familiar, except for the fact that ‘#include <stdio.h>’ is in fact ‘#include <iostream>’ in C++.
So, lets take a look at what’s happening. Well, to be fair it’s actually very simple. ‘#include <stdio.h>’ is a command that will include the standard I/O library of functions for C, otherwise we couldn’t print things to the screen or get the users input!
int main() is the name that must be used for the main function of a C program. It is the function that will be automatically used when your program is compiled and run.
The { and } tags are important. They signify the opening and closing of a function. So { starts the opening of the main function, and } signifies the end of the main function.
printf() is one of the functions that is from the <stdio.h> library we included, and is used to print to the screen what ever is inside the two brackets – in this case “Hello World!”
return 0 is very important. For a C programs, nearly everything returns a value. If this value is 0, then the code was successful, otherwise the code has errors – but more on this later.
You may have noticed the ‘;’ at the ends of the 2 lines of code in the main function. These semi-colons are vital, as they signify the end of a function call, or an assignment. with out it, you would get a lot of errors :(
And finally we have the close { tag, that signifies the end of the main function, and the program altogether.
Next we’ll take a look at Comments for your code – these are a vital part of coding, especially for when you have hundreds of lines of code O_O
No comments:
Post a Comment