"Hello World Tutorial
ok guys this is my first tutorial on C and hope it helps. Im not too good with C but this might help someone who wants to start learning C. What is C? C is a computer programming language, it is a successor of the language B (in case some people may be wondering, no there was no A :P).
Writing a C code in a text editor and then compiling that snippet of code should ouptut the commands you have written in C. This simple tutorial will teach you how to output "Hello World" on your screen. Compilers. To compile a C code you will need a compiler, here's a good on you can download for free: http://www.bloodshed.net/devcpp.html There are more compilers if you do not wish to use that one, you could use google in that case for C compilers.
Getting ready.
After you have downloaded and installed DevC++ open that compiler. in the new window go to : file > new > project OR click on the new project image on one of the toolbars. A small new window should pop up with: Basic|Introduction|Multimedia| Windows Application :: Console Application and the rest. well at the bottom you will see: C project C++ project. we will be making a code in C so you choose for C project. And application type, you choose Windows Application. In a small input box at the bottom of the window you insert the name of your project. We will be naming it: "Hello" but this is optional, you can name it whatever you want to. After clicking "OK" you will have to save the project somewhere.
My advise would be to make a folder with all your projects in it. After saving it you will get a window with a large code in it. Remove the code, all the code in that textarea can be removed. so now we have a new white page where we will be writing the code.
The Code.
first thing we do in C is include a header file. A header file contain all the info for using a build in functions you will need to make a functional code. The header file we will need to write or string is stdio.h This is the line on how to include this header file in your code
#include <stdio.h>
Now that we have our header file included we will move on to the printing of "Hello World". in all C codes you must define the function main(), this function is the first function that will be excecuted in the code. to declare function main we use this
main(){
Now we have our function main. the opening bracket "{" is used to open a function, in between the two brackets "{" and "}", you put the code you will run. Now for the exact command to print "Hello World" into the screen, we will use the function printf().
printf("Hello World");
This is really simple if yo look at it, first we call the printf function, and whatever we want to print onto the screen with printf we put inside double quotations. So if you wanted to print something else you just put something else in the double quotations:
printf("Hello Dude");
A small note, after every command in a C code, you must add a semi-colon. so now that we have added the printf function we will close the main function using the clsoing bracket "}" so now your code look like this:
#include <stdio.h> main(){ printf("Hello World"); }
To run your program click the compile & run button (its a button in the top left of your window with 4 colors). or you can just click F9. you may see a black screen for 0.1 seconds or something and then it closes automatically. this is because after printing "Hello World" the program closes itself. to stop it from doing this you add
system("PAUSE");
in the main function. So now your code looks like:
#include <stdio.h> main(){ printf("Hello World"); system("PAUSE"); }
"Hello World" may be on the same line as "Press Any Key To Continue". if you want this on a seperate line you ad a break line charachter "\n" in the printf function. like this:
printf("Hello World\n");
And that's it for your first C program (and my first C tutorial :P). hope this was helpfull, if not you can PM me or email me. have fun with C!!
Comments
Post new comment