MESSAGE
DATE | 2008-06-28 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C Programing Workshop
|
The C programming language was invented side by side with Unix. The King text goes through much of the history of the language. Here I'd like to make a few points both in the general and specific to GNU/Linux as a platform for C development.
Oxymoronic as it sounds C language is a bare bones specification without anything useful. All it requires is the keyword main, which defines the root function that everything else is piled into. Everything that C does is imported from external libraries. These libraries are defined, in the simplist case, on top of your program.
For example, on page 10 of the King book is the following program:
#include
main(){ printf("To C, or not to C: that is the question \n"); }
which was inherited from the K&R text. The first line defines the library stdio.h which c is to import. The main keyword is present (which defines the required function main() to be explained later, and the function printf which is defined in the library stdio.h.
Unlike programming languages such as Perl, Python and Rudy, C is a compiled programming language. It needs a compiler to make a binary program that can then be executed. The binary has machine like instructions which your computer can directly understand. Unfortunately, your computer doesn't speak English (or Spanish, Russian, Japanese, Manderin, Hebrew, or Udo). It speaks binary. C is the interface between human language and machine code. The binaries are very low level and C as a language, today, is considered to be very close to the machine. You'd have to write programs in Assembly to get much close to your CPU.
Therefor, you need a C compiler to translate your C Programming text into machine code. At one time suc compilers were expensive and hard to get. Thanks to Richard Stallman and friends, today we have compilers on almost every GNU based system. That compiler is called gcc. Compilers do three things: Parse, compile and link.
When you call:
gcc file.c
It loads file.c into the compiler. It then parses the programming you created and checks for errors. It then analysis the symbols in your program and looks for their programming definitions in the libraries that you call with #include. It then the different code machine code peices it finds into a single binary.
The gcc compiler is very complex and learning C includes learning much about the compiler and also the debugger, called gdb. Both are documented with man pages, and have full books which are available from the Free Software Foundation and worthy additions to your library. Both are included in the NYLXS library as references.
Any editor can be used to make C programs, but I strongly recommend learning either EMACS or VI. EMACS has a GNU version, and VI has a version called VIM which can be downloaded and used. I perfer the VIM editor and will use in in example and demonstrations. No editor wars are needed here. I will say, however, if your using EMACS, then LEARN it. It is not Microsoft Notepad.
After editing this first program run the following command on the command line: For this workshop, the "$" or "ruben-at-www2:~/cprog>" is the shell prompt. Your prompt on the NYLXS server will look like the second example.
$ gcc -Wall file1.c -o file1
the output will look like this: ruben-at-www2:~/cprog> gcc -Wall file1.c -o file1 file1.c:3: warning: return type defaults to `int' file1.c: In function `main': file1.c:5: warning: control reaches end of non-void function ruben-at-www2:~/cprog>
The compiler gives you 3 warning messages. I won't go into them currently, but in a few weeks it should make sense to you. We will, however, fix the warnings later. For now, if you look at your directory
ruben-at-www2:~/cprog> ls -l total 12 -rwxr-xr-x 1 ruben staff 7041 2008-06-28 21:45 file1 -rw-r--r-- 1 ruben staff 86 2008-06-28 21:35 file1.c
file1.c is the file we are editing. You see it has no permisions to be executed. file1 is the new executable we created. In addition. you see that it is executable. gcc makes it executable permisions for us in most systems. We run our new program on the command line like this:
ruben-at-www2:~/cprog> ./file1 To C, or not to C: that is the question
The C language defines the function main() and not much else. However, it is defined as a function that returns an integer. We didn't return anything. We can fix this with a "return function".
We return a 0 by convention unless we are trying to indicate an error or other message. We also put the keyword 'int' infront of main which explicitly defines that main returns integers.
Our program not looks like this:
#include
int main(){ printf("To C, or not to C: that is the question \n"); return 0; }
and when we compile:
ruben-at-www2:~/cprog> gcc -Wall file2.c -o file2 ruben-at-www2:~/cprog>
No warnings.
and when we run it, it looks like this:
ruben-at-www2:~/cprog> ./file2 To C, or not to C: that is the question ruben-at-www2:~/cprog>
Next - Comments and C varriable basics.
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
© Copyright for the Digital Millennium
|
|