Introduction to C Topics Compilation Using the gcc Compiler

advertisement
Introduction to C
Topics
• Compilation
• Using the gcc Compiler
• The Anatomy of a C Program
Reading
• Sections 2.1 - 2.6
L07
1
Writing C Programs
• A programmer uses a text editor to create or
modify files containing C code.
• C code is also called source code.
• A file containing source code is called a source
file.
• After a source file has been created, the
programmer must invoke the C compiler before
the program can be executed (run).
L07
2
Using the C Compiler
• Invoking the compiler is system dependent.
o
o
L07
At UMBC, we have two C compilers available,
cc and gcc.
For this class, we will use the gcc compiler as it
is the compiler available on the Linux system.
3
Invoking the gcc Compiler
At the prompt, type
gcc -ansi -Wall pgm.c
where pgm.c is the C program source
file.
L07
4
To specify your own executable filename
gcc -ansi -Wall project1.c -o myname.out
Where project1.c is the source file that
contains a program, and the executable file
created by the compiler is: myname.out
not the usual a.out filename.
L07
5
The Result : a.out
• If there are no errors in pgm.c, this
command produces an executable file,
which is one that can be executed (run).
o
o
Both the cc and the gcc compilers name the
executable file a.out
To execute the program, at the prompt, type
a.out
• Although we call this “compiling a program,”
what actually happens is more complicated.
L07
6
3 Stages of Compilation
Stage 1: Preprocessing
o
o
L07
Performed by a program called the preprocessor
Modifies the source code (in RAM) according to
preprocessor directives (preprocessor
commands) embedded in the source code.
o
Strips comments and whitespace from the code
o
The source code as stored on disk is not modified.
7
3 Stages of Compilation (con’t)
Stage 2: Compilation
o
o
o
o
Performed by a program called the compiler
Translates the preprocessor-modified source
code into object code (machine code)
Checks for syntax errors and warnings
Saves the object code to a disk file, if instructed
to do so.
o If any compiler errors, compiler warnings, or linker
errors are received, no object code file will be
generated
L07
8
3 Stages of Compilation (con’t)
Stage 3: Linking
o
o
o
Combines the program object code with other
object code to produce the executable file.
The other object code can come from the RunTime Library, other libraries, or object files that
you have created.
Saves the executable code to a disk file. On
the Linux system, that file is called a.out.
o If any compiler errors, compiler warnings, or linker
errors are received, no executable file will be
generated
L07
9
Program Development Using gcc
Editor
Source File pgm.c
Preprocessor
Modified Source Code in RAM
Compiler
Program Object Code File pgm.o
Other Object Code Files
Linker
Executable File a.out
L07
10
An algorithm for writing code
L07
Write the algorithm
Write the code using emacs (pico, vi)
Try to compile the code
While there are still syntax errors
Fix errors
Try to compile the code
Test the program
Fix any semantic errors
Compile the code
Test the program
11
Incremental Approach to Writing Code (I)
• Tips about writing code.
• Write your code in incomplete but working
pieces.
• For instance: For your project
o
o
o
o
L07
Don’t write the whole program at once.
Just write enough that you display the
prompt to the user on the screen.
Get that part working first.
Next write the part that gets the value from
the user, and then just print it out.
12
Incremental Approach to Writing Code (II)
o
o
o
o
Get that working.
Next change the code so that you use the value
in a calculation and print out the answer.
Get that working.
Make program modifications:
– perhaps additional instructions to the user
– a displayed program description for the user
– add more comments.
o
L07
Get the final version working.
13
A Simple C Program
/* Filename:
Author:
Date written:
Description:
hello.c
Brian Kernighan & Dennis Ritchie
?/?/1978
This program prints the greeting
“Hello, World!”
*/
#include <stdio.h>
main ( )
{
printf ( “ Hello, World! \n ” ) ;
}
L07
14
Anatomy of a C Program
program header comment
preprocessor directives (if any)
main ( )
{
statement(s)
}
L07
15
Program Header Comment
• All comments must begin with the characters
/* and end with the characters */
• These are called comment delimiters
• The program header comment always
comes first.
• Look at the class web page for the required
contents of our header comment: such
things as filename, author, date written and
description of the program.
L07
16
Preprocessor Directives
• Lines that begin with a # in column 1 are
called preprocessor directives (commands).
• Example: the #include <stdio.h> directive
causes the preprocessor to include a copy
of the standard input/output header file
stdio.h at this point in the code.
• This header file was included because it
contains information about the printf ( )
function that’s used in this program.
L07
17
Preprocessor Directives
• The preprocessor Directive files can be
found in a directory on the system called:
/usr/include
• You can look at the contents of these files
with a unix command like:
more /usr/include/stdio.h
L07
18
main ( )
• Every program must have a function called
main. This is where program execution
begins.
• main() is placed in the source code file as
the first function for readability.
• The parentheses following the reserved
word main indicate to the compiler that it is
a function.
L07
19
Braces
• A left brace (curly bracket) -- { -- begins the
body of every function. A corresponding right
brace -- } -- must end the function.
• The style is to place these braces on separate
lines in column 1.
L07
20
printf ( “ Hello, World! \n ” ) ;
• This line is a C statement.
• It a call to the function printf ( ) with a
single argument (parameter), namely the
string “ Hello, World! \n ”
• Even though a string may contain many
characters, the string itself should be
thought of as a single quantity.
• Notice that this line ends with a semicolon.
All statements in C end with a semicolon.
L07
21
Another C Program
/*****************************************
** File: proj1.c
** Author: Joe Student
** Date: 9/15/01
** ID #: xxx-xx-6789
** Section: 0304
** E-mail: jstudent22@umbc.edu
**
** This program prompts the user for two integer values then displays
** their product.
**
***********************************************/
L07
22
Another C Program (con’t)
#include <stdio.h>
int main()
{
int value1, value2, product ;
printf(“Enter two integer values: “) ;
scanf(“%d%d”, &value1, &value2) ;
product = value1 * value2 ;
printf(“Product = %d\n”, product) ;
return 0 ;
}
L07
23
Another C Program (con’t)
• Note the use of the reserved word int:
int main()
• Many C compilers will cause main() to
default to int if it is omitted. However, the
gcc compiler will issue a warning.
• Also note the last line:
return 0 ;
• This statement must also be included as the
last line of your program to avoid a compiler
warning.
L07
24
Good Programming Practices
• C programming standards are available on
the class homepage.
• You are expected to conform to these
standards for all programming projects in
this class and in CMSC 201. (This is part of
your grade for your project!)
• The program just shown conforms to these
standards, but is uncommented (later)
• Subsequent lectures will include “Good
Programming Practices” slides.
L07
25
Examples of Comment Styles
/* a comment */
/*** another comment ***/
/*****/
/*A comment can be written in this
* fashion to set it off from the
* surrounding code.
*/
L07
26
More Comments
/*******************************************\
* If you wish, you can put comments *
* in a box. This is typically used for *
* program header comments and for *
* function header comments
*
\*******************************************/
L07
27
Use of Whitespace
• Use blank lines to separate major parts of a
source file or function
• Separate declarations from executable
statements with a blank line
• Preprocessor directives, main(), and the
braces are in column 1
L07
28
Use of Whitespace (continued)
• All executable statements are indented
L07
one tab stop. How deep should my tabs
be ?
• Either 3 or 4 spaces. Choose
whichever you like and use that same
number consistently throughout your
program.
• 2 spaces are not enough for good
readability, more than 4 causes the
indentation to be too deep.
29
Download