Title: Module 2
1Module 2
2What is a C Program?
- It is a collection of one or more functions
- Functions act as organizing groups, each doing a
smaller task and working together to produce the
desired result - A C program must have a function called main()
- Execution always begins with the first statement
in main() - All other functions are subprograms and are
executed only when called
3Implication for writing software?
- The natural way to organize your code is to have
the main() function serve as an outline - Keep functions relatively small (one to two
screen fulls) - Another term for function is methodI will use
the term function, mainly, since it is consistent
with our text
4The Smallest C Program
// This is a commentit is ignored by the
compiler! int main( ) //function
header //braces tell beginning or
ending return 0 //our first statement!
//everything inside and including
the //braces is the function body
5What is in a Function Header?
type of returned value
parameter listlist of inputs to function
int main ( )
name of the function
6So what does this program do?
// This is a commentit is ignored by the
compiler! int main( ) //function
header //braces tell beginning or
ending return 0 //our first statement! //e
verything inside and including the //braces is
the function body
7What Happens to the Comments?
- The compiler works in several stages, including
- Preprocessor
- Syntax recognition
- Semantics
- Optimizer
- One job of the preprocessor is to remove comments
- The preprocessor also notes which libraries to
load and takes care of defined macros
8General Structure of C Programs
include ltstdio.hgt include myheaderfile.h defi
ne PI 3.1415927 using namespace std const type
identifier type identifier function function
function
9Lets Start With Identifiers
- An identifier is a name you give to a data object
(variable or constant) or function - C is case sensitive
- Use meaningful identifiers!
10What does this function do?
int myfunction( int t, int v ) int t p t
v return p
11How about this one?
// Function calculate_pressure calculates
pressure from // inputs temperature and volume
and returns the value // of pressure. int
calculate_pressure( int temperature, int volume
) int pressure pressure temperature
volume return pressure
12Rules for Identifiers
- All elements of C follow rigid rules
- Remember, the computer is stupid!
- Identifiers must start with a letter or
underscore, which is followed by zero or more
letters, digits, or underscores - Spaces not allowed they are delimiters
- Oddball characters not allowed
13Data Types
simple
structured
address
integral enum floating
array struct class union
pointer reference
char short long int
float double long double
unsigned
14Standard Data Types for C/C
- Integral Types
- Represent integerspositive and negative values
without fractions - Declared as int, short, or long eg 4578,
-4578,0 - Floating Types
- Real numbers with a decimal point
- Declared as float or double eg 95.33, 95.0, 0.0
- Characters
- Represent single characters
- Declared as char eg B, d, ?,
15Putting Identifiers with declarations yields
- A variable is a location in memory which we refer
to by the identifier it is given we can store
data of the appropriate type here! - Declaring a variable means specifying both its
name and its data type - Examples
int ageOfDog float taxRateY2K
16What a Variable Declaration Does
- A variable declaration tells the compiler to set
aside enough memory for the item to be stored - Different variables require different amounts of
memory - For example, the float might require four bytes
and the integer two bytes
17Note on Declarations
- You can put more than one on a single line
- This makes adding comments harder, so dont abuse
this privilege
int money, monie, moonie