Lecturas W1
Lecturas W1
Lecturas W1
computer programs. Programmers around the world embrace C because it gives maximum control and
efficiency to the programmer.
You will be able to read and write code for a large number of platforms -- everything from
microcontrollers to the most advanced scientific systems can be written in C, and many modern
operating systems are written in C.
The jump to the object oriented C++ language becomes much easier. C++ is an extension of C,
and it is nearly impossible to learn C++ without learning C first.
If you are a programmer, or if you are interested in becoming a programmer, there are a couple of
benefits you gain from learning C:
In this article, we will walk through the entire language and show you how to become a C programmer,
starting at the beginning. You will be amazed at all of the different things you can create once you
know C!
What is C?
C is a computer programming language. That means that you can use C to create lists of instructions
for a computer to follow. C is one of thousands of programming languages currently in use. C has been
around for several decades and has won widespread acceptance because it gives programmers
maximum control and efficiency. C is an easy language to learn. It is a bit more cryptic in its style than
some other languages, but you get beyond that fairly quickly.
C is what is called a compiled language. This means that once you write your C program, you must run
it through a C compiler to turn your program into an executable that the computer can run (execute).
The C program is the human-readable form, while the executable that comes out of the compiler is the
machine-readable and executable form. What this means is that to write and run a C program, you
must have access to a C compiler. If you are using a UNIX machine (for example, if you are writing CGI
scripts in C on your host's UNIX computer, or if you are a student working on a lab's UNIX machine), the
C compiler is available for free. It is called either "cc" or "gcc" and is available on the command line. If
you are a student, then the school will likely provide you with a compiler -- find out what the school is
using and learn about it. If you are working at home on a Windows machine, you are going to need to
download a free C compiler or purchase a commercial compiler. A widely used commercial compiler is
Microsoft's Visual C++ environment (it compiles both C and C++ programs). Unfortunately, this
program costs several hundred dollars. If you do not have hundreds of dollars to spend on a
commercial compiler, then you can use one of the free compilers available on the Web. See
http://delorie.com/djgpp/ as a starting point in your search.
We will start at the beginning with an extremely simple C program and build up from there. I will
assume that you are using the UNIX command line and gcc as your environment for these examples; if
you are not, all of the code will still work fine -- you will simply need to understand and use whatever
compiler you have available.
Let's get started!
The Simplest C Program
Let's start with the simplest possible C program and use it both to understand the basics of C and the C
compilation process. Type the following program into a standard text editor (vi or emacs on UNIX,
Notepad on Windows or TeachText on a Macintosh). Then save the program to a file named samp.c. If
you leave off .c, you will probably get some sort of error when you compile it, so make sure you
remember the .c. Also, make sure that your editor does not automatically append some extra
characters (such as .txt) to the name of the file. Here's the first program:
#include <stdio.h>
int main()
{
printf("This is output from my first program!\n");
return 0;
}
A computer program is the key to the digital city: If you know the language, you can get a computer to
do almost anything you want. Learn how to write computer programs in C.
When executed, this program instructs the computer to print out the line "This is output from my first
program!" -- then the program quits. You can't get much simpler than that!
On a UNIX machine, type gcc samp.c -o samp (if gcc does not work, try cc). This line invokes the C
compiler called gcc, asks it to compile samp.c and asks it to place the executable file it creates
under the name samp. To run the program, type samp (or, on some UNIX machines, ./samp).
On a DOS or Windows machine using DJGPP, at an MS-DOS prompt type gcc samp.c -o samp.exe.
This line invokes the C compiler called gcc, asks it to compile samp.c and asks it to place the
executable file it creates under the name samp.exe. To run the program, type samp.
If you are working with some other compiler or development system, read and follow the
directions for the compiler you are using to compile and execute the program.
The line int main() declares the main function. Every C program must have a function named
main somewhere in the code. We will learn more about functions shortly. At run time, program
execution starts at the first line of the main function.
In C, the { and } symbols mark the beginning and end of a block of code. In this case, the block of
code making up the main function contains two lines.
The printf statement in C allows you to send output to standard out (for us, the screen). The
portion in quotes is called the format string and describes how the data is to be formatted when
printed. The format string can contain string literals such as "This is output from my first
program!," symbols for carriage returns (\n), and operators as placeholders for variables (see
below). If you are using UNIX, you can type man 3 printf to get complete documentation for the
printf function. If not, see the documentation included with your compiler for details about the
printf function.
The return 0; line causes the function to return an error code of 0 (no error) to the shell that
started execution. More on this capability a bit later.
Variables
As a programmer, you will frequently want your program to "remember" a value. For example, if your
program requests a value from the user, or if it calculates a value, you will want to remember it
somewhere so you can use it later. The way your program remembers things is by using variables. For
example:
int b;
This line says, "I want to create a space called b that is able to hold one integer value." A variable has a
name (in this case, b) and a type (in this case, int, an integer). You can store a value in b by saying
something like:
b = 5;
You can use the value in b by saying something like:
printf("%d", b);
int - integer (whole number) values
float - floating point values
char - single character values (such as "m" or "Z")
In C, there are several standard types for variables:
We will see examples of these other types as we go along.
Printf
The printf statement allows you to send output to standard out. For us, standard out is generally the
screen (although you can redirect standard out into a text file or another command).
Here is another program that will help you learn more about printf:
#include <stdio.h>
int main()
{
int a, b, c;
a = 5;
b = 7;
c = a + b;
printf("%d + %d = %d\n", a, b, c);
return 0;
}
Type this program into a file and save it as add.c. Compile it with the line gcc add.c -o add and then run
it by typing add (or ./add). You will see the line "5 + 7 = 12" as output.
The line int a, b, c; declares three integer variables named a, b and c. Integer variables hold
whole numbers.
The printf statement then prints the line "5 + 7 = 12." The %d placeholders in the printf
statement act as placeholders for values. There are three %d placeholders, and at the end of the
printf line there are the three variable names: a, b and c. C matches up the first %d with a and
substitutes 5 there. It matches the second %d with b and substitutes 7. It matches the third %d
with c and substitutes 12. Then it prints the completed line to the screen: 5 + 7 = 12. The +, the =
and the spacing are a part of the format line and get embedded automatically between the %d
operators as specified by the programmer.
Accidentally putting a ; at the end of a for loop or if statement so that the statement has no
effect - For example: for (x=1; x<10; x++); printf("%d\n",x); only prints out one value because the
semicolon after the for statement acts as the one line the for loop executes.
C Errors to Avoid
Makefiles
It can be cumbersome to type all of the gcc lines over and over again, especially if you are making a lot
of changes to the code and it has several libraries. The make facility solves this problem. You can use
the following makefile to replace the compilation sequence above:
main: main.o util.o
gcc -o main main.o util.o
main.o: main.c util.h
gcc -c -g main.c
util.o: util.c util.h
Week 0 Page 6
util.o: util.c util.h
gcc -c -g util.c
Enter this into a file named makefile, and type maketo build the executable. Note that you must
precede all gcc lines with a tab. (Eight spaces will not suffice -- it must be a tab. All other lines must be
flush left.)
This makefile contains two types of lines. The lines appearing flush left are dependency lines. The lines
preceded by a tab are executable lines, which can contain any valid UNIX command. A dependency line
says that some file is dependent on some other set of files. For example, main.o: main.c util.h says that
the file main.o is dependent on the files main.c and util.h. If either of these two files changes, the
following executable line(s) should be executed to recreate main.o.
Note that the final executable produced by the whole makefile is main, on line 1 in the makefile. The
final result of the makefile should always go on line 1, which in this makefile says that the file main is
dependent on main.o and util.o. If either of these changes, execute the line gcc -o main main.o util.o
to recreate main.
It is possible to put multiple lines to be executed below a dependency line -- they must all start with a
tab. A large program may have several libraries and a main program. The makefile automatically
recompiles everything that needs to be recompiled because of a change.
If you are not working on a UNIX machine, your compiler almost certainly has functionality equivalent
to makefiles. Read the documentation for your compiler to learn how to use it.
Now you understand why you have been including stdio.h in earlier programs. It is simply a standard
library that someone created long ago and made available to other programmers to make their lives
easier.
Week 0 Page 7