MESSAGE
DATE | 2008-07-01 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C programming Workshop part II
|
C is a particularly detailed programming language. The next section in our text covers the an brief introduction to how C handles symbolic variables. C is very close to the computer, both the CPU and the RAM. That is one of the reasons why almost all operating systems and device drivers are written in C. With the introduction to variables, we are getting our first look at some of this detail that is the bane of all C coders.
C is called a “Typed†language. What that means is that each variable, in fact each object in C, is defined as storing a specific type of data. That data is itself sorted in the RAM, a certain number of bytes for each type of data. Variables in modern C are declared, and then used. You can't (or you shouldn't) just create variables randomly in your code. First you should declare them for your compiler, to tell it what kind of variable your going to be asking for, so that the compiler can do both data checking and create space for your variables.
Let's look at the example program on page 19:
#include
int main(){ int height, length, width, volume, weight;
height = 8; length = 12; width = 10; volume = height * width * length; weight = (volume + 165)/166; printf ("Dimensions: %d x %d x %d\n", height, width, length); printf ("Volume (cubic inches): %d\n", volume); printf( "Dimensional Weight: %d", weight);
return 0; } ~ ~ ~ ~ ~ For the benefit of those of us programming in VIM, I'd just like to point out that you can compile this program straight from the command mode of your editor with the following command.
!gcc -Wall -o file3 file3.c
It would look like this in your terminal:
#include
int main(){ int height, length, width, volume, weight;
height = 8; length = 12; width = 10; volume = height * width * length; weight = (volume + 165)/166; printf ("Dimensions: %d x %d x %d\n", height, width, length); printf ("Volume (cubic inches): %d\n", volume); printf( "Dimensional Weight: %d", weight);
return 0; } ~ ~ ~ ~ ~ :!gcc -Wall -o file3 file3.c
This says open a shell and run the gcc command. You can see it run and examine the errors, just like an expensive Rapid Development Environment.
In this case there are no errors or warnings. Everything is kosher.
You can now run your program (also from VIM using the ! trick).
#include
int main(){ int height, length, width, volume, weight;
height = 8; length = 12; width = 10; volume = height * width * length; weight = (volume + 165)/166; printf ("Dimensions: %d x %d x %d\n", height, width, length); printf ("Volume (cubic inches): %d\n", volume); printf( "Dimensional Weight: %d", weight);
return 0; } ~ ~ ~ ~ ~ :!./file3 resulting in the following shell interaction:
Dimensions: 8 x 10 x 12 Volume (cubic inches): 960 Dimensional Weight: 6 Hit ENTER or type command to continue
The last line is produced by VIM, not your program. In combination with ctags, Make and gdb, this makes VIM a fast and powerful development tool. One should also become familiar with the following VIM commands:
:syntax on :cindent :Tohtml
This program has proper typing and declaration of variables. Notice that comas are used in the syntax in declare a group of variables together (as ints in this case). later we assign them values. The assignment operator (=), is a leftward operator, that is everything that is on the right is evaluated first and then assigned to a variable on the right. Some operators are rightward. This concept is covered more deeply on the Programming with Perl program.
Once a variable is declared, not that the program when run will assign memory space for that variable. That RAM is not likely to be empty. And in fact, for fun you can alter this program and see what these variables might have stored in them before you run the assignment. It should seemly be random numbers. Because the RAM that is assigned to your variables likely have something already there, C allows for the assignment of variables when they are declared. For the int type, the syntax is more or less the same as any other assignment in your program. For other types, as we shall see later, distinctly different syntax exists for initialization (declaration with assignment) and normal assignment.
We can alter this program as follows and gain the same results.
1 #include 2 3 int main(){ 4 int height = 8, length = 12, width = 10, volume, weight; 5 6 volume = height * width * length; 7 weight = (volume + 165)/166; 8 printf ("Dimensions: %d x %d x %d\n", height, width, length); 9 printf ("Volume (cubic inches): %d\n", volume); 10 printf( "Dimensional Weight: %d", weight); 11 12 return 0; 13 } 14 15
If we examine the function printf, notice is generalized form
printf(“some format string in double quotesâ€, exp, exp ,exp);
where the format string can have any number of %Char meta char combinations, %d in this case, and where exp can be any expression that returns a value. We'll see a lot more detail on printf later.
Next post is on reading input and scanf fundamental.
Note, tell your firends to join. The hangout mailing list in subscribeable from the NYLXS website at
http://www.nylxs.com/mailing.html
and of course this is also archived on the same site along with all the other hangout mail. I'll need to alter the archive to put a search function in the near future. Code examples are on the nylxs docs directory
http://www.nylxs.com/docs/workshops/
soon to be linked in from the resources pages.
Ruben -- 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
|
|