Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Topics
Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation Styles
Reading
Sections 2.1 - 2.4
L08IntroToC.ppt
Writing C Programs
A programmer uses a text editor to create or modify files containing C code.
L08IntroToC.ppt
At UMBC, we have two C compilers available on the GL system, cc and gcc. There is also a C compiler on the CD in your book. Additionally, you can download a free version of cygwin that has a gcc compiler. For this class, we will use the gcc compiler as it is the compiler available on the Linux system. All grading is down on linux.gl.umbc.edu. If you use any other compiler, run it first on the system where the grading is being done. There are differences between the compilers and we only support the gcc compiler on linux.gl.umbc.edu version!
L08IntroToC.ppt
command produces an executable file, which is one that can be executed (run). The gcc compiler names the executable file a.out . To execute the program, at the prompt, type a.out Although we call this process compiling a program, what actually happens is more complicated.
CMSC 104, Version 8/06 L08IntroToC.ppt 5
3 Stages of Compilation
Stage 1: Preprocessing
o o
Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code The source code as stored on disk is not modified.
o o
L08IntroToC.ppt
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 (we will not do this).
o If any compiler errors are received, no object code file will be generated. o An object code file will be generated if only warnings, not errors, are received.
L08IntroToC.ppt
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 linker errors are received, no executable file will be generated.
L08IntroToC.ppt
Preprocessor
Modified Source Code in RAM
Compiler
Program Object Code File pgm.o Other Object Code Files (if any)
Linker
Executable File a.out
CMSC 104, Version 8/06 L08IntroToC.ppt 9
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>
int main ( void ) { printf ( Hello, World!\n ) ; return 0 ; }
CMSC 104, Version 8/06 L08IntroToC.ppt 10
Anatomy of a C Program
program header comment
reader of the program understand its content. 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 ourL08IntroToC.ppt comment. header CMSC 104, Version 8/06
12
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 is used in this program.
CMSC 104, Version 8/06 L08IntroToC.ppt 13
stdio.h
When we write our programs, there are
libraries of functions to help us so that we do not have to write the same code over and over. Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. Using the functions will also make it easier to learn to program!
CMSC 104, Version 8/06 L08IntroToC.ppt 14
main indicate that it is a function. The reserved word void means nothing is there.
CMSC 104, Version 8/06 L08IntroToC.ppt
15
body of every function. A corresponding right brace -- } -- ends the function body.
The style is to place these braces on
separate lines in column 1 and to indent the entire function body 3 to 5 spaces.
L08IntroToC.ppt
16
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.
CMSC 104, Version 8/06 L08IntroToC.ppt 17
return 0 ;
Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement
return 0 ;
indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to useL08IntroToC.ppt the statement. CMSC 104, Version 8/06
18
Another C Program
/***************************************** ** File: proj1.c
** their product.
** ***********************************************/
CMSC 104, Version 8/06 L08IntroToC.ppt 19
{
int value1, value2, product ; printf(Enter two integer values: ) ; scanf(%d%d, &value1, &value2) ; product = value1 * value2 ;
Tokens
The smallest element in the C language is
the token. It may be a single character or a sequence of characters to form a single item.
L08IntroToC.ppt
22
Tokens are:
Tokens can be:
o
o
o o o o o
Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators
L08IntroToC.ppt
23
Numeric Constants
Numeric constants are an uninterrupted
sequence of digits (and may contain a period). They never contain a comma. Examples:
o o o
L08IntroToC.ppt
24
Character Constants
Singular! One character defined character set. Surrounded on the single quotation mark. Examples:
o o o
A a $ 4
L08IntroToC.ppt 25
String Constants
A sequence characters surrounded by
Keywords
Sometimes called reserved words. Are defined as a part of the C language. Can not be used for anything else! Examples:
o o o
L08IntroToC.ppt
27
Names
Sometimes called identifiers or labels. Can be of anything length, but on the first
31 are significant (too long is as bad as too short). Are case sensitive:
o
letters, digits, and underscores. Must follow the standards for this course!
CMSC 104, Version 8/06 L08IntroToC.ppt 28
Punctuation
Semicolons, colons, commas, apostrophes,
L08IntroToC.ppt
29
Operators
There are operators for:
o o o o o o o o o o o o
assignments mathematical operations relational operations Boolean operations bitwise operations shifting values calling functions subscripting obtaining the size of an object obtaining the address of an object referencing an object through its address choosing between alternate subexpressions
L08IntroToC.ppt 30