Friday, May 17, 2013

Chapter 2 at last!

Just to give you all a heads up, there are some sections of the textbook that I won't necessarily take notes on. It's a great book, and I want to leave some content out of my blog so I don't get hammered with licensing issues or anything. I've been feeling like this is possibly treading a thin line.

I have a lot of respect for this author for making such a great resource, and I want to urge anyone following along to pick up the book as well. Maybe it isn't quite so bad if I generate some publicity for the book, right? :D

That being said, I will stop promptly if this blog is found to be too close to original content.

Anyway, Chapter 2 notes below the break.





C Fundamentals: This chapter has eight sections. We'll be in chapter 2 for a while! Over the course of the chapter, we'll be seeing simple programs, using comments, learning about variables and identifiers, and overall layout of programs. The chapter ends with exercises, but I'll see whether or not those make it into the blog, proper.

Writing a Simple Program

It seems like everyone who has pursued some form of education in C has seen K&R's classic first program, hello world. I hadn't even picked up a book yet, when I stumbled across it. That is not the first program this author has chosen to start with, however. The first program introduced in this book is the bad pun.

To C or not to C: that is the question.

The program below, pun.c, will display this phrase each time it runs.

#include <stdio.h>

int main(void)
{
   printf("To C, or not to C: that is the question.\n");
   return 0;
}

The first thing to notice is the first line.

#include <stdio.h>

This is necessary to "include" information about C's standard I/O (or input/output) library. The program's executable code goes inside main, which represents the "main" program, or the specific commands being issued.
printf is a function from the standard I/O library that can display formatted output. \n is a code that tells printf to advance to the next line.

return 0; indicates that the program is to "return" the value 0 when it terminates.

Compiling and Linking

This process begins by creating a file with this little program in it, and that is achieved with a simple txt file. Of course, it could be named anything, but the .c extension is required by the compiler. Then, the program needs to be converted from text to an executable program. The following steps outline this process:

  • Preprocessing is the first step, during which a preprocessor recognizes and obeys commands that begin with # (those commands are called directives).
  • Compiling occurs next, which translates the text into machine instructions (object code).
  • Finally, linking takes place, during which a linker combines object code with any additional code needed to complete the executable program. This additional code includes library functions, such as printf.
Much of this is automated, and it is unlikely that anyone using a compiler will actually see it happening. Depending on the compiler used, the commands will vary. In UNIX, the C compiler is typically named cc. To compile pun.c, the following line should be entered in a command-line window or terminal:

cc pun.c

(There will be a prompt preceding this command. On my distribution, the terminal has a $~ prompt, but on others it may be a %.)

Linking is automatic in this method.

After compiling and linking is complete, cc leaves the executable in a file named a.out by default. cc has many options, such as renaming.

cc -o pun pun.c

This will change the name of the executable version to simply pun.

There is also a compiler called the GCC Compiler that comes with Linux. Use is similar as with the traditional UNIX compiler, but the command will be preceded by gcc in place of cc.

The textbook goes on to describe Integrated Development Environments (IDEs). This is a software package that allows editing, compiling, linking, executing, and debugging without leaving the environment. These components are designed to work together; for example, highlighting an area of code that the compiler detects as an error. While no more information is given about them, the author urges researching IDEs to see which are available for you.

(GNU Emacs actually stuck out to me a bit. I like how it color codes the text.)

--

So, I'm sure if I had been patient enough, I would have seen in the textbook eventually how to actually run the program I wrote. This is very hands-on for me, so the moment I got home from work, I cracked open my zombiemac -- which my loving fiance had just finished installing with a beautiful graphics interface -- and wrote up a text file in Abiword. I'm rather proud to admit that I got almost all of the basics of the program down from memory, just not quite in the right order (I forgot "int", dammit).

I saved this file to the desktop with the proper file extension, and proceeded to try to compile it. No file in the directory. Oh! The directory wasn't set to "Desktop" (yes, that is case-sensitive, at least in my distribution). So, with my fiance watching over my shoulder, we set the directory to the Desktop and send the command to compile... And it works! We get an executable file called a.out!

Yaayy!!

But, double-clicking it doesn't do anything. I had wondered about this. Because, where would the program display the text? Not on the desktop... It would have no way to show up, unless it were run through the terminal!

A Google search later, and we brought up a handy command that hadn't been introduced in the textbook yet: ./

For anyone who is hasty, as I am, simply make sure your directory is set to where your file is saved, and enter "./a.out" (or whatever your compiled program is called), and it will happily run in terminal!

I'm about to dive into section 2 of chapter 2, but it has been a long day at work, and I'd love to do some gaming. Ciao for now!

No comments:

Post a Comment