Thursday, April 30, 2009

[C/C++] New to C Programming Language Part 2(WIP)

LKS is not good at English. Without a reference book in English, it's hard to write and explain the guidance in English. If there is any wrong grammar or misspelling vocabulary, please don't blame LKS too harshly.

  • Compiling

First program “Hello C”
This is a typical code in learning C programming language

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("Hello world!\n");

system("pause");
return 0;
}


Save the code as hello.c

  • Brief explanation

#include
Includes a file called stdio.h which lets us use certain commands.

int main()
“int” is called the return value.
“main” is the name of the point where the program starts.

{}
The two curly brackets are used to group all the commands together so it is known that the commands belong to main.

printf("Hello World\n");
This is the printf command and it prints text on the screen.

return 0;
The “”int” is short for integer. We need to use the return command to return the value 0 to the system to tell it that there were no errors while the program was running.

LKS will write another tutorial about the detail of C …

Now is the time for Compiling.
Open up the command line (cmd.exe) and set the current directory to where your code file is.

Type the command
gcc hello.c -o hello.exe

-o <file_name>
Place the output into <file_name>.
Default the output file would be a.exe.

LKS notices that you should add a new line at end of code in every code or you will get a message like this

hello.c:X:X: warning: no new line at end of file

Working In Process...

0 意見:

Post a Comment