Professional Documents
Culture Documents
Introduction
ME 325
Spring, 2007
C Compiler
• Below is a link for a free C/C++
compiler
• http://www.bloodshed.net/dev/devcpp.
html
Overview
• History and Philosophy of C
• Compiler
• First program
• printf
History
• The initial development of C occurred at AT&T Bell Labs
between 1969 and 1973; It was named "C" because many of its
features were derived from an earlier language called "B" .
• Identify Input
• Example
if Credit Card Valid
Process valid credit card routine
else
Process invalid credit card routine
end if
Flowchart
• Symbols
– Start and end symbols, represented as ovals
or rounded rectangles
– Arrows, showing what's called "flow of
control" in computer science.
– Processing steps, represented as
rectangles. Examples: "Add 1 to X"; "replace
identified part"; "save changes" or similar.
– Input/Output, represented as a
parallelogram. Examples: Get X from the
user; display X.
– Conditional (or decision), represented as a
diamond (rhombus). These typically contain a
Yes/No question or True/False test. This
symbol is unique in that it has two arrows
coming out of it.
– A Data File represented by a cylinder
Program Development Cycle
Write
Pseudocode
Program
Source Code
Compile/ Libraries
Link
Executable
Test
Document
C Compile Model
• C is 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
The C Compilation Model
The Preprocessor
• The Preprocessor accepts source code as input
and is responsible for
– removing comments
– interpreting special preprocessor directives denoted
by #.
• #include -- includes contents of a named file. Files
usually called header files. e.g
• #define -- defines a symbolic name or constant. Macro
substitution.
• #define MAX_ARRAY_SIZE 100
Compiler
• The C compiler translates source to assembly
code. The source code is received from the
preprocessor.
Assembler
• The assembler creates object code. (.OBJ on
MSDOS) to indicate object code files.
Link Editor
• If a source file references library functions or
functions defined in other source files the link
editor combines these functions (with main()) to
create an executable file.
Hello World,
a Program is born
#include <stdio.h>
int main()
{
printf(“Hello World\n");
return 0;
}
Hello World,
Explanation
• #include <stdio.h>, this line includes the "standard I/O
library" into your program. The standard I/O library lets you
read input from the keyboard (called "standard in"), write
output to the screen (called "standard out"), process text files
stored on the disk, and so on. C has a large number of standard
libraries like stdio, including string, time and math libraries. A
library is simply a package of code that someone else has
written to make your life easier.
• The line int main() declares the main function. Every C
program must have a function named main somewhere in the
code. 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 (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 “Hello World" symbols for carriage returns (\n), and
operators as placeholders for variables.
printf ( const char * format,
... );
• Output formatted string to the standard
output (stdout).
Flags description
- Left-justify within the given field width; Right
justification is the default
+ Forces to preceed the result with a plus or minus sign
0 (+ or -) the number with zeroes (0) instead of spaces
Left-pads
(width)
• #include <stdio.h>
• int main()
• {
• printf ("Characters: %c %c \n", 'a', 65);
• printf ("Decimals: %d %ld\n", 1977, 650000);
• printf ("Preceding with blanks: %10d \n", 1977);
• printf ("Preceding with zeros: %010d \n", 1977);
• printf ("Some different radixes: %d %x %#x \n", 100, 100, 100);
• printf ("floats: %4.2f %E \n", 3.1416, 3.1416);
• printf ("Width trick: %*d \n", 5, 10); /* This will place 5 spaces thin print 10 */
• printf ("%s \n", "A string");
• return 0;
• }
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 0x64
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string