Basic Programming in C Under Linux


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:

  • The GNU Compiler Collection, which will compile and link programs written in seven programming languages.

  • The GNU Debugger (gdb), which identifies and helps you troubleshoot problematic code.

  • The Make utility, which pulls multiple source-code files into a single working package.

  • Autoconf, which allows a programmer to create customized compiling and installation configurations for a program's source code.

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:

1.

Open your favorite text editor.

2.

Type the source code.

3.

Save the file with the appropriate extension (.c for a program or .h for a header file).

4.

Repeat steps 2 and 3 as necessary if your application's source code will be broken down into multiple files.

5.

Compile the program with GCC.

6.

Run the program and then debug any problems.

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).



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net