Sunday, February 24, 2008

C Programming Part 1

Basic computer programming things to know:
C is a low-level computer programming language that is mainly used for system programming.

compiler - a program that converts source code (the code a person creates) into object code (the code a computer understands).

linker - a program that combines object code with other object codes or libraries to create an executable program.

Go here and download MS Visual C++ 2008 Express Edition to obtain a C compiler for Windows. There are many other compilers out there that will also work.

Here is a compiler for the Linux OS.

Use notepad, vi or any other simple text editor (if you use MS Word, make sure to save as a text file) to create your source code.

You will be using the Windows command line (from Visual Studio 2008 Command Prompt) or the Linux terminal window to compile and run your codes.

Here is a list of commands for the MS Visual 2008 C++ command line:(the outside quotation marks are not part of the command)

MS Visual 2008 C++ Command Line
CommandsDescription
cd dir_namechanges directory to dir_name (cd \ brings you to root folder)
mkdir dir_namecreates a directory with the name dir_name
dirlists everything in directory
clsclears screen
notepad file_name.copens file_name.c in notepad (if file doesn't exist, it will create one)
type file_name.cshows you the content of file_name.c
cl file_name.ccompiles and links file_name.c
file_nameruns the file file_name.exe
move file_name dir_name
moves the file, file_name, into the directory dir_name
del file_name
deletes the file file_name
(Hold Ctrl and C)breaks out of the program (useful when stuck in an infinite loop)


Here is a list of commands for the Linux terminal window:
Linux Terminal Window Commands
CommandsDescription
cd dir_namechanges directory to dir_name (cd \ brings you to root folder)
mkdir dir_namecreates a directory with the name dir_name
dirlists every file in directory
vi file_name.copens file_name.c in vi editor (if file doesn't exist, it will create one)
:w (vi editor command)saves the file
:q (vi editor command)quits the vi editor
cat file_name.cshows you the content of file_name.c
cc -o name file_name.ccompiles file_name.c and creates an output file called name
./nameruns the file called name


Basic C Program, Hello World:

#include <"stdio.h"> // Quotation marks are not supposed to be in this line of code.
main()
{
printf("hello world\n");
}

The simple code above will show the text hello world.

  • The #include <"stdio.h"> (no quotation in real code - blogger problem) is used to add the standard input/output library (a library is just preprogrammed keywords/commands) to the code.
  • The // indicates a comment. Anything to the right of the // is not considered part of the code.
  • Every C file must include a main() {}. It is called the main function and it is the first place the computer looks at when running the code.
  • The printf is just a function that displays whatever is provided inside the parenthesis.
  • The quotation marks inside the parenthesis indicates that hello world is a string (ordered sequence of characters).
  • The \n indicates that a new line is to be created next.
  • The ; indicates the end of the statement.

No comments: