Documente Academic
Documente Profesional
Documente Cultură
Most computer users will never write a computer program, just as most
people who enjoy music will never write a musical composition.
Three Requirements
Three things are necessary for creating C programs: a text editor, a compiler
and a C standard library. A text editor is all that is needed to create the
source code for a program in C or in any other language.
A text editor is a program for writing and editing plain text. It differs from a
word processor in that it does not manage document formatting (e.g.,
typefaces, fonts, margins and italics) or other features commonly used in
desktop publishing. C programs can be written using any of the many text
editors that are available for Linux, such as vi, gedit, kedit or emacs. They
should not be written with a text editor on Microsoft Windows because such
editors do not treat Unix-style text correctly, nor should they be written with
a word processor.
At least one text editor is built into every Unix-like operating system, and
most such systems contain several. To see if a specific text editor exists on
the system, all that is necessary is to type its name on the command line (i.e.,
the all-text user interface) and then press the ENTER key. It it exists, the
editor will appear in the existing window if it is a command line editor, such
as vi, or it will open in a new window if it is a GUI (graphical user interface)
editor such as gedit. For example, to see if vi is on the system (it or some
variation of it almost always is), all that is necessary is to type the following
command and press the ENTER key:
vi
All that is necessary to see if the GCC is already installed and ready to use is
to type the following command and press the ENTER key:
gcc
An error message such as gcc: no input files indicates that the GCC is
installed and ready to use. If there is a message such as command not found,
which usually indicates that it has not been installed, the GCC can be
installed in the same manner as other software (i.e., from the operating
system installation disks or by downloading if from the Internet).
The locate command can be used to confirm that glibc is already installed on
the system as follows:
locate glibc
If it is on the system, the above command will probably generate many lines
on the monitor screen referring to it, particularly referring to the directory
/usr/share/doc/.
Information about some of these programs, including the GCC, vi and gedit,
can be obtained from the man built-in manual. For example, information
about the GCC can be obtained by using the following command:
man gcc
The best way to learn about programming is to jump right in and begin
writing a very simple program. This approach helps make concepts, that
might otherwise seem vague and abstract, make sense, and the positive
feedback obtained from getting even an ultra-simple program to work
provides a strong incentive to improve it and write the next one.
The first step is to open a text editor and type the source code for this
program as follows:
#include <stdio.h>
main()
{
printf("Hello, world!\n");
return 0;
}
This code should (at least initially) be written exactly as shown. The file
should then be saved (i.e., written to the hard disk) using the save command
of the text editor.
The file can be given any desired name, but it is usually best to use a name
that is descriptive of the program. The name must end with a .c extension in
order to indicate to the compiler that it is C source code; if the .c extension is
omitted, an error message will be returned. (Extensions usually are not
necessary for files in Unix-like operating systems, but this is an exception.)
Thus, a good choice for the above example is hello.c.
Every C program has exactly one function named main(). A function is a set
of one or more statements that are enclosed in curly brackets, perform some
operation and return a single value to the program in which they reside; it
could also be looked at as a subprogram. The main() function is the starting
point of any program; that is, it is where the program begins execution (i.e.,
running); any other functions are subsequently executed.
printf()'s name reflects the fact that C was first developed in the early 1970s
when display monitors were rare, and thus all computer output was generally
printed out on paper. However, today printf() by default actually sends its
output to the monitor screen (referred to as standard output), not to a printer.
The f in printf stands for formatted.
The second statement in the main function is return 0;. It is not necessary for
the functioning of this simple program; however, it is included to illustrate
how a second statement can fit into a program. It is also included because
similar statements are required in more complex programs and thus it is
useful to become familiar with it from the beginning.
C programs are not fussy about the way they are laid out (in contrast to some
other programming languages). For example, hello.c could be written very
compactly in just two lines without affecting performance as follows:
#include <stdio.h>
main(){printf("Hello, world!\n");}
Compiling hello.c
After a program has been planned, written and checked for obvious errors, it
is ready to compile. Compilation consists of two main steps, compilation
itself (inclusive of preprocessing) followed by linking (i.e., connection of
multiple files in a program); the compiler usually performs the linking
automatically.
The program hello.c can be compiled with the GCC as follows:
The -o option informs the compiler of the desired name for the executable
(i.e., runnable) file that it produces. The name used in this example is hello,
but it could just as easily be anything else desired, such as hello.exe, file1 or
Bonjour. If the -o option is omitted, the compiler will give the name a.out to
the executable file.
ls hello.c hello
Possible Problems
Various things can go wrong when writing and compiling software. And
they frequently do, particularly for long and complex programs. Fortunately,
modern compilers generally provide fairly detailed error messages that
facilitate correcting errors in the source code.
There is little that can go wrong with the hello.c program because of its
brevity and simplicity, and any errors can prove instructive and are easy to
fix. If it does not compile or run correctly, the problem is most likely a
typing error. Such errors are among the most frequent sources of problems in
programming. Particularly common is forgetting one or more of the curly
brackets. This is especially easy to do in programs that contain multiple pairs
of brackets, some of which are enclosed in others.
Another common error is the omission of one or more semicolons at the ends
of statements. Yet another typical source of problems is forgetting the .c
extension at the end of the name of the source code file(s).
Running hello
Once the executable file has been created by the compiler, it is ready to run.
This can be accomplished on some systems by merely typing the file name
and pressing the ENTER key. On other systems, it might be necessary to
precede the file name with a dot and a forward slash and then press the
ENTER key, i.e.,
./hello
The result will be to write the text Hello, world! on the monitor screen.
hello.c may seem extremely simple and useless, and in some ways it is.
However, a little practice with it provides an excellent foundation for writing
longer programs that actually do something useful. And they are not much
more difficult than hello.c. A slightly more interesting example, and one that
introduces some additional C programming concepts, is provided on the page
How to Create a Second C Program on Linux.
LINFO
#include <stdio.h>
main() {
int count;
puts("Please enter a number: ");
scanf("%d", &count);
printf("The number is %d \n", count);
}
A Closer Look
int a, b, c;
The -o option tells the compiler to use the string that follows
it as the name for the executable (i.e., runnable) file that it
produces, which in this example is number. If this option is
omitted, the compiler will, by default, give the name a.out
to the executable file.
./number
Experiments
________
1
These benefits include safety (i.e., preventing attempts to
code some operations which are not valid in certain
contexts), optimization (e.g., providing useful information
to the compiler), abstraction (i.e., allowing programmers to
visualize programs in higher level terms), modularity
(which facilitates creating consistent interfaces between
subsystems) and documentation (which facilitates the
reading of complex code that others have written). C has
fewer data types than other popular programming languages
because of its extreme simplicity.