C - programming basics - hello world tutorial[C][Coding]

Started by: Automaton | Replies: 4 | Views: 2,093

Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Aug 26, 2008 1:45 AM #234202
C HELLO WORLD TUTORIAL

So, I take it you're here to learn your first C program right? If not, then I'm afraid you've got lost somewhere. Now, this tutorial is intended for people with no experience in C, or any other programming language what so ever.
[SIZE=3]
Now, what exactly is a programming language?

you might be asking. Well, it is a language, or code, designed to be interpreted into lower level machine code called binary (formed with lots of little 1's and 0's, representing "on" and "off", but this is not important). It is a language that can be run, and do almost anything you can with a computer. For instance, you know when you open Microsoft word? Or OpenOffice? How do you think it got there? It didn't get there by magic, that's for certain. It was programmed, using a programming language. That's the type of thing you can achieve by programming, and this tutorial will be your first step into it.


Why choose C?
[SIZE=2]C is a very quick and easy language. It is probably the most fast and robust high-end programming language, and the most easy to learn IMO, that there is. That's all there is to it. There are many people who would disagree, and google all you want to find more reasons.

[SIZE=3]Your First Program
[SIZE=2]

#include

int main()
{
printf("hello world\n");
return 0;
}

There you go, that's your first program, your first look at some C code. I would explain at this point, but first I think you should have a go at running it. To compile and run this program, go to http://sourceforge.net/project/downloading.php?groupname=dev-cpp&filename=devcpp-4.9.9.2_nomingw_setup.exe&use_mirror=switch
This is the latest stable version. Google for other downloads. What you have just downloaded is an IDE (integrated development environment). It has the editor (to write your code), the debugger (to get rid of errors in your code) and the compiler (used to turn the code in to an .exe, or program). Now, copy that code, open Dev C++, click the blank page button, and paste the code in to the blank canvas. Then go to Execute>Compile. Save it to wherever.c (the .c is important), and once it's compiled, you can run the exe through the command prompt (so it doesn't flash off). For instance, if I compiled it on my desktop, and it was called "hello.exe", I would go to start>run>"cmd">type "cd Desktop">type hello. And et voila, it prints "hello world" to the screen. That is how to run your first program :D

[SIZE=3]What does it mean?
[/SIZE][/SIZE][/SIZE][/SIZE]
[/SIZE]
Ah the code... I'll go through it a line at a time:
#include 

The #include speaks for itself, it says to "include" whatever comes next. You need this in every program. is a header file. Header files are documents that store information about the language you are using. The header file stdio.h includes information needed to print things and take input. Thus, it stands for "standard input and output.header". You can also make your own header files, but that's a more advanced programming technique. You need to include this header file if you want to print that line.


int main()
{
/*this is where all your code goes*/
}


main is a function. It is the function that all your code goes inside from a beginners point of view. It returns an integer value, thus the "int main()". Generally speaking, you will only really ever need it to return an integer (number). The parentheses "()" are left empty. They are of no concern to you at the moment. All you need to know is that functions have to have parentheses, and these parentheses can have values passed to them. But with main, the only time you will need to pass anything to them is when you need to find out how many arguments were passed to the program, and what they were. This is not important though. Just leave them empty, or "void" as the jargon has it named. Now, as I have clearly stated in the code, all the code goes inside the 2 "braces" ({}). In the code above, I have added a comment (in bold). Comments start with /* and end with */. Anything in between those is not read by the compiler, and is ignored. These can be used to remind yourself, or other readers, just what is going on.


printf("hello world\n");
return 0;

This is the easy bit. printf is a function included in stdio.h. Anything inside the parentheses, and inside "" is printed to the screen. The "\n" is a terminator. It basically means "go on to a new line". There are other terminators as well:
\t ----- new tab
\r ----- return to the beginning of the line
\\----- inserts a backslash
\"----- inserts a quote
\'----- inserts a single quote
All statements are ended with a semicolon (;). This is important you add this. If you miss any small code out, the compiler will point it out when you try to compile the program.

return 0; is the return statement for the main function. Remember the int main? It has to return a number, and because there is nothing in particular we want it to return, we return 0, telling the program everything is fine.

Thats it for this tutorial. If there's anything you want me to add or need help with, feel free to ask :P

Thanks for reading, and I hope to have inspired some to continue into the great world of C programming :D

Bonk
2

Posts: 2,778
Joined: Mar 2008
Rep: 10

View Profile
Aug 26, 2008 8:32 AM #234685
C isn't that quick or easy compared to some others dude.

But yeah, if you could get some harder to find code up that'd be great.
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Aug 26, 2008 12:23 PM #234765
:S TBH I'm not very good at explaining. I think I'll just add one more, about functions and pointers. Also including argc and argv, because if I make another tutorial on variables and the basics, I think it would take forever. And yeah, there are quicker, but C is probably the quickest if you want the job done efficiently and properly.
Bonk
2

Posts: 2,778
Joined: Mar 2008
Rep: 10

View Profile
Aug 26, 2008 1:03 PM #234780
Yeah, I'm not saying it's bad. I actually want to learn more, so yeah I'd look at any tutorials you make.
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Aug 26, 2008 1:17 PM #234789
Thanks :D I'll have a go at making another one some time today