SUSE Linux 10.0 Unleashed
Unix and the C programming language have been partners from the very beginning. The vast majority of Unix and Linux code (including the Linux kernel) is written in C, and that tradition continues. C is a platform-neutral language; the code is not dependent on any particular system architecture. C is a compiled language, so you won't need any other special programs to run your applications aside from the editor and compiler you use to write it. Interpreted languages, such as Perl and Python, need a separate program known as an interpreter to run. You first enter your C program's statements (called source code) into a text file. This code is then fed to a compiler that translates the source code you've written into a binary file the ones and zeros that the machine can read. Often, a linker is used to connect the program with other system libraries or with other programs. As you saw in Chapter 5, "Getting Started with SUSE Linux," SUSE Linux offers a variety of text editors. Most contain special features for programming in assorted languages, such as highlighting commands in different colors and checking for mismatched parentheses and brackets. Programmers will also find the following essential tools in SUSE Linux:
Note These programming tools are not installed by default when you install SUSE Linux. You can easily get them using YaST, and they must be installed before any of the programs mentioned in this chapter will work on your system.
A Simple C Program
C program source code files come in two types: header, or include, files and programs or procedural files. Standard C program files have a *.c extension and contain at least one function. Functions are a set of commands a program carries out to accomplish its tasks. Header files have an *.h extension and carry variable descriptions, declarations, and other data common to all source files in the project. Writing simple programs in C is a six-step process:
Since time immemorial, the first program taught to students of a new language is called "Hello World" and consists of printing those two words on your screen. It's a short program, but its aim is to demonstrate the basic syntax of the language. We will follow the steps listed previously to write a "Hello World" program in C. Open your favorite text editor and write the following source code text, exactly as you see it. Press Enter to insert a new line after the final brace. #include <stdio.h> int main(int argc, char* argv[]){ printf("Hello, World.\n"); return 0; } Details vary from editor to editor, but chances are good that your braces are highlighted in color, as is the quoted phrase. Now save this file as a plain text document named hello.c to a source directory, either in Home (/~/source) or in Documents (/~/Documents/source). |