So, you've decided to learn C? Well, you made a good choice. This first
tutorial is just how to compile a program, and kind of just learn how C
works.
Depending on the compiler you want to use, the instructions vary.
But, basically you will do the following steps.
For Command-Line compilers on Windows
- Open up Notepad or an equivalent
text
editor, NOT a word processor or something with fonts.
-
Type in the following:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
-
This is the traditional Hello, World program. Save it as something like "hello.c" (
with
the quotes in Notepad).
- Open a command prompt (or a DOS command line)
- Change to the directory that you saved hello.c in (using
cd
)
- Execute
gcc -s hello.c -o hello.exe
(For other compilers, look at your manual)
- If you get an error, check your typing and try again.
- Otherwise, type
hello
and press enter.
- You should get a Hello, World! message. Try going back and changing the message.
When you're done experimenting, let's look at what this means:
-
#include <stdio.h> = Standard
Input/Output Header, kind of like a screen "enabler".
This is required for the screen and the keyboard.
-
int main(void) { = The place where
the computer starts executing, therefore all statments after this
line are executed.
-
printf("Hello, World!\n"); = Prints
'Hello, World!' to the screen, as you may have guessed from
experimenting.
-
return 0; = Return function exit
code 0. Kind of advanced, skip for now, until we get to
functions.
-
} = Braces mark code segments.
Notice that the code segment starts at "int main".
Double quotes (") mark strings. So, "Hello" is a string, whereas
'Hello' is not. (Look-ahead fact: ' marks a char constant).
"\n" prints a new line to the "console" (screen).
Got that? Good. Now, try to make a program that . . .
- Prints "Hello from C!"
- Then 4 enters
Your solution should look something like this:
#include <stdio.h>
int main(void) {
printf("Hello from C!\n\n\n\n");
return 0;
}
It's okay like this, as well:
#include <stdio.h>
int main(void) {
printf("Hello from C!");
printf("\n\n\n\n");
return 0;
}
They do the same thing.
C ignores whitespace (Tabs, spaces, enters). So,
#include <stdio.h >
int main( void ) {
printf( "Hello, World!\n" ) ;
return 0 ;
}
Will do the same as the first Hello, World! program.