Coding 101 62 (Transcript)
Net casts
you love from people you trust. This is Twit. Bandwidth for Coding 101 is
provided by Cachefly at www.cachefly.com)
Father Robert Ballecer: Today on
Coding 101 it’s a brand new module. Folks, we’re jumping into C. Welcome to a
very abrupt Coding 101. I’m Father Robert Ballecer, the Digital Jesuit and I’m
joined by the man who is the Code Warrior but as of this segment is our super
special guest co-host. Mr. Lou Maresca, Lou we’ve got to find a new title for
you because this is going to get ridiculous.
Lou Maresca: Thanks
for having me back though still.
Fr. Robert: Now we wanted to do something a little
bit different because we know that a lot of our audience really enjoyed our “Do
we know” segment so we did a module with Mark Smith from DEFCON who showed us
how to make a steam punk our do we know clock. That’s just C programming and I
talked to Lou and he said “let’s do a mini module. Let’s go ahead and give
people the basics of C so that they could do programming for our do we know or
a mega chip or any other device that may use standard C”. Lou is that about
right? Can we give that to the folks?
Lou: Yes, what we should do is start off
from the very basic beginning and try to give them just the fundamentals of
what C is all about. It’s a very legacy, it’s 1 of those things that’s
legendary in the programming industry and we want to make sure that they get
the basics as well.
Fr. Robert: Right. Now before we jump into the
module there are those who will look at a C module and say well why? I don’t
understand why, there are so many other languages that have surpassed C, that
have replaced C as the standard. Why would we want to give people C? To them
what would you tell?
Lou: You know C is alive and well in the
industry. Like you saw people are using all these different Meta devices but
they’re still allowing C programming and the reason is it is just raw speed.
Without the overhead of object oriented programming. It’s just raw speed.
Programmers who develop in C can develop very fast and efficient code and I
think it’s probably not one of those things that’s going to go away any time soon. Its standards keep getting upgraded; like for
instance there are the old standards but now there are some new standards that
are adding what we like to call syntactic sugar in the actual logic and in the
code that allows you to kind of do things today that you couldn’t do in the
original C code. So it’s still alive and well and it’s still being used by lots
and lots of people in the industry.
Fr. Robert: Yes there was actually a host here in
the studio who passed by the desk and he passed me this book and he said C is
still the best language to learn, it strips away all that sugar as you said,
strips away the framework and all the layers of abstraction and really gets you
down to knowing your concepts which is what we’re going to do. But before we do
that Lou why don’t we find out what’s going on in the world of programming.
Lou: Let’s do it.
Fr. Robert: This little story comes to us from
Science Daily. Now we’ve known for a while that in the world of computer
science especially if you’re teaching or learning it then you’re going to get a
lot of assignments that look at solutions. So we just want to know if you were
able to solve for the problem that they gave you. Now that’s relatively easy if
the program works and if it gives you the right output that’s great and if it
takes the right input that’s great. But there’s an element that’s been missing
and that’s that you can’t tell from whether or not someone has the right
solution if they have it. Now Lou we’ve talked about this. We talked about this
with Steve Gibson, we talked about this with Smitty.
This idea that there are people who can put code together but then there are people
who are true programmers, who can think of unique ways to reach a solution.
Ways that maybe haven’t been taught to them but that they conjured. That’s an
“it” factor. You look for that when working with your employees’ right?
Lou: Definitely. We look for patterns
especially in how people code for sure.
Fr. Robert: Well the researchers over at MIT say
they may have figured out a way to automate that search for “it”. Now this month’s
ACM – that’s association for computing machinery if you’re a CS major you
probably are a member of that organization. Their conference on human factors
in computing systems – well they were able to create a computing system that
they call “Over Code” that takes solutions presented by students and turns them
into templates and then puts them side by side so you can see where they
differ. Now the interesting thing about this is that they look first at
variable names, then they look at the value of
variable names. So even if you don’t use the same variable names they’ll look
at the procedure that they used to compute a solution and then the look at the
presence of sub function. In order to test this what they did was they got 24 expert programmers and they had them look at a few
1000 homework assignments. Beginning CS homework assignments and they
identified specific patterns – people who tackled the problems in different
ways. What they found was that Over Code was not only able to have all the same
patterns as those 24 experts but it was able to do it in a fraction of the
time. The creators of Over Code have said that the differences get even more
pronounced as you get into more difficult solutions, more difficult problems
and more difficult assignments. Lou let me ask you this. If you had a system at
your disposal that you could run against the code base, the code portfolio of
anyone who might be applying for a job at Microsoft and it could tell you if
they’re cookie cutter programmers or if they were programmers who showed true
imagination and initiative, would that change the way that you hire?
Lou: You know I think it all depends on the
type of question you’re asking. If you’re asking for a very kind of ad hoc
question to somebody and they provided some kind of solution that you wanted
figured out I think that would be very useful but most interviews today at
least the ones I conduct are giving some common problems that people see in the
industry. So they’ll come in and they’ll answer those questions and you can
basically tell up front if they’re following what the text book said or if they
come up with some very unique maybe even more performance solution. So if
you’ve done programming for a long time you could basically tell but I think
like you said if you do an ad hoc solution I think this would be really helpful
to tell – did they copy this from somewhere else, did they maybe do a bunch of
practicing before they came in or did they just come up with it. That would be
very useful during an interview.
Fr. Robert: Lets jump into the module but first I
want to go ahead and bring up this again. This isn’t just something that Leo
handed to me off the cuff. This is the book that he thinks is the C bible. He
said this is his 2nd copy of Kernighan and Ritchie. Wow, I actually
have this but in a different color. This is sort of the C bible. If you are
interested at all in programming in C pick up a copy of this. You can still
find this on Amazon and use it as your reference. Let’s jump in. Lou, where do
we start?
Lou: Well you held up what you need to start
with. Dennis Ritchie and Ken Thomason came up with C during the Unix operating when they were developing that and then Brian
however you pronounce his last name – Kernighan or Dennis Ritchie wrote down
that prescription language and they produced that book. That’s still today one
of the best descriptions of the language that you can find. There’s been people
writing books year over year and they just never ever get even close to the
description of C that they’ve done in that book. So it’s kind of the ultimate
bible of C. Now there are some better, newer implantations of that, of versions
of the book and some of them are out there. We’ll put them in the show notes
but there are some new standards. There’s the new ISO C99 and the C11 standards
that have come out in about the last 10 years for C that have improved the
language quite a bit and that’s not going to be in that book that you show
there. So even some of the newer editions don’t have that
stuff in there so that would be kind of the place to start. Then the
other thing that you have to do is you have to decide on what platform you’re
going to code on because C is universal and can be going across… you can
develop something in C on Linux and then be able to compile it and use the same
code on Windows but there are many different what we call compilers and we’ve
talked about this in the past where you have for instance Microsoft’s compiler
and you also have the GCC compiler or the claim compilers or the compilers that
you can use on Mac or OSX or the ones at Windows and that kind of thing. So we
wanted to go in really fast and talk about the version of the compiler that you
can use that you can actually use across all the different platforms. It’s
pretty easy to set up.
Fr. Robert: Now let’s go over some of the basics of
C. Of course it’s a compiled language so there is no
interpretations here. It’s not 1 of those hybrids, it’s pure, straight,
and simple compiled. You give it code and it’s going to give you a binary and
an executable. It’s also a strong type of language. We don’t have dynamic
variables like we would in say C Sharp. You have to declare what type of data
you’re going to be putting into your variables.
Lou: Correct. It’s basically very similar to
some of these upper level or higher level languages like C Sharp and Java but
again when you declare its type you know exactly what size it is going to be
and how much memory it needs to take at the very raw level, at the machine code
level. So for instance an integer is going to take 8 bytes and that kind of
thing. So when you declare something as an integer and it compiles down to the
machine code you know that for sure when you put a number in there it’s going
to be 8 bytes. So that’s kind of the key with C – what you have and what you
statically have on the page is statically what the machine code is going to be.
Fr. Robert: Which is actually another reason to get
one of these books because they will go over all the different variable types
and you will see exactly how much memory each one is going to take. Remember C
was developed at a time when we didn’t have the computers that we have right
now. You were actually very resource
limited and even though now you could just throw a bunch of resources at any
sloppy code that you want we know that it’s a better practice not to do that –
to program efficiently. Now let’s go to that first part which is choosing a
compiler because C does require a compiler. You can actually get a couple for
free but Lou can you recommend any?
Lou: Sure. I want to correct myself too. I
said integers and it’s not. It’s actually 4 bytes. Double is 8 bytes. So I
wanted to make sure I corrected myself. So there’s a great one that I like to
you and it’s a free one that you can use with many different IDEs today. It’s
called Cygwin and Cygwin is spelled Cygwin.com. If you go there it’s a really
cool package profile that lets you actually install and select the different
packages that you need. So I wanted to quickly show what that looked like when
you installed it. It’ll take me about 30 seconds to show it here. So if I
actually pull up my development kit here and start the setup here what you’ll
be able to see is this guy here. So the Cygwin is set up and what you’ll do is
say I’m going to install from the internet. You’re going to pick a location
that you want and just keep hitting next until it down loads it. Then the next
thing you’re going to do is choose a package. There’s going to be a whole long
list of packages that you can actually choose from. It’s very confusing. I’m
not sure why they haven’t changed this interface for a while. But the thing
that you really need most is what they call GCC or GCC Plus compiler. So what
you could do up here is you can just do a quick search of GCC and then they
break it down to these little levels here but we’re going to just choose DEVEL
with is for develop. And in the list here you’ll see that there’s the G++
compiler and you can select to install it. I have it installed already but you
want to select to install. So that’s one of the key ones. The other one is
going to be “Make”; again that allows you to actually compile or run the Make
command and so again down here you search for Make and it is underneath develop
and the GNU version and Make Utility. You’re going to click that and it’s going
to install that guy. Then lastly you’re going to want – there are 2, there’s C
Make and Make so you want actually C Make and Make. So I’m going to choose C
Make as well. Then once that’s done you’ll hit enter and it’ll install it. So
this is actually going to install these things.
Fr. Robert: Let me ask, Bryan you’ve been taking C.
I know that you’ve been taking a course right now. What compiler do they have you
using?
Bryan Burnett: We’re using Visual Studio.
Fr. Robert: Alright so we’re doing it the down and
dirty way and you’re doing it the hoity toity way.
Bryan: Yes, we’ve been using visual studio but
then also on the side I’ve been playing with X-Code just so I’m familiar with
Mac and Windows stuff.
Fr. Robert: Oh you cross platform people. No I’m
sorry. Ok Lou, I digress, get back to it.
Lou: Yes, no problem. You could use any of
these. Visual Studio is a great place to start if you want to do a really fast
set up you go to install the desk top version of the Windows Visual Studio
express and you can immediately start writing C and C++ code and it compiles easily.
The problem with that though is people say that Visual Studio isn’t going to
get you using the new compilers for like C because those compiler types change
very often so you want to basically install something – you can install
something that’s open source that’s more useful from the internet that will let
you do more cross platform development and that’s where I like to use Cygwin
for that but Visual Studio is a good option too.
Fr. Robert: Now we’ve got our compiler installed
and there are people in the chat room who are saying oh just use Note Pad and
actually I think there’s a lot of us who do. What do you
like writing in?
Lou: I think for me my favorite one here I’m
showing on my screen right now is right now I like to use Sub line text and
what you can do is you can integrate the compiler directly in… what they call
the build functionality of it and you can integrate directly into the text
editor but it gives you some good text highlighting. It also gives you a little
bit of code completion too so if you start to type it gives you kind of hints
of what I need to type down and put in there. It gives me function names and
stuff like that but there’s lots of them. There is no
plus plus, there’s them, there’s Emax,
there’s like you said just note pad or text pad. There’s really any you want to
develop in you can develop in and then you just run a command line to actually
compile it. So this is an example of a command using the C99 and ISOS spec and
I can just run this and now my code is actually compiled. So I go to the
directory that I have here for it and I can actually open up the file and I can
see the executable that’s been created here. If I explore this directory –
maybe it I spelled it right…so in the directory I see my main executable and if
I run it you’ll notice that it’s got my code running in there – Hello world and
it has got some math function that I wanted to demonstrate as well. So you can
do it on text pad, you can just use the command line to execute or you can go
directly into sub line text and I can just use this fancy control B and then
down below here it will compile and run it for me right away.
Fr. Robert: Fantastic. Ok so we’ve got that. Of
course you get to choose your own editor of choice but let’s give them the
first program because they need to be able to create something and compile it.
Show me from the start what they need, every single line.
Lou: You bet. So what’s not a program
without Hello World program right? So I think the first thing we should talk
about really fast is this thing at the top here. This thing is “include”. So basically what this is is they call it a pre-processor but what it really is doing is it’s finding this
header file that has a bunch of definitions in it and it’s finding that and
it’s just pulling it in and copying it into your code. This is called the
standard io library and what
it’s allowing you to do is to pull in all the definitions, especially io – input and output for anything, for instance consoles.
So it has what they call the print f function so I can put in here Hello World
and I can immediately get a Hello World program out just by doing that. So now
if I run - actually executable and I’ve got a Hello world at the bottom there,
finished in .3 seconds. So there’s a lot of other
functions obviously and there’s a lot of other properties and attributes in
this header file but what’s useful for us right now is just this print
definition. So 1 last line of code – 1 thing to also notate is a lot of times
when you see a console app or any type of application, especially in C, the
operating system is going to look for this main function and there’s just
several schemas or syntax for the main function but in this case the general
approach is to just use an integer return. This is an integer type or number
and usually they do that for an error. If you want to return
an error code to the operating system or something. And then you call
main and of course the parenthesis should say hey I’m not passing anything in
there right now but I could later. So I could put parameters here but for right
now I’m not passing anything in it. Then I have little curly braces for the
beginning and the end of that function and then the operating system
immediately when I run this application will call main and then whatever
functionality that’s in there in those brackets will run sequentially.
Fr. Robert: And of course we already know this from
working with C Sharp with some of the other languages and that is that main
section and it’s called different things in different languages but that really
should be as small as possible. We don’t care right now because it is a
beginner module but eventually you’re going to want all the heavy lifting done
in functions that get called by main, not done in main itself.
Lou: Correct, exactly. So the example that I
gave here is I wanted… C Sharp is another programming language where it’s
really easy to stop the console from executing and returning so you can’t
actually see what happened. So I wrote a quick function called “press enter to
continue” and what that does is it actually does a duplicate while loop until
you actually hit enter. So I called that from in here so now if I actually run
this program it will allow me – I’ll actually run it from the directory so we
can see how it works and you’ll see that right now it’ll say Hello World, press
enter to continue. So I’ve called the function that is now sitting here waiting
for input from me so I’m just going to hit enter and boom that exits the
program. So like you said you need to break the program up into functions and
into functional routines; this way it’s not as confusing and it’s not everything
inside this main function here.
Fr. Robert: Right and it just makes it cleaner
because if you keep just function calls inside of main then it means you know
what the process of your program is. If you start writing sub functions within
main it gets really, really messy and remember one of the things that we want
is we want the ability for another programmer or maybe a member of your team to
be able to pick up your code, see from your comments what’s going on and
continue with the project. You can’t really do that if you make your code…
Lou: That’s right. C code is basically just…
really all it does it is evaluates a bunch of expressions. So for you a
function is an expression and basically it builds up expression trees under the
covers and it’ll break down what it needs to do and it will sequentially go
through those and execute those expressions that you’re expressing to the
computer. So that’s really all that a function is. In this case I’m expressing
say, go and call this and as you’re calling it wait for the user to do
something and then when they do it then return back. So that’s really the
expression I’m trying to tell the PC to do and that’s what it is doing for me.
Fr. Robert: Right. If we wanted to add a layer of
complexity on that; let’s say we wanted to send the folks home with hello world
plus what would you suggest they do?
Lou: So I think one of the biggest things is
to understand is to break up the idea of variables, to really understand what
variables do in your application. We talked about this in some of the other
modules but it’s fairly the same thing in C. You want to declare a variable
which is just a place holder in memory for a value. You have things like
integers and characters or char as they call it in C, doubles which is also a double precision decimal, doubles is a number with a
decimal place. So there are different ways of learning, learn how to declare
variables, learn how to store variables, learn how to store data in those
variables. Print it out to the screen, that’s really how the beginning program
is – just print stuff out. In this case I have a math function that I’m doing
where I’m showing the normal distribution between a number here. So basically I’m pulling in what they call the pre-processor directive at
the top called math and it has a bunch of math functionality in there and I can
use that to do things like evaluate an expression or do the square root of
something. Another thing you can see here is I’m using a different variant of
the print function. This print function allows me to use this formatter. We did
this in C Sharp where you out a little character in there like curly brackets,
0 in C Sharp’s case but this is what they call just a
formatter expression. You put %g and you’re saying I want to put a number or
double in that location and the actual number that I want to put is this thing
over here. So it makes it really easy to kind of print stuff to the screen. So
try that now. Create some variables, use the print function, print it out to
the screen, see how it works, learn to compile things, and learn to get the
compiler errors. Like for instance if I were to take this curly bracket out at
the top it’s going to say hey, wait a second, you don’t have a terminating
character. You haven’t finished this character string out for me yet.
Fr. Robert: Actually we should probably talk a
little about syntax there because the missing curly bracket is probably the
bane of every C programmer because at some point you’re going to miss one and
it won’t give you the error or what you think the error should be for that
missing curly bracket. So you just have to start to recognize that. We’ve also
got Dallas in the chatroom who is saying ok there’s a
few things. Again remember C is very strongly typed, you need to use the right
variables for the type of data that you’re pulling in otherwise you will get…
it won’t work or you’ll get really, really, wrong output and everything is case
sensitive so just know that. Now let’s talk about syntax. Let’s just talk about
main for a second here. So of course we’ve got the declaration of main, why do
you have int in front of main?
Lou: So again main is the function that
operating systems will look for and in this case the normal what they call
schema for that function is that it will return an error code of some type so
in this case most of the time people return 0 if things are successful so I’ll
put return in my function – return 0 meaning everything was great, worked
great, no problems so the operating system says fantastic, this application runs
successfully or you can return some kind of error code like a negative 1 or
some other number and then that user says ok well it looks like I got an error
code and then maybe the application defines in their help file saying error
code number you know 400 means I didn’t connect correctly right. So the idea
here is it’s supposed to return some kind of a code so you understand if the
application ran successfully or not.
Fr. Robert: Right. Actually first we’ve got the
curly brackets and I don’t know about you but when I was programming C whenever
I would put in 1 curly bracket I would put in its opposite just to make sure
that they would both be in there. Is that still a convention or am I just old.
Lou: No that’s exactly right and a lot of
these text note pad editors… a lot of these editors today that allow you to
write C code and C ++ code in them they will automatically put them in for you.
So for example Sub Line then will now… some of these text editors will actually
put it in there so you don’t forget.
Fr. Robert: Right. Next line down you actually put
in a comment which is good because there are 2 different ways to comment in C
and you’ve used 1 of them which comments out a particular line but can you show
that? There we go, that’s the other one and that comments out an entire
section. So it will comment out everything between the slash and the… what do
they call that – the asterisk.
Lou: Right so that’s what they call multi
line, multi break down of a comment and some people like to put asterisks so
you know that this whole thing is like a comment so you can put asterisks all
over the place. Like I said single line comment makes it useful. This is again
all modern languages have these comment things – actually multi line.
Fr. Robert: Right and next line down… remember
we’re aiming this at people who want to get in on C at the very beginning, the
basic level, int number creates a variable, an integer variable called number
and you’re assigning it the value “foo” which is interesting.
Lou: That’s right. Why I wanted to do that
is because I wanted to show them what happens when you do something silly like
that. So you know that foo is not a number. In this case it’s telling you wait
a second this is probably not right. You’re initializing an integer with a pointer
and the pointer means a string variable so if I put a character – let’s not do
a string, that’s a little bit confusing for this point. Let’s just do just a
character. So now what it’s going to do is it’s actually going to do compile
because what I’m doing is if I actually output this number it’s going to say
some garbage character. So if I actually put a number in here now. This is
syntactically set number to an actual number. So if I do something else like
for instance put an actual decimal point in…
Fr. Robert: Which is not an
integer.
Lou: That’s right. So what this will do is
it’ll actually round this number because now what you’ve done is you’ve put a
decimal value or double precision pointed decimal in there and it’s saying well
integers are whole numbers so they are real numbers. You can’t actually put
this. So if I were to actually output this to the screen you would actually see
that this is going to come out as 2. I have to actually put in this little guy
here in this formatter thing. There we go. Next to it I should actually put a
new line thing here so we can see it better. It actually rounded it out because
it’s not actually a decimal and if I change it now to a decimal or double… g,
oops, so we’re going to use a different notation rather than…called g and now
we get 2.2. Do it very careful sometimes because the compiler’s not going to
know what you meant. Sometimes the compiler says ok you put a number in there
but you… in this case it’s not going to actually tell you that the number that
you put in there has a decimal and the static data type that you’re using
doesn’t allow that. So there’s some rules that you have to go and look up so
that’s why I say play with the data types, play with variables so you
understand some of the rules that it goes through that the compiler is not
going to find.
Fr. Robert: Right and actually Bryan your earlier
assignments it seemed like your professor was always trying to mess you up with
data types. That was like the purpose of some of the earlier homework assignments
that you showed me which is they wanted you to use the wrong type so that you
would get strange output, not necessarily an error but just not what you
expected.
Bryan: Yes that was definitely one of the
first things that we played with. But Lou I have a quick question. Why do we
have the percent I? I know the percent gives it the extra that ends the line
but what’s the percent I in that print f function?
Lou: That’s a really good question. So what
the percent I does is it allows me to print out the type
so I can just pass in the type over here and I can print out whatever’s inside
that variable. In this case it is an integer so the percent I will then allow
me to print that variable. So if I want to put a whole bunch of stuff in here
the number is percent I, and then what will happen at run time is that whatever
is the variable here, in this case 2, will be replaced inside of there. So if I
run this… I keep forgetting to put my new line in here. This is called a new
line formatter. There we go. So now we see the number is 2 so I can put a whole
long string, a character array there, a long string in here of whatever I want
and if I wanted to put variables like for instance number, I can replace that
with the variable number.
Bryan: Ok so if you’re to put a second
variable in there like if you did a double and then had that in the line too so
then you would just put that character name in there?
Lou: You could put a number in there and
another one here also percent I and we take the new line out from here and I
put the new number over here. You can pass in as many as you want so I’m going
to run this now you have 2.
Bryan: Ok and it just will grab whatever the
next one is.
Lou: Right. It’s a little bit more
declarative in C Sharp. For instance in C Sharp you would put 0, 1 like this,
and then like you would know this is the first one and the second one and
that’s what will replace these but with C it is not as declarative, it just
says we’ll just put them in the order that you put them in there.
Bryan: Ok so if you flipped it and you put
number 2 before number then it would grab the 4 before the 2 I guess.
Lou: You’re exactly right. So it’s a little confusing
but you get used to it.
Bryan: Ok, I wasn’t familiar with the
percentage and then declaring the integer.
Lou: Right, another thing to note is that
there are different formatters. So if I wanted to put again my decimal in here
and my double number, if I wanted to put this in here I actually have to use a
different formatter – this guy here and then I have to put another number and
compile this. Now it replaced it but if I were to actually put this as an “I” it would yell at me.
Bryan: Ok are you just picking the g because
you like the character g or is that something specific?
Lou: No these are just different formatter
types. If you go look up in all the help documentation for print f in the
standard input output library you’ll notice that g is for doubles and I is for integers and so you have to use the correct
formatter. You’ll notice here I put a bunch of junk here and it’s probably a
memory location of some type so it doesn’t really know what I’m trying to do.
In this case I’ll put g and then it’ll come out correctly.
Fr. Robert: Whoa, hold on here. Bryan, are you
having Lou do your homework?
Bryan: Not quite yet. If we
were to do classes… that’s about where we’re at in classes.
Fr. Robert: Maybe the next module. Ok you got it.
Actually Lou if we go back to your screen there another quick point – the semi
colon. Every line ends with a semi colon. This is other than the curly quotes –
curly brackets this is probably the 2nd most made mistake in C.
Lou: Right, make sure you always end… a lot
of things a lot of people don’t know is you don’t
necessarily need to finish your functions with a semi colon but definitely
finish any type of variable definition or when you assign something to a
variable and that kind of thing you want to finish it off. A lot of times when
you’re doing like “do while loops” or loops which we’ll get into later on, you
want to end them with semi colons. This way it’s kind of a terminator for that
expression, I like to call it.
Fr. Robert: Right. As long as we’re in the print f
line one of the things that people get confused with is – I see this a lot in beginning programmers; when they try to put
special characters into the string of characters that they’re trying to print
and it doesn’t work. For example, if you try to print quotation marks just by
putting a quotation mark in there it’s going to kill it. You actually have to
use a special character in order to make that print properly.
Lou: Right, exactly right, it might confuse
what you’re trying to do. So if I keep putting single quotation marks it will
actually output… it is working. Print f is actually what they call literal so
that means anything that you put in there is literal unless you use these
special formatters. So for instance if I want to do a new line or a tab I could
put these special formatters in there and you notice it puts a tab in there but
anything else I put in here is going to be the exact characters that I put in
there.
Fr. Robert: Right. Let’s go down to the function
column. So you created a function here called “press enter to continue” and
that’s how you call it. So it’s press enter to continue, it’s not passing you
any parameters but you could. You’ve got the little brackets there so that you
can do it. At that point in the main the compilers going to get to that, it’s
going to leave main, go up to that function and run the function. Now we get a
question here every once in a while. At least I used to and that is people were
wondering “can I put the functions below main, can I have main at the top and
that doesn’t always work out well right Lou?
Lou: Correct. We can actually do that to see
what happens. So what happens is the compiler is going to say wait a second. I
have no idea what this is”. So it’s going to say here some previous decoration
and it’s going to try to run it and it’s going to actually give you a warning.
It’s going to say no you should probably move this because it’s conflicting
stuff. So I’m going to move this. Let’s run it again.
Fr. Robert: Just remember that the compiler is
looking from top to bottom and from left to right.
Lou: That’s right. So in this case it’s
actually being smarter than we thought because it’s going to say I found the
definition for it and the compiler says we can run that but it’s warning me
saying that there is something wrong here. It’s conflicting and there’s
something wrong so I’m going to move it out of the way and move it back up
here. Now if I run it I don’t get any more warnings.
Fr. Robert: Right. That compiler was being smart so
you’ve got a more advanced compiler that said let me look through the entire
program before I stop. But the convention is to declare it before you actually
use it.
Lou: Right and I was using the C89 compiler yesterday for all my examples and it will literally yell
at you saying this is not defined yet. You need to find that definition for it.
Fr. Robert: Now let’s go up to the function. So
you’ve declared this function at the top and you pressed enter to continue and
inside of it you’ve got a couple of interesting features so of course we’ve got
our brackets, make sure those are in there. In C, so you’re declaring an
integer, a variable called C that can hold an integer value, you’ve got a print f which is a standard print out the screen. F flush, we
haven’t seen that before.
Lou: So this is a STI – standard input,
output variable. It’s basically just flushing whatever the contents is in the
standard output right now; saying anything you’ve entered before, if you’ve hit
an enter or escape key or anything like that just flush it out, just don’t use
it right now because we want to basically get the next character that the user’s
going to enter. So this is again – look up the definition for this so you can
kind of understand a little bit better but it’s just getting the console ready
for me to capture something from the user.
Fr. Robert: Right and that’s also just because
whenever we declare a variable if you don’t assign it a value it could actually
be anything. In C remember when you declare a space you, you set aside space
that space could actually be filled up with random junk and you need to do
something to make sure that junk isn’t interfering with your input or output.
Lou: Right:
Fr. Robert: Let’s go to that next line. You’ve got
a “do”. Now this is interesting, we haven’t had do before.
Lou: This is what they call a “do while
loop” and what it is saying is “do this, do get the latest character, while I
determine if it’s actually 1 of these things. If it is a new
line or if it is a function at all. Every time you put it in a character
if I were to run this and from the actual command line here; so if I actually
run this guy, if I keep entering characters in here do you notice it is not
going to do anything until it actually hits that character like a new line or
an enter. So if I enter violate it determines oh wait it is exactly the
characters that I have in here. So these are called conditional statements and
we’ll get into that a little bit later but basically it is saying these are
like different way to say if it’s not equal to a new line and as well as then
it’s not equal to the end of function or end of file.
Fr. Robert: Right. You could also do this with a
loop and “if then” statements but this is more elegant.
Lou: Right. There’s another way of doing it
where they call a while loop and there’s another one called a 4 loop. The 4
loop is just a condensed version of the while loop. So I like to use the higher
level ways of doing these up front so you can kind of understand the convention
it needs to cover.
Fr. Robert: Now you’ve got some conditional operators
here. This is important because any time we want to do any decision making
statements these will save you a lot of time or they just make it possible for
you to have a solution. Explain to me exactly what conditional operators you’ve
got there and what they’re doing.
Lou: Sure, for instance there are
conditional operators meaning if I were to put 2 equals here it is saying that
while if the C code – if the c, the very little character that we’re getting
actually equals a new line then do something. In this case I want to say it is
not equal to meaning it’s not actually ever going to be that. In this case it
will wait until it’s actually that character. And then we have another one over
here called double and. It is just saying don’t just wait for the new line but
also wait for the potential as well as – and or as well as, the end of the
file. So it’s saying that in order for this to be true it’s either this and
this and there are other operators called “Ors” if I wanted to put an “or”
operator in here where it can say is it the new line or it could be the end of
file. So there’s lots of different ways to actually do this. It’s what they
call – logic – to basically determine what you actually want to determine for
that function. So in this case we’re using “not” equals 2 but again you can use
“not” equals 2 or greater than equals 2, you can use greater than or lesser
than, there are lots of different expressions here that you can put in here.
Again I would try them out to see how they work and determine what their use
it.
Fr. Robert: iBooker in
the chat room is saying “So either argument satisfied the do while when you
paired them up with an “and and no”. Actually it’s the other way around. So if
we were to put an “or” there it means that either, or would satisfy that
statement. In this case only both of them.
Lou: Right, exactly.
Fr. Robert: So both of them have to be true. Ok
wait now hold on, now see you’re messing me up. Both
of them have to be true in order for the while loop to stop.
Lou: Correct.
Fr. Robert: There…I hated this part. Ok so that’s a
pretty good run down and again you’re going to have all that code. Those assets
will be available to you so you don’t have to copy it off of the screen. You
will have proper code that you can use inside your compiler but as Lou said,
especially when you’re getting started, no matter what compiler you choose
start making errors because you want to be able to interpret those error
messages. One of the worst things that happens in C is
when your program gets really long and you have an error and you have no idea
how to interpret it. You can’t tell if it’s a missing semicolon, is it a
missing bracket, did I use the wrong type. And also I think maybe the errors
that bug me more than those errant semi colons are when I have output that just
isn’t right but it doesn’t give me an error. Technically the program will run, it’s just not running the way I want it to. Lou, what else
do they need to know before they move on to homework tonight?
Lou: I think that this is probably enough.
We can get into complex stuff like pointers and arithmetic and arrays and all
that stuff but again this is a good start. Make sure you try this out and get
your variables in there. Use the standard output, print stuff through the screen.
Maybe even capture stuff through the screen. Try to actually capture stuff. In
my case I gave an example where I’m capturing the next character of the screen
for this case looking for enter. So again do some
stuff like that and try it out, maybe capture some text from the screen and do
something with it. Maybe print out Hello world with somebodies name or
something like that or build your own calculator. That’s another option. We showed
you and gave you an option to do a function and you can pass in some numbers
maybe try to build a function similar to this press enter but make it so you
can pass some numbers to it. So I guess just build that out, just try those
things out and then we’ll move on to some more complex stuff later.
Fr. Robert: Bryan do you think maybe that we could
make a calculator with multiple functions that handled each type of a
mathematical operation? Do you think maybe you could do that?
Bryan: Yes I think. Could we have it ready
before next Thursday because I have a due date for that actually?
Fr. Robert: I make an agreement with Bryan. I said
hey you know if you want to start making your homework the actual homework
assignments we could do that. We could work out some sort of deal here. I don’t
know if Leo will be up for it. Lou Maresca, our Code Warrior and also our super
special guest co-host. We’re going to have to work on that. Thank
you so very much for being with us and thank you for breaking down this
knowledge. If I were to have it all to do again I probably would have
made C the very first module we ever did because it is so fundamental, it is so
basic and it’s really easy for people to get into. We’re going to make sure
that they have the resources that you pointed out. All the websites they can
download the different programs that you made available but can you please tell
the folks where they can find you and your work?
Lou: Definitely. I’m @LouMM on Twitter –
LouMM and of course all my work during my daily job is at crm.dynamics.com.
Fr. Robert: Lou Maresca,
our code warrior once again Sir, we salute you. Now folks that’s it for this
episode of Coding 101 but don’t worry because we’ve got plenty more coming. We
do this show live every Monday at 2:30 PM Pacific time. Just go to
live.twit.tv. If you drop in early you can see the pre-show and then you can
stay late and watch the post show and pretty much everything that goes in
between. You can see all the little bumbles and foibles that we have to take
out of the finished product. Also don’t forget if you’re going to watch live
you might as well jump into the chat room. You’ll see me every once in a while
looking up here, right about there. That’s you, if you’re in the chat room I
can see what you’re typing. If you’ve got questions I can ask our code warrior
or our co-host. If you’ve got comments, if you’ve got something that you just
think we need to know it’s a really good way to be part of the Twit TV
experience. Also don’t forget that you can follow me on Twitter. Just go to
twitter.com/padresj. That’s @padresj. If you follow me there you’ll find out
what we’re going to be doing on every episode of Coding 101. You’ll see what
we’ve got for future modules. You’ll see who we’ve got for guests and co-hosts
and you’ll also find out when I’m getting steamed buns for Bryan because he
loves him some steamed buns which it’s probably not good for him. I think that
thing has like 5000 grams of sugar but that’s whatever. Sorry.
Bryan: You didn’t tell me that!
Fr. Robert: Sorry about that, you may need some
insulin.
Bryan: I was blissfully unaware. They are so
good though.
Fr. Robert: They are really good. Don’t forget that
you can find all of our episodes at Twit.tv/code or Coding 101, it all goes to the same place. Not only will you find out back episodes along
with links to Git Hubs and code for the assets but you’ll also find a place
where you can download every episode automatically to your device of choice. If
you want the audio version in your iPhone, maybe the video version in your
iPad, perhaps you want the high definition version in your laptop, your desktop
Mac or PC. We’ve got it all for you because we love you. Finally I want to
thank everyone who makes this show possible; to Lisa and Leo for letting me
take up this space. Of course to my fantastic TED Mr. Bryan
Cranky Hippo Burnett. Bryan can you tell the folks where they can find
you on the Twit TV network?
Bryan: Well they can find me on Thursday doing
Know How with you. We always have some new project we’re working on so check
that out. Then Fridays we do BYB. Occasionally I do reviews depending on how
much time I have in the week. We’ve got some other cool stuff coming up but I
don’t think I can talk about that.
Fr. Robert: No we can’t actually it is embargoed.
Bryan: But that’ll be cool so stick around and
watch and check out that stuff and you can find me on Twitter @Cranky_Hippo.
Fr. Robert: Until next time, I’m Father Robert
Ballecer the digital Jesuit. This has been Coding 101. End of line!