Instructor Diego RiveraGutierrez djrgciseufledu httpciseufledudjrg httpsufcprog2015wordpresscom Administrative Stuff New rule whenever you participate tell me your name ID: 802664
Download The PPT/PDF document "COP 3275 Computer Programming Using C" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
COP 3275 Computer Programming Using C
Instructor: Diego Rivera-Gutierrez
djrg@cise.ufl.edu
http://cise.ufl.edu/~djrg/
https://ufcprog2015.wordpress.com
/
Administrative Stuff
New rule: whenever you participate tell me your name!
Homework #1is available for submission via Canvas!
Did
you guys register for a CISE account?
If not, do so soon!
http://register.cise.ufl.edu/
You should have heard back already if you registered on Monday. Takes 2-3 business days for them to get back.
If you haven’t submitted your registration yet, I’m afraid you won’t be able to test your Homework 1 on the server
If we need to, I’ll cover a few alternatives. (Disclaimer: None of them can guarantee 100% that it would be as effective as trying it directly on the server)
Slide3Alternatives for the procrastinators
Ask one of your classmates that does have an account to help you test.
If you do so, be careful to abide by the Honor code! Please don’t cheat! I’m starting to like you.
I won’t teach anything about these, and can’t guarantee it will be the same as the server, but:
At the very least test in
CodeBlocks
(free, good IDE, available for Windows and Mac)
You could potentially use
MinGW
(Linux simulator for Windows)
Slide4Variables And types
Slide5What is a variable?
Similar to what a variable is in Algebra/Math.
A variable is “something” in my program
that stores a value
Slide6Storing Values
Remember computers work in binary:
1 or true (high voltages)
0 or false (low voltages)
Potential instruction: 10010101011100011100101110101010
Everything we store in a computer is eventually a long binary number.
Numbers
Text
Photos
Music
Videos
Slide7Units of measure in binary
Name (abbreviation)
Size
Bit (b)
1
single binary digit (0 or 1)
Byte (B)
8 bits
Kilobyte
(KB)
1024 bytes
Megabyte (MB)
1024 KBGigabyte (GB)1024 MBTerabyte (TB)1024 GBPetabyte (PB)1024 TB
Notice how elegant: everything is a power of 2!
2
0
= 1 2
3
= 8 2
10
= 1024
Slide8Memory (Ram) vs. Hard Drive
Disclamer
: I’m oversimplifying a bit of this content to make it easier to understand.
Who has ever carefully selected a computer?
By looking at the tech specs, not how shinny it looked.
What do “
memory
” and “
hard disk drives
” have in common?
Devices (parts of a computer) that store information
Units in which we measure their size
Gigabytes (GB) or Terabytes (TB)
Slide9Memory vs. Hard Drive
Usually
Memory
Hard Drive
More storage?
Faster access?
More expensive?
More relevant to today’s class?
Where do programs usually “live”?
Does not require power to keep information
During execution At any other time
Slide10So what, Diego?Why do we care?
Because “something”
A
variable is “something” in my program
that stores a value
So “something” is actually a space in memory!
A variable is
a space in memory
where
my program
will store
a value
Each variable is associated with a type that tells us how to interpret the binary number stored at that location
Slide11Defining and assigning Variables
The basic definition for a variable is going to look like this:
<type> <identifier>;
<identifier> is like a “name”. It can be almost anything!
Rules:
Starts
with an alphabetic character (a-z or A-z) or an underscore (‘_’) – That’s not an emoticon…
Can contain alphabetic characters (a-z and/or A-z), numbers (1-9) and underscores (‘_’)
No spaces, no other weird characters.
Good practices tell us it should be something meaningful.
Choosing good identifiers for our variables is important.
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Rick Osborne
Slide12Examples of Variable identifiers
Valid
sum
Flag
myFirstResult
Variable_1
adorable_penguin_1002
_this6thVariableIHaveNoIdeaWhatToName
Please notice that “being valid” and “being meaningful” are very different.
Slide13Examples of Variable identifiers
Invalid
101Dalmatians
$money
#
why_does_C_hates_hashtags
May_I_ask_you_a_question
?
my variable
Slide14What types are available
Type
Values
int
Integer
numbers (no decimals)
float
Numbers,
including those with decimals
double
A
float with more “precision”
char
A single character. ‘A’ for example. _BoolEither a 1 or a 0 (true or false)
Slide15Examples of Variable definitions
int
sum;
_
Bool
Flag;
float
myFirstResult
;
char
Variable_1;
d
ou
ble
adorable_penguin_1002;
int
_this6thVariableIHaveNoIdeaWhatToName;
Do I have to define all variables separately?
What if I want to define 3 integers quickly?
Slide16Defining multiple variables in a single line
int
sumA
,
sumB
;
_
Bool
Flag1, Flag2, Flag3;
float
myFirstResult
,
mySecondResult
;
char
Variable_1, Variable_2;
d
ou
ble
adorable_penguin_1002, adorable_penguin_1003;
Separate identifiers with commas. In this case both
sumA
And
sumB
would have type integer.
Slide17Assigning values to a variable
The basic assignment structure is:
<identifier> = <value>;
<value> could be a constant or some calculation.
For now let’s talk about constants. Examples:
Type
Examples (separated by commas)
int
0, 100, -200, -1717,
12345
float
0.100,
3.1415, -100.300f, 90.84F
double0.100L, 3.1415l, -100.02char‘a’, ‘b’, ‘#’, ‘_’, ‘\n’. ‘\’’, ‘\\’_Bool
1, 0
Slide18Examples!
int
sum;
sum = 100;
float
myFirstResult
;
myFirstResult
= 0.01f
;
Ugh! Two lines to define and assign my variable… that is so tedious.. Is there a more efficient way?
Slide19Examples
int
sum = 100;
float
myFirstResult
=
0.01f
;
char
Variable_1 = ‘D’;
/*Why ‘D’ you ask? Well… it starts my name after all… */
So much theory… Let’s test it!
Slide20Printing a Variable
We will use, surprise
surprise
printf
How? By using a special character in the string…
Each type uses a different character, we will need to learn them…
Type
Printf
characters
int
%
i
, %x, %ofloat%f, %e, %g, %adouble%f, %e, %g, %achar
%c
_
Bool
%
i
, %u
Slide21Printing a variable
#include <
stdio.h
>
int
main
(
void
)
{
int
grade
= 100;
printf
(
"I will get a %
i
in my first
C HW\n"
,
grade
);
return
0
;
}
Slide22Printing A variable
#include <
stdio.h
>
int
main
(
void
)
{
char
letter
= 100;
printf
(
"I will get a %
i
in my first C HW\n“, sum
);
return
0
;
}
Slide23Printing Two or more variables
#include <
stdio.h
>
int
main
(
void
)
{
char
letter
= 100;
printf
(
"I will get a %
i
in my first C HW\n“, sum
);
return
0
;
}
#include <
stdio.h
>
int
main
(
void
)
{
char
letter
=
'D'
;
int
grade
= 100;
printf
(
“With a %
i
in my HW I won't get a %
c in my C programming class\n
"
, grade, letter
);
return
0
;
}
Slide24Type Specifiers
Specifier
long
long
long
short
unsigned
signed
So, can I have a infinitely large number stored in a variable of type
int
?