Title: New Data Types, Constants, Include Files
1New Data Types,Constants,Include Files
Jan. 22, 2008
2Hackerz Tip of the Day
Client/Server Model
Server Computer
3Hackerz Tip of the Day
Peer-to-Peer Model
4Hackerz Tip of the Day
Server Computer
Client/Server Model
5Hackerz Tip of the Day
Peer-to-Peer Model
6Hackerz Tip of the Day
Peer-to-Peer Model
7Hackerz Tip of the Day
Peer-to-Peer Model
8Hackerz Tip of the Day
Bittorent Model
Server Computer
9QUIZ!
- Which variable type can hold a larger value, an
int or an unsigned int? - Write a statement that prints out the value of a
floating point variable, googleStockPrice,
showing 2 places past the decimal point - How do you print out a tab character in a printf
statement? - What is wrong with this statement
- printf( There are .2d days until summer\n,
daysUntilSummer )
1030-Second Survey
- Know when to use ints vs. unsigned ints?
- Know when to use floats vs. ints?
- Written a program with no compiler errors the
first time compiling? - Written a program with more than 10 compiler
errors the first time compiling? - Have difficulty understanding cryptic compiler
error messages?
11News
- Turing's Craft Website
- Work through programming problems
- Helpful messages about errors in your code
- Free trial followed by 9 fee
- http//www.tcgo1.com
- Section Access Code INDIAN-4783-0
- IU Robotics Club
- http//www.indiana.edu/roboclub/
- Meeting tonight at 7pm
- Source Code Examples
- Week 1 password spider
- Week 2 password zebra
12More New Variable Types
13Simple Addition?
int main( int argc, char argv )? int x
2147483647 int y 1 int z x y
printf( "z is d\n", z ) return( 0 )
14How Big is an Int?
- An int is a simple variable of fixed size
- Some numbers are too big to fit in an int
variable - If we try to put a value that is too big, we get
a negative value back - This is called arithmetic overflow
15An int in Memory
- How much memory does an int variable take up?
- How many int variables can I declare if I have
2GB of memory free? - Somewhat unfortunately, they are different sizes
on some machines - Most modern, normal machines will be the same
16Determining Variable Memory Usage
- Computer knows how much space an int requires
- Use the function, sizeof, to tell how many bytes
a single int variable takes up - You can use sizeof with variables or datatypes
17int Size Test
Calculate how many bytes the variable x uses
int main( int argc, char argv )? int x
0 int howBig sizeof( x ) printf( "x
uses d bytes\n", howBig ) return( 0 )
18int Size Test
Store the number of bytes in a variable called
howBig
int main( int argc, char argv )? int x
0 int howBig sizeof( x ) printf( "x
uses d bytes\n", howBig ) return( 0 )
19int Size Test
int main( int argc, char argv )? int x
0 int howBig sizeof( x ) printf( "x
uses d bytes\n", howBig ) return( 0 )
Print out howBig, which is the number of bytes
used by x
20float Size Test
Now we're working with a float
int main( int argc, char argv )? float x
10.51 int howBig sizeof( x )
printf( "x uses d bytes\n", howBig )
return( 0 )
21float Size Test
Why is howBig still an int? Is this going to work?
int main( int argc, char argv )? float x
10.51 int howBig sizeof( x )
printf( "x uses d bytes\n", howBig )
return( 0 )
22Maximum Value for int
- If an int is 4 bytes, why is the largest possible
value 2147483647 ? - 4 bytes 32 bits
- 232 4294967296
- 231 2147483648
23Variable Chart
24Constants
25Constants
- Some values never change
- digits of pi 3.1415...
- days in a week 7
- total count of honest politicians 0
- number of Spartans required to fight the
Persians 300 - We can define constants in our programs
- A little different than variables in the way they
work
26Defining a Constant
Use define to define a new constant
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
27Defining a Constant
Constant name is camel case, but starts with a
capital letter
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
28Defining a Constant
No sign
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
29Defining a Constant
Constant value can be a literal int or float, or
anything else
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
30Defining a Constant
No semicolon required. Guaranteed very weird
errors if you put a
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
31Defining a Constant
include ltstdio.hgt define DaysInWeek 7 int
main( int argc, char argv )? printf(
"There are d days in a week\n", DaysInWeek )
return( 0 )
Constant can then be used in the program in the
same way as the literal value would have been used
32System-Defined Constants
- Some constants are defined by the system
- pi, e, etc.
- Remember 2147483648?
- This is defined already in a system constant
- include ltlimits.hgt
- Constant is called INT_MAX
33Defining a Constant
The constant is defined in limits.h
include ltstdio.hgt include ltlimits.hgt int main(
int argc, char argv )? printf( "Largest
possible integer is d\n", INT_MAX )
return( 0 )
34Defining a Constant
include ltstdio.hgt include ltlimits.hgt int main(
int argc, char argv )? printf( "Largest
possible integer is d\n", INT_MAX )
return( 0 )
The constant is called INT_MAX
35Programmer Include Files
36Including External Files
- We have used include for two purposes
- stdio.h
- The printf function for printing to the screen
- limits.h
- The INT_MAX constant
- These .h files are called header files
- They contain functions and constants that are
available for our use - Don't reinvent the wheel!
37Header Files?
- Why use header files?
- Why not define all necessary constants and
functions inside our .c code? - Wouldn't this make it easier to see which
constants are defined? - How did we know the constant was called INT_MAX?
- Wouldn't this make it easier to find things?
38Header Files.
- Programs quickly become quite long
- 50 million lines of code for Windows Vista!
- Keeping things organized often becomes one of the
main problems - Separating code into different files and reusable
libraries makes it much more manageable
39Defining Personal Header Files
- We can create our own header files
- These can be included just like system header
files (with a minor syntactical difference)? - We can reuse our own header files in different
programs to save time (no copy-paste required)
and keep things organized - The longer you program and more experienced you
become, the more you will create your own header
files to reuse
40Simple Header File (myheader.h)?
define DaysInWeek 7
41Simple Header File (myheader.h)?
define DaysInWeek 7
Pretty simple, eh?
42headertest.c Using myheader.h
Use quotation marks instead of angle brackets for
header files you have defined
include ltstdio.hgt include "myheader.h" int
main( int argc, char argv )? printf(
"There are d days in a week.\n", DaysInWeek )
return( 0 )
43headertest.c Using myheader.h
Use the constant in exactly the same way as if it
were defined in this .c file
include ltstdio.hgt include "myheader.h" int
main( int argc, char argv )? printf(
"There are d days in a week.\n", DaysInWeek )
return( 0 )