A Word on C
A Word on C
In This Chapter
Hello World
Conclusion
Next Steps
Having been exposed to the C language syntax in Chapter 2, "Programming Constructs," we now focus full attention on the structure and convention of the language.
Every year the C community runs a contest to see which participant can write the most obfuscated C code.
obfuscate: -cated, -cating, -cates. 1. a. To render obscure. b. To darken. 2. To confuse.
The point of the contest is to show the importance of programming style in an ironic way and to emphasize subtleties of the C programming language. Although other structured programming languages have intricacies of their own, C inherently lends itself to the contest by offering features that are often confusing.
This chapter will lead you through a review of the syntax seen in the examples of Chapter 1, "UNIX for Developers," and Chapter 2, "Programming Constructs," and expand your exposure to the C language.
If you are already comfortable with your understanding of the C programming language, feel free to proceed to the discussion in Chapter 4, "Windowing Concepts."
The C programming language has a significant evolutionary history in the world of computer science. It was derived from the B language written by Ken Thompson in the late 1960s. (The predecessor of B was BCPL written by Martin Richards.)
The importance of this history is to recognize that C did not appear as a new language but was adapted from an existing language.
C's predecessor, B, was a language without types wherein it was up to the programmer to ensure that variables were used in a valid context.
The previous chapter discussed data types and the challenge they bring to computer programming. On the heels of this, you have to wonder how confusing the B language could have been.
Dennis Ritchie wrote C in the early 1970s, keeping most of B's syntax. Two elements that are characteristic of C, and often cause the most confusion with the language, are the relationship between pointers and arrays and the similarities between declaration syntax and expression syntax.
Developing the necessary awareness of C pitfalls while learning correct programming structure and C syntax is a goal of this chapter.
Hello World
A constant when teaching C is that at the end of the exercise the student is able to print Hello World. Using this as our structured example and bringing together lessons from previous discussions, let's begin with a file to hold the C source code.
A file can be created using the vi command (refer to "The vi Editor" in Chapter 1, page 22 for a review of the command):
bash[1]: vi first.c
Because every C program must define a function called main, begin by inserting the following code into the file:
0: /* the start of the program */ 1: int main( int argc, char *argv[ ] ) 2: { 3: printf("Hello World"); 4: 5: return( 0 ); 6: }