002a_Hou2 - Computer Science and Engineering

Download Report

Transcript 002a_Hou2 - Computer Science and Engineering

Structure of a C Program
Hours 2 and 3
Presentation original from Dr. Turner’s class
USF - COP-2270 - C for Engineers
Summer 2008
1
Fundamental Concepts
Objectives
You will be able to
 Identify and describe the stages of compilation of a C
program.
 Identify the parts of a simple C program.
 Use the preprocessor commands #include and
#define correctly and appropriately in a C program.
 Write comments in a C program.
USF - COP-2270 - C for Engineers
Summer 2008
2
The Compilation Process
References to external
functions (e.g., printf)
• Preprocesing (e.g. #include)
• Parsing (uses grammar rules of C)
• Code Generation (translate parsed
code into machine instructions)
The compiler
produces object code
Zhang, p. 35
Note: Most compilers (e.g.
gcc and Visual Studio’s )
runs both a compiler and a
linker.
USF - COP-2270 - C for Engineers
Summer 2008
3
Parts of a Program
/* This is the traditional first program. */
Comments
// Another way to write a comment
A preprocessor
directive
#include <stdio.h>
int main()
{
printf( "Hello, World! \n" );
return 0;
}
A function
definition
Every C program has a function named “main”.
This is where program execution begins.
USF - COP-2270 - C for Engineers
Summer 2008
4
#include
The preprocessor replaces
#include <stdio.h>
with the contents of the file stdio.h
which it finds in directory /usr/include (on Unix)
This is a header file for the standard IO library.
 Includes a declaration for the printf function.
 Without it the call to printf would cause a compile
error.
USF - COP-2270 - C for Engineers
Summer 2008
5
#include


You could include anything in your program
anywhere you like with the #include
directive.
But it is bad practice


to use #include for anything other than header
files
to put #include anywhere other than at the top of
your program
USF - COP-2270 - C for Engineers
Summer 2008
6
Functions



A block of code with a name.
In C, executable code exists only within
function definitions.
Examples:



main()
printf()
A function that we wrote
A function included with the
compiler
More about functions later in this class
USF - COP-2270 - C for Engineers
Summer 2008
7
Curly Brackets
Curly brackets mark the beginning and end of a block of code.
int main()
{
printf( "Hello, World! \n" );
return 0;
}

Every function definition is a block.


We will see other uses of blocks in the future.
Note alignment of the curly brackets.
USF - COP-2270 - C for Engineers
Summer 2008
8
Curly Brackets

Align the start and end brackets for each block.


This is a matter of programming style.




Indent everything inside the block by four spaces.
Makes the program easier to read and understand.
No effect on what the program does.
Compiler doesn’t care.
Many variations



Most organizations have their own style guidelines.
No widely accepted single standard.
Still, you will follow the rules of this course
USF - COP-2270 - C for Engineers
Summer 2008
9
Variables


A variable is a named location in the
program’s memory, where it can store a
number or a character for later use.
Variable names can consist of:

Letters (Upper and lower case)
The underscore character ( _ )
Digits ( 0 – 9) -- Except in initial position

Examples:




Student_Name
Address1
USF - COP-2270 - C for Engineers
Summer 2008
10
Invalid Names




Address%
Here&Now
First.Last
2_Be_or_Not
USF - COP-2270 - C for Engineers
Summer 2008
11
Variable Names

All identifiers in C are case sensitive.

Address1 and address1 are different names.
USF - COP-2270 - C for Engineers
Summer 2008
12
Variables

Use descriptive names for your variables


Avoid the predefined keywords of the C
langage.



Write for the reader!
See page 56 in the text.
(Don't need to memorize.)
Avoid names of system functions


printf
exit
USF - COP-2270 - C for Engineers
Summer 2008
13
Variables

Each variable has a type.



Examples




Specifies how it is to be used.
Determines the amount of memory used.
int
double
char
32 bit integer
64 bit floating point number
8 bit character (letter)
Sizes can vary from one system to another
but typically does not.
USF - COP-2270 - C for Engineers
Summer 2008
14
Declarations



Before using a variable you must tell the
compiler what type to use
Examples:
 int number_of_students;
 double distance
Put declarations at the beginning of the code
block where the variables are used.
USF - COP-2270 - C for Engineers
Summer 2008
15
Variables




Each variable has a memory address
determined by the compiler and linker.
We use the variable’s name in program
statements.
Actual machine instructions in the compiled
program use addresses.
The variable’s name is bound to its address
during the process of compiling and linking.
USF - COP-2270 - C for Engineers
Summer 2008
16
Expressions

We can combine variables and numbers in an
expression.

(2 + 3 ) * 10

(4 * a) + (5 * b) - c


These are called arithmetic expressions.
+, *, and – are arithmetic operators.
USF - COP-2270 - C for Engineers
Summer 2008
17
Arithmetic Operators

C defines five arithmetic operators





+
*
/
%
addition
subtraction
multiplication
division
remainder (or modulus)
USF - COP-2270 - C for Engineers
Summer 2008
18
Arithmetic Operators

There is no operator for exponentiation.
We can’t say area = pi * r2 ;
or
Instead:

area = pi * r^2;
area = pi * r * r;
Why?
USF - COP-2270 - C for Engineers
Summer 2008
19
Precedence
2*a+b

Does this mean
(2*a) + b
or
2 * (a+b)
?
USF - COP-2270 - C for Engineers
Summer 2008
20
Precedence


* and / have higher precedence than + and –
Operators are applied in order of precedence,
high to low

2 * a + b means (2*a) + b

What you were taught in high school algebra.

When in doubt, use parentheses!
USF - COP-2270 - C for Engineers
Summer 2008
21
Statements


One "sentence" of a program.
Typically sets a variable to the value an
expression

a = 2*x + b;

Terminated by a semicolon.

White space (including new lines) is not significant
except to separate identifiers.
USF - COP-2270 - C for Engineers
Summer 2008
22
Statements
a = 2*x + b;


This is an assignment statement.
Says set variable a to the value of the
expression 2*x + b.


Different from algebra!
Note flow from right to left.
USF - COP-2270 - C for Engineers
Summer 2008
23
Example: A Program that Computes
Open Visual Studio and Create a New Project
USF - COP-2270 - C for Engineers
Summer 2008
24
Creating New Project
USF - COP-2270 - C for Engineers
Summer 2008
25
Creating New Project
USF - COP-2270 - C for Engineers
Summer 2008
26
Add a Source File
USF - COP-2270 - C for Engineers
Summer 2008
27
Add a Source File
USF - COP-2270 - C for Engineers
Summer 2008
28
Source File Open in the Editor
USF - COP-2270 - C for Engineers
Summer 2008
29
Example: A Program that Computes
#include <stdio.h>
int main()
{
int a;
int b;
int c;
a = 5;
b = 7;
c = a + b;
printf ("The result is %d\n", c );
getchar();
return 0;
}
Build and run.
/* Keep the window open. */
USF - COP-2270 - C for Engineers
Summer 2008
30
Program Running
End of Section
USF - COP-2270 - C for Engineers
Summer 2008
31