C tute 1: hello world
July 27th 2006 03:31
Today I am going to do a brief introduction to the C programming language. To complete this lesson you will need a compiler. I will be using bloodshed dev (http://www.download.com/Bloodshed-Dev-C-/3000-2069_4-10019857.html) and the tutorials will assume you are too. This is going to teach you how to write a simple hello world program.
Getting started
Open your compiler and from the file menu select new->source code. Once you have done so type the following code.
#include <stdio.h>
int main()
{
printf("Hello world\n");
getchar();
return 0;
}
Then go to file->save and save it as a c source file names "Hello world". Now, go to execute->compile, and then, once this is complete, execute->run.
This should open up a window which display the words "hello world", and then waits until you press enter. When you do so it should close.
Looking at the code
So what do all the individual pieces mean?
#include <stdio.h>
This tells the compiler that it needs to include all the code that is contained in the header file stdio as well as all the code that you write. This is what gives you access to many of the functions that you use.
int main()
This tells the compiler that the function main, will return an integer.
{
This signals the beginning of a function.
printf("Hello world\n");
This function outputs "hello world" to the screen. "\n" inserts a line break at the end of this and the ";" marks the end of the function.
getchar();
This function tells the program to wait for input from the keyboard. It ensures that the program doesn't open, display "hello world" and close, all before you have a chance to see any of it.
return 0;
This tells the compiler what value to return to the computer. We will return to this later.
}
This marks the end of the function
Well that's it for today. With the next post I will deal with inputting text.
Adam
Getting started
Open your compiler and from the file menu select new->source code. Once you have done so type the following code.
#include <stdio.h>
int main()
{
printf("Hello world\n");
getchar();
return 0;
}
Then go to file->save and save it as a c source file names "Hello world". Now, go to execute->compile, and then, once this is complete, execute->run.
This should open up a window which display the words "hello world", and then waits until you press enter. When you do so it should close.
Looking at the code
So what do all the individual pieces mean?
#include <stdio.h>
This tells the compiler that it needs to include all the code that is contained in the header file stdio as well as all the code that you write. This is what gives you access to many of the functions that you use.
int main()
This tells the compiler that the function main, will return an integer.
{
This signals the beginning of a function.
printf("Hello world\n");
This function outputs "hello world" to the screen. "\n" inserts a line break at the end of this and the ";" marks the end of the function.
getchar();
This function tells the program to wait for input from the keyboard. It ensures that the program doesn't open, display "hello world" and close, all before you have a chance to see any of it.
return 0;
This tells the compiler what value to return to the computer. We will return to this later.
}
This marks the end of the function
Well that's it for today. With the next post I will deal with inputting text.
Adam
| 121 |
| Vote |
subscribe to this blog


















