Updates: Tyler on PowerHacking CSS to cutestrap your app in this latest episode!

Topic 1 - Basic Data Types and the First Function Signature


  • int, char, long, short. Thinking about bits.
  • declaring vars up top, code below
  • Includes aren’t Imports (static preprocessor), stdio
  • First look at ASCII, putchar and printf
  • strings as an array of chars and the null byte
  • the three part for loop


hello everybody
we are rocking sorry got a little
excited there i'm so excited i'm so
that she's excited and this is going to
be a little bit different than the
stream that you may have seen earlier
today i know
opti you were here um and hello how are
you opti
um so we're uh
we have a special guest tonight uh who
is on the line and she is
a astrophysicist that extraordinaire
that i caught one day on twitch um
rocking some astrophysics
and some python and some
pikachus i think that night but it was
uh kind of
kind of an interesting night that i
walked into but uh and i was i was in a
troll mood but uh
she said hey you know i'd i'd like to
learn some c i've been doing enough of
this python and i said
hey let's learn some c let's get going
she's she's coming from a programming uh
mostly in python for data stuff and
we're going to start a new series
in uh also in the monday learn sessions
c programmers that are learning c as an
additional language
and with that said i'd like to introduce
you all
and reintroduce you for those who
already know her to jess
the enceladasaurus enceladus source
which i'm totally gonna get right
someday the enceladusaurus so hello jess
are you there
yep hey everyone all right sweet um
we are totally going with uh one video
camera tonight due to
i'm having enough technical issues over
here but we are going to uh
maybe add video later for her as well so
i don't know if you all saw the screen
um with the uh with the intro but
i was very proud that i rolled both of
these 19s
uh while we were just setting the stream
yeah i feel very good about those 19s i
think that
that jess is just good luck
i'll take it that's that's a hell of a
role so
that's great we're going to be using uh
the same tool that we were using
before where i have this
so this is um this is the same tool that
you saw earlier today that was uh
running with tn for the python from
scratch course but
we're done with python we are moving out
of interpreted languages we are
compiling things we are going to start
in c and in c you don't get anything for
literally like every thing that you want
to do on the computer
you know what you're paying for on every
line like it's it's designed
this is like a very old language at this
uh compared to a bunch of the other
stuff it does none of the memory
management for you
and it is your responsibility everything
becomes your responsibility i told jess
i don't know why you want to learn this
but you know
oh boy i am not my sister's keeper so
she wants to learn it she gets to learn
it alright so
jess have you ever written any c before
i have i i guess i think the the arduino
programming language might be similar
it's very c like yes yeah so i think
that's the closest i've come
but proper c absolutely no
prophecy i don't even know if there is a
proper c
let's just say c lots of c okay
um so to uh
just because uh opti's here and opti
knows the sort of
issues i have with the whiteboard i am
going to
use the whiteboard because i went and
crammed code up this
feature and uh to get this whiteboard on
the screen and then the first session
that it was available
i didn't use it at all your whiteboard
is really cool
whiteboard is real whiteboard is cool
you know that you know it's funny that
you say that
because it is black but it's not it's
really white okay so the reason i say
it's a white board and
nightshade dude by the way who happens
to be here hello nightshade dude
is always upset every time i refer to
this thing as a whiteboard but just
it's it's an old thing for me it i even
have white board glove
glove on um so
what is that is that so you don't smudge
yeah it's it's
it's not really smudge and like so that
your hand doesn't detect and like you
get a big squash
plus it's like really hard to write if
you like hold it
like far enough away not i always put my
hand on the paper so
so we have the glove and i call it a
white board mainly just because
it's like it doesn't screech like chalk
it's got a pen marker thing and you know
we've got
we we totally have like different colors
that we can draw with
and you know we we can we can just break
out into trees
and you know whatever
and so that's not at all what we're
going to be writing because
we're serious c programmers and you know
we're we're done with this sort of
python world we've had enough of the
memory managing memory for us so there
there's no more of this memory
i want to do this memory management
so i'm going to tell you i'm going to be
explicit about
everything that i allocate
and this is kind of a big deal because
we know that yeah there are no more
training wheels
you are definitely done with that oh and
by the way chat are you getting her uh
typing sounds is that
coming through as well because i'm i'm
definitely curious
um so we're gonna be explicit it's not
too loud
uh i hopefully it is loud you should let
your keyboard scream
be proud okay
um well you know just fill in details as
we kind of go along
so we're going to declare things so
this and by the way i really shouldn't
pick such a difficult to see color we're
because declaring is about declaring
things so we're gonna we're gonna
the things and this is really important
because in c
basically the idea is that you kind of
get nothing for free so
um unlike python which has like a global
scope where you can just sort of like
write code whatever you feel like it
c requires that absolutely everything is
in a function
and it this dates back to the time when
like you know there were language
debates about whether something was a
if it didn't return a value because it
came from math language right so to be a
function you have to return
something like you can't be like f of x
nothing i mean even that is returning
something so
that that that was kind of a problem and
so of course c sort of standardized that
as the null which
you know you're going to get some issues
with later
because most constants get written in c
as capital just like in python
so null is definitely null and
um this uh so
we do need to define everything that's
in a function so what do you do if
something's not in a function well you
start with main
main is your starting place so the
the linker is going to go looking for
main as the first place to invoke your
and it's going to have a few scary
things because
in python you would have defined it
something like this right
yeah all right so that's cool because
you know you could return something
or you could just not return anything
right you could just pass
on that um
so that's that's a very like pythonic
way of doing things and we're not we're
not doing python in here we're
we're doing c and that means that we
have to say exactly what we're paying
at every moment so if we're going to
define a function as main
we need to specify on the left what it
every function must return something and
on the right
and by the way i'm teaching you proper c
as it were i thought there was no
let's let's say like as proper as we can
so so we're not having any of these
functions that don't return things okay
that's that's that's
old c we don't we don't do that anymore
um got it we're using the something a
little bit after the c89 standard
so we'll don't worry about that too much
but we're gonna
we're gonna set the compiler to be like
really complain mode
it has pedantic mode okay so um
so we need to specify a return value
and we need to specify actually let me
we're already getting a little bit crazy
here so in order to have a function so
we have we have whatever the function
name is right
and you know i'm just going to blow this
up for
the people not
there now of course i threw away my
yeah okay um so
we're gonna and just because things are
gonna be
sort of hard for jesse to read
i'm going to try to write a little bit
bigger so
um we start off with our function name
that's great so we're defining this
thing and then
we have to specify over here a return
type now this is python this is not
python so you actually have to say
what type everything is you can't be
changing it like middle of the program
yes this deserves a monocle by the way i
just saw that
so right it's not dynamically tight
right it is not dynamically typed or
duct typed or
whatever oh i totally lost it but i had
a single use monocle here and i feel
really bad
that i don't have it here because it is
just a fantastic thing
okay so awesome yes so
we have this name and then what we do is
we have just like in
python we specify this you know the
parameter list
or the argument list it's called and see
i don't really know why that started but
anyway they call it
arguments i'll call them params a lot
but you know
same idea okay and again everything
that is there really needs a you know it
a name and it needs a type okay so
python we can just sort of like sit
there and be like yeah okay we're gonna
have x and we're gonna assign it like
five and then
later on we're sort of doing some magic
and then we're just like okay you know
i like x to be a string and that's cool
in python but that is totally not
allowed in c so you know if you
you choose you make that choice that's
so c sounds like kind of like like like
the old man
who's like get off my lawn a little bit
you know like no you can't that's not
how we do things
well like one of those things it's like
you're going to realize is that python
quite a bit later so they sort of said
like hey well what if he wanted to do
that and like see he's like no
like see so been around forever so
so with what you're saying here like
with the return type and everything um
like i totally don't know what i'm doing
oh yeah kind of be
like you could kind of go like oh i
don't know if you need after or what but
like is that kind of
like i'm specifying that it would return
an integer let me uh let me get to that
before we hit the code um i just like
like emulating because sometimes that
helps it again oh yeah sure i mean well
the problem is that we don't
all right yeah we do need to start with
what those types are um but the thing is
let me let me just say real fast that
basically so we have our argument list
and we have our
return and we have to make sure oh and
we get extra lines too and we have our
return and we have to make sure
that everything needs a type and a name
and the only exception to that is in
the return type which doesn't it's just
a type
so when you're specifying a function
think like because they're they're
coming from the world of math
you're saying like okay this is my
this is my function name and this is
going to be my return type you know so
you know it in math it's always a number
right but in c it could be whatever so
we'll start with your your type that you
kind of got going with which is going to
be kind of important because
it happens to be what maine needs um so
we have ins
we have cares um and
we're gonna do i i let's not do
too much more than that we're gonna
we're gonna add on like some decorators
to that in a little bit
but um so starting there
we need to have the standard invocation
for maine
which is formulaic and this you just
have to memorize because
don't write it any other way ever
all right and this is just going to be
in is the return type
on main and this thing takes two
it takes an arg c and it takes an arg v
i'm not talking about windows here we're
talking about posix
standard c
that's a bad p
okay so this is like kind of like the
unit the
unix world you know and then by the way
just convention oh by the way you always
need braces
so any place where a brace is optional i
don't want to see optional braces i
always want to see braces
and that is basically our invocation
so what are we invoking
oh sorry uh this is the definition of
the thing we're going to invoke so
the system always gets you started by
calling your main routine
and it always calls it with this
okay so let me cut over to uh
our learning screen okay so yeah
just to get you started this kind of
looks sort of like this
okay that's it in cleaned up non-guy
handwriting so so that has to be at the
start of everything
yeah okay it usually it's funny that you
say the start because it's usually at
the bottom of wherever the main
routine is but so is this
so so let's say that you have um
like a couple of a couple of scripts
that you've written and then some are
never going to be
run like they're never going to be they
don't have a main
yes so this is this is just like the the
thing that you do at python which is
like if something
equal do this other yeah okay i know
thank you the name thing yeah that okay
it is like that in that it's required
that's that's where your program will
um python overloads it because it says
like okay like if this thing's here like
run the program if you're running it
then then run this routine
and if you're importing it don't run
with this routine okay c doesn't have
any of that
c is just like it sees main it's calling
main you don't have main it throws an
i see okay so this is like you know this
is your
formulaic invocation to the compiler
i am ready to write let's see so
okay um now just a few like quick things
we're going to need like to get going is
this is a c plus style comment which is
totally allowed and it just means like
like this line is out yeah yeah just
hashtags in python okay and this is your
multi-line comment so
i can do that and say hey
more comments stuff yep okay and in
there's this will not be run um there's
already a bug in this program which is
that and this is why i always write this
we said that this thing returns an
okay and i always forget by the way that
you can't see the selections i'm
selecting it but
um line four i need i need to add that
so i can see on the i've got the the
stream up so oh okay cool selecting yep
um yeah basically that return type says
that we have to return something
um so make sure that you return
don't get in the habit of like maine
actually is like
allowed to slide because willy nilly
so main is a special function so if you
don't return then it just returns zero
and but okay we wanna
just just get in the habit of like
always having that so right here this is
minimal c program that you're always
going to write like every time you want
to write an sc program
doesn't that just though so if
if you're defining this function main
and that's telling
you know i guess your compiler or
whatever that you are writing
you're writing c i mean isn't nick just
going to tell this program then to
return zero
regardless of what you write that's
correct it's always going to run that
um now why do we want to return 0 that
actually gets into an operating system
this return type which is coming back
from main
is the exit code of your program
and that's a very unixy kind of thing
which by the way every other operating
system stole it so
um but this this this int that we're
returning on main
that actually is your exit code and that
basically is you telling the operating
like this is how i ended up and
basically it can either be
zero which means everything is fine
everything's fine here just move along
that could be a number and any number it
is that's not zero
means that it was an error and like
somehow your program died
okay so that's your way of telling the
operating system kind of what's going on
with status now
some programs are non-standard and they
don't you know follow the convention but
most of those programs are gone from
unix lore so
just zero means everything's fine and if
you wanted to exit
like early like say like if something
happened then
which we always do braces then we would
we could say like
return negative one okay so there's kind
of like a standard
like okay that means something bad
happened there's actually definitions
for what these things are
um but we're not going to worry about
that that's totally advanced and we're
we'll get there later for now just throw
anything you want i mean i have a habit
of always returning
two or three because i never want to see
i don't know i i had a thing about twos
and threes when i learned how to program
this is just good like like you know
error handling or ways to kind of
oh that's not handling there's no hammer
you pay for everything in c it doesn't
handle anything like if something goes
wrong it'll just crash all over the
so this helps you know yeah this gives
you a hint like okay
oh god that program didn't return
anything what am i getting myself into
yeah it's it's gonna be hard coming from
a python mindset where python does
everything i mean you know python has
import anti-gravity right
which is awesome by the way that that
works i don't know if you've tried that
in python i tried what
there really is import anti-gravity
really yeah that
i'm not even kidding that's that's a
real thing um
okay uh it's a randall monroe joke if
never done xkcd stuff but um oh yeah
uh so this is just standard call
um and by the way thank you people for
the follows i'm sort of like half out of
it on chat we're just getting
oriented here but uh goku and loaf bone
thank you
um okay so yes there is abort uh korean
american bbq you are jumping ahead
um delta t this program is our bare
c program this is just getting started
with our standard call signature this is
uh the first time running through c for
uh the end
seledosaurus yes yes i totally said you
got it
okay all right so oh what is the
tutoring program this is uh something
that i wrote myself
um basically kind of starting with
and celadosaurus because you know i was
saying hey i'll teach you c and then i
was like well we need a tool to learn
so oh my god you built this that fast
yeah yeah actually
like a week ago i know this if you watch
my next stream the day after we had that
conversation you would see me building
that's why there's a bunch of missing
features like oh wow
i made a plot it took me two days now um
yeah i mean that's
me i start doing javascript that it's
like days
so anyway um no i have not pushed it to
github korean american barbecue where
okay wait okay just just just wait for
everyone sees this thing they're like
hey i want that all right sorry because
it's awesome getting excited that you
built this that fast
getting sidetracked getting sidetracked
you you got to write c so
why don't you run our first c program
here okay
hello akshay how are you and what do we
get for output
i see nothing absolutely nothing because
and this is your first lesson at sea
the return code goes to the operating
system not to you
i see okay that's great all right
so xero is going to the operating system
the operating system's like yeah that's
i got it nothing went wrong with that
program and you will not know that which
is very annoying so
let me return the question stupid
question how does it know that zero
is is okay like why does it think that
that's the acceptable like that's
everything's great kernighan thing
so kernighan and richie like so like
way back what what what i'm referring to
very briefly is what we're going to call
k and r
um kernighan and richie are sort of like
the language creators for c
and yes there was an a that was a b and
they had problems
so c made it and
and kernighan being the sly dog he was
you know was building the unix operating
system which was another spoof
on you know because it's unix right
which was a play on unix
which was a play on multix which was the
operating system of the time because it
could run multiple programs at the same
and so this was the the one program at a
the unix so anyway
he if you get to invent the language you
get to choose the convention so back
then it was a lot it's convention
yeah all right um you can throw
something different back to the
operating system and the operating
system just won't care
i mean it's just if you looked in my in
the messages file of the
of the server that's hosting this right
now you would see that it'd be like oh
something might have gone wrong because
the program didn't return zero and
that's all it does
okay so wow not very useful it is useful
in shell scripts but to us today it's
really useless so let's not count on the
return code other than
zero if it's fine and not zero if it's
not fine
okay and that's a little bit weird and
that's important because a bunch of the
libraries that we're gonna use
follow the same convention you would
think because if you chose zero you'd be
like oh yeah
zero is fine so then i say if
zero gonna do the branch or not
this is a little confusing but anyway um
basically there's all sorts of things in
the standard libraries that return
zero when they work okay
and the problem is this would cast to
if there was false in the language but
um so
this is it's horribly inconsistent
so we're not going to worry about that
though what we are going to worry about
is how do you get some output to start
showing up
okay so you might have heard of this
function before yeah yeah okay so i've
used that with arduino
okay cool um i think they have a print
version of it
instead of printf do they i'm not sure
the print the print f looks familiar at
least so maybe that's just okay good
looking at something else yeah okay but
you don't get anything free because
c and not arduino so you actually have
to say where that comes from
so we have to say like that's i want
standard input and output
so include standard input and output and
then i get printf
okay so if you forget to write this is
almost always going to be at the start
of every program you write
if you forget to include it and you're
like why is this not working
what is the pound sign yeah
that's a pre-processor directive so
c back in the day sort of pre-dates
being able
like python is like so radically
different because it's so far in the
like compared to see it's like 30 40
years later
so at that point they were like there
was a notion of
files on the file system that you could
c didn't have that c didn't come c
predates i o
basically so i mean it's like right
there at the start of the operating
system so
like nothing is included like i said you
have to know what you're paying for so
in this case
this is a preprocessor directive which
is saying
before you compile this program i want
you to go
find this thing stdio.h
and these angle brackets oops these
angle brackets
well the less sign and the greater than
they say that it's in the standard
system path and this
says i want you to go include that right
as if you copy and pasted it
okay so there's none of this runtime
stuff like like in python you can kind
of do something like this and be like
you know oh import the thing yeah
you know and it'll only conditionally
import this thing like it'll it'll be
like oh yeah well if she runs main
then i'll go import the thing there's no
notion of that in c
so c says if you include it
like i will just paste it right there
before i compile it because there is
nothing that pretty much happens at
so there's a whole bunch of these that
we're going to end up using
like we can do you know these these ones
with the angle brackets are sort of
standard like
you know there's standard def and
standard library and you know
there's types and there's time and
there's you know all sorts of things but
we're going to get to those a little bit
for now this one we're just going to
kind of one off and it includes our most
important function which is
printf so that has the function
signature for
for printf and oh signature just means
the declaration line
like you know this main line i'm
okay so and so you so because this is
because all right so the way i'm
thinking of include which might you know
not be right but it is
a little bit like the import statements
except you said like it's telling it to
basically copy and paste
the code from that particular file and
is that why you don't have to like say
studio dot
or you know whatever dot h dot print tf
because oh you know you're not you're
not you have to reference
yeah you don't have to reference it
because it's actually saying include
the content of this you don't have to do
that right
because it's including it it's not
importing it for exactly the reason you
there is no notion that's why import
they actually that's why they changed
the word
because they wanted to say like this is
not your c
notion of an include because it include
literally means
take this file and copy it right here
oh wow okay like do you have imports as
well and see
there is something similar there's
dynamic libraries that you can load
but that's definitely beyond this course
okay yeah no worries i'm just curious
yeah there's a dl open
there's there's a bunch of stuff so like
you can programmatically say pull this
thing in but i can tell you that that
came like
10 years later so
you got to keep mine when they were
writing c like punch cards were still a
oh my gosh i finally figured out why all
my computer science professors are so
obsessed with sorting algorithms because
you drop a stack of punch cards and you
know there goes your afternoon like i
i'm just positive that's the reason
they're so obsessed with sorting
but i'm glad there's a reason in there
somewhere other than just torturing us
i'm coming up with it i'm not sure it's
the thing but um
yes yes loaf bone it was originally
discovered in cave painting so
it's it's that old but you know it's
it's kind of beautiful in the fact that
like it does
nothing surprising like it does
literally whatever you tell it
and there's there's no magic hiding okay
so to use printf we basically just give
it a string
um i'm starting a little bit higher
level on this but
so um strings work
pretty much the same way that they do in
python so i could say like you know
this is um output um one thing you're
probably not used to is
c does nothing for you so you have to
i want a new line at the end of that
okay um
so that's a gotcha but
so if we go and we execute this thing we
actually get the output
cool okay great so if i forget to put
this new line here i can run it
and that's fine until i run it another
time and
i see what you're saying all right oh my
gosh yeah so literally
you get nothing for free um
so okay cool um
this we're going to be using to dump
stuff out uh
and it has a whole bunch of format
specifiers which you might have seen
with um arduino i'm not sure if they're
in there or not
but let's start off with some variables
so like let's let's just do a little bit
of basic math so
in python i would say something like
yeah let x
be five and except you know by the way
you have to put a semicolon on the every
end of every line
that was the statement terminator
because c never knew when you were done
you know because you could
do this because it didn't fit on your
punch card oh my god
um okay that's actually kind of cool
there's a cool reason for it but
is this where we go like var x oh var
we don't get far no is that not what it
is oh that's sorry that's arduin
yeah no you have to specify a type it's
not just a variable
yeah if we run this thing it's going to
fail in the compile pass and we get no
output but
if i actually had my error message
showing up on console it would say
this for you
i see okay because we haven't declared
it first that makes sense
so to declare it we have to specify its
type i wasn't kidding about types
so if we want this thing to be a number
we just call it
okay okay um follow good practice for
now so like if you want to do multiple
variables put them on separate lines
this is actually valid
you can do that but that's that's
naughty depth so
let's not do that let's try to write
clean c for today
okay um all right so if we want to do a
little bit of math
um and let's uninitialize something so
we're just going to have z which is
now keep in mind like python it sort of
does default things and it says that
will be equal to zero
c does not do that
right now z is literally
whatever that piece of memory had in it
prior to allocation to you
so if it said that if this if the run
time ends up going
this is the piece of memory you're
getting that will have whatever it had
in it
now modern see modern operating systems
clear that memory because that was a
great way to steal passwords
back in the day you never really knew
what you were getting when you got
memory so
nowadays they actually do clear it but
like do not count on that behavior
because you never know where this
thing's going to be running so if you
want it to be initialized to zero
set it to zero okay
so how am i your basic arithmetic works
kind of the way you would express
so why don't you just take a guess how
do i get x times y
and z x times y
and z oh in z let's store the result in
oh lord um well so i've already declared
it do i have to declare it twice
nope already declared okay so in that
i've even set the values for you
yes that's that's exactly it so
your standard arithmetic works exactly
the same way as python so
you don't have to worry about that stuff
even function invocations kind of work
the same way like this this whole
parenthesis you know to say like this is
a function i want you to call
okay now we might be curious what the
value of z is
in python we could just do something
like this
right yes except that's no good in c
what why because
because z is literally unlike in python
you see what python's doing is when you
actually have that z
you're actually casting it to a string
right you're saying like okay i want you
to turn this thing into a string
and then put that on the output
wait so because is that why can't the
output handle integers
because the output doesn't handle
anything but strings
yeah okay strings only
did not know that okay so that makes a
lot more sense than so you would have to
kind of change its type first
but you can't change this type because
that's exactly you can't change types
and how would we change its type we'd
have to write a program that reads the
and turns it into equivalent ascii
characters yes
yeah that that's that printf does that
for you but you basically that's what
the f
is f stands for format i see okay so
that's like a nice little
handy tool because then you don't have
to deal with that's why we're starting a
little bit high level if we if we
actually use
like the low level stuff like put c
puts a character on the output
if i tried to put a character like 65
what do you think
well actually let's put a number sorry
if i try to put the number 65 on output
what do you think you're going to see
well i'm guessing it's not gonna work oh
yeah it's it's not happy because uh
put c requires two arguments of course
um this uh you're not on a
unix system right now right i am not
okay so you actually don't have the man
pages but
um oh sorry i actually want put care i'm
writing the the short one here but
um this this is actually the routine
that i'm looking for so if i go and i
run this thing i get an a
what what
yeah i run it i get an a i put in a
66 i get a b what
i put in if i put in a c
that's not even like like that's not
hold on um if i put in a 70 i got an f
that makes
what hold on up one off
that's not that's not going it's not
mapping numbers just straight to
letters so what ah that's the fun part
this number here is just the number 74.
yes it's literally the number 74. so if
i stick that into a memory location like
if i say there's a character
by the way don't don't declare things
down below
try to do your declarations up at the
top of whatever your routine is
so if i have a character which is just
like some character
and we're going to use c-style camel
casings so
i don't want to see any of this camel
casing nonsense
no snake casing rather so
we can actually just do some character
and i can do something like i want some
character to be equal to
74. that looks wrong doesn't it
it doesn't yeah i mean it looks like
it's just putting 74 into some character
but if i go and i actually dump this
thing out on console
i get that j again
right so actually
what i'm actually hiding here is this is
this is ascii
in a nutshell so um okay
ascii manual page
so i'm going to try to do this here so
that we can see it on stream but
um this is this would be what you got if
you typed man
ascii then uh if you can see the stream
yep this is uh this is what you would
see you would see this sort of character
and okay we're putting in numbers that
are decimal
because by default it's decimal unless
you say it's not decimal
and so if we jump down here and we go
looking for 74
of course i'm looking at the wrong
column but if i go here and i find this
decimal 74 i would see that this is
really a j
okay so if i wanted to write out jess i
could do
74 69 and
okay so you said it doesn't like it
doesn't give you anything it doesn't
give you anything for free
but it's giving me ascii conversion for
i'm telling it i'm telling it print a
number and it's like i'm going to
convert this to
that's i mean that's actually
in that function so that okay you're not
getting that for free
um so if i go and i execute this then i
can spell because some care
actually has a 74 in it
so i get chess oh my gosh
okay i get jess the hard way right you
know i actually had to spell out
every one of those ascii characters so
um that's kind of
because you called it a character though
so it
up there when you declared it you told
you told
i don't know you told the program that
it's that it's a character does that
mean that it's that's not a problem
don't worry about that
i can actually call some character
actually you know why don't we just
change it to an n
except put care takes a character so
if we run this we're going to get a
we're going to get a compiler warning
which says by the way you're
using the wrong type so i'm just going
to cast it for you
and it still works so we actually still
get jess
here in the output but if we go
and we like i mean so it doesn't really
seem to matter because that 74
if it's a character or if it's a
if it's an integer or a character some
care as
care and i make that a 74. i can
i can do this just as well
okay okay so this is like kind of the
bad part of going into c like when you
get casts
when when you're sort of taking a type
and making it another type in python
you're actually getting a lot of magic
happening kind of hidden from you
but this literally puts 74 into memory
so like this memory location which has
some care as care
has the number 74 in it and if i put it
it doesn't matter if the type is a carer
and in it's going to treat it as if it's
so if i type something like this if i
say like
j as a character then
it's this is going to work and
if i ask for the character that's after
that which should be
well let's say some care is some care as
care plus one then
cass okay but we don't want to turn you
into cass because
bad star trek i'm clearing my output
i should have thought that out
i actually i really like voyager by the
way but um
me too my computer's named janeway so oh
nice i didn't realize that um
so by the way in c this is quite
like single quotes and double quotes
you'll see why i write double quotes
every time i write python but
they're not the same in when you're when
you're operating in c
so okay single quotes means that
this is just a character so it's for
exactly the kind of line that we just
wrote like if you have a if you have
your variable declared as a care
then you use single quotes if you want
to use a letter otherwise you can use a
and that's fine because you can go look
up in your ascii man page what the right
letter it what the right code is for
each letter
that's a lot of work so we could just
outsource that to the compiler and say
here you go figure that out okay
all right and i'm just going to get rid
of line 11 here so if we go and we run
that that's fine but if i go and i
change this and say this thing is
like really double quotes
what what
what no it doesn't make any sense right
that's because we're used to things like
python where python's like hey you got
double quotes you got single quotes
whatever you mean letters right
what are double quotes for c what are
double quotes double quotes
are actually for strings so we actually
used one before when we said printf
this is stuff this is a string right
and by string it literally comes from
string of characters
like if you took a bunch of characters
and strung them together
with a piece of thread that is literally
what that came from
so okay so that's a string but that's
different from a character because a
is only one of those
including spaces though right so you
could do a space there yeah i could do a
but i couldn't do a space and a z
because that's two characters that's two
characters right
so it's this is kind of like the first
hard shift for c is that like
every memory address has literally
whatever you put in it
so all right does that make sense yeah i
know that does that makes perfect sense
i still don't know why it's a d but yeah
well let's get to that because that
funny you should ask because
let's say that we had a buffer and
what if okay well it doesn't need to be
a buffer why don't why don't you name
the variable i'm typing too much
no go for it it's called buffer uh
buffer okay
so let's say that we had this thing and
it was called a buffer
and we wanted to put the characters for
jess in it
okay so how much space do i need to
store the word jess
um four characters
four characters worth of space so and
we're gonna upgrade ourselves into the
future we're gonna add lowercase
which came after c by the way
okay um all right so we go here and we
think all right great well we'll just
assign it to
jess so we need four characters right
turns out a raise you're familiar with
arrays in python
so i can allocate four characters and i
could say
buffer now this is why i wanted to call
this thing buff but i'm just going to
call it b
because i don't want to type a lot so b
sub 0
is going to be j b sub 1
is going to be e b sub 2
is going to be s see now you've lost me
a little bit
because when you declare um
like your char b um
i'm saying that it's four wide
okay and so four wide of whatever the
type is so in this case the type is care
so if i do b of four like this then i'm
gonna get
four characters in a row i see and then
the rest is indexing
correct so here this this line line 11
is a declaration but lines 13
through 16 are assignments so there i'm
actually saying like
okay put j into location b sub zero
put e into location b sub one and so on
so i see
because this is this is okay that's
interesting so an index
of characters is still
um is still declared as a character even
though technically it's an index of
that's correct because what is the
underlying type
of each location in the right a
it's a character so if i want the type
to be an integer
then i would get an array of four
integers which
coincidentally works just fine for
characters but
you're going to see some crazy results
if i actually do that so we're not going
to do that
so actually why don't we start with
numbers just before because i'm about to
trap you in something terrible
enough terrible things at once so okay
let's just say like all right so we want
to create this b
thing well just to not confuse us we're
going to we're going to
get rid of all this nonsense and so
declaring this stuff an array of four
that's correct got it okay so
now i was just about to sneak up just
before we get to that string thing
i want to sneak up on why printf is so
cool so
we actually come here and we say like
print f and if i tried to like
back to my initial example if i just say
b is like five or something
okay so if i try to do this
bad stuff happens as we kind of learned
before and that's because
b as 5 would actually cause printf to go
what is ascii character 5.
okay it's the enquiry
command okay
which is a control code because it's
down in the ascii set like the bottom
like 32 yeah it's 32.
um the bottom 32 characters of the ascii
set are all control codes
so okay if we try to print them out
like say we accidentally print out a
number like i just did
it's going to interpret them as whatever
those control codes are which is not
actually what we wanted to do
we wanted to see what the number was
and you'll notice here if you still have
that page
then you'll see that the actual digits
are encoded up above
so they're right after that control code
area so well
i mean there's a bunch of symbols here
in the middle but um so
0 actually starts at 48.
so of course because that's the code it
was assigned because if i actually look
at zero
that's null yeah okay um which would
which actually has a different meaning
and the new line that i was doing before
was is actually code 10. so if i wanted
to see a new line
i could just put i could using just put
i could say put care 10 and
this would actually give me a new line
which it does
every time i run it if i try to see null
bad things might happen so let's not do
that to our poor program
but if i wanted to see zero i would
actually have to jump all the way down
here to character
48. okay so i would have to do
that and there i actually get my zero
and if i want my one it's
48 plus one it turns out it's in the
spot they weren't crazy they didn't put
the numbers all random places
that's good at least yeah ascii was they
were trying to be
sane but um an attempt
yeah that that's all fine except the
minute that i roll over past
that i'm going to get something a little
bit different because i'm out of the
printable characters
because keep in mind ascii is encoding
what letters and numbers they are it's
not the actual number itself
like we might have an integer
okay and that's really just about like
how we're going to encode the text for
display on output and
it's a rough thing to see the first time
you're in c but
that's kind of where it starts you know
you have to say like if all you had was
this put care routine
the first thing you would have to do is
like take a number
and chop it up like you would have to
like come along here and say like
which we're gonna get to that but um
lord you would have to say let's say
the variable number was like one two
three four
actually that's gonna screw us up so
let's just do seven nine
two and four okay then we would actually
have to to print this out
we would have to look seven up in our
seven is here it's it's character number
in ascii and we would have to look 9 up
which is 57
and we would have to look 2 up and we
would have to look 4 up and we would
that this is really not fun to do on a
table but it would be 50
and it would be 52. it's mildly
yeah and so what we would have to do is
we would have to take this number which
is actually just an integer
and we would have to start cutting it we
would have to say okay divide out like
the ten the thousands place then divide
out the
hundreds place divide out the you know
tens place and divide out the digit and
i mean this seems particularly
nightmarish to me because i use
statements in python quite a bit when
i'm like writing a new function that
might be a little bit complicated i will
kind of go
piece by piece and put print statements
in there just to verify that it's doing
i think i'm telling it to do and then
i'm getting the outputs and like you
variable definitions that i i think that
i'm telling it to do and often i'm
i think i'm telling it to do something
and i'm not so it's a great way to you
know have that that
verification but how do you
it's a lot to learn at first especially
for c i mean that kind of
that kind of debugging doesn't seem to
be or that that ability just sort of
print various
um yeah values doesn't it
really you can't do that well so that's
actually what they went and they did
for you in stdio.h
ah perfect thank you so much that took
care of the problem that's exactly what
because this gets old fast
as you can see um so like if i just want
to see like what's in that integer i'd
have to say like okay well
take the take the first you know take
the ten in my example that i had up
seven nine two four i would have to say
take this thing and chop off the ten
the thousand digit and uh
add that to whatever the value of zero
is as a character which is 48 if i
wanted to unroll it
and i would have to put that thing on
and forget about the conversion issue
for a second and let's just
just to be careful on order of
operations even though plus is lower
we do that and we get our seven okay
so it's mildly again inconvenient
i can do it this way
and i can run it
what yeah because the problem is that i
didn't get rid of the old number
so because yeah you were just still all
right yep that makes sense yeah i
divided it there and then it turns out
that that thousand is still hanging out
so i would have to do this thing and i
would have to
you know take this thing but i'd really
have to get rid of
the result before
of that thing right and
okay that'll give me
yeah and this is about as much fun as it
so we're not going to do that we're
going to thank the good authors that
they actually built this printf function
and what printf does so even though it
doesn't it's not as magical as
python and that you can just put
anything in there and it'll just sort of
figure it out it'll be like
oh that'll probably look like this if i
cast it and python's pretty good like
you can print a function
and it'll give you something
you know it won't crash but okay you
know like i could even print f print f
that won't work in c
now i'm lost so c requires two things in
order to print things out using printf
and by the way printf is going to be the
general thing we use
you first have this string which as we
saw before we can put literals in so
like i can just say
jess is learning c right and i remember
to put my new line
i execute this thing jess's learning c
shows up
okay well i can also specify that there
should be a number
i can say like okay i want you to treat
this as a decimal number
and this percent says that i'm doing a
format specifier
yep and it works this i don't know if
you do this in python using a twist
okay that's actually based on this
syntax oh good all right something
that'll look familiar
it will look familiar so if i go and i
pass this thing in integer
d is the format specifier for integers
and i go and i do that
and then it puts a new line after my
number gets interpolated and
yeah we actually see the number so we
don't actually have to cut the thing up
and write our own implementation
which is very handy i could say output
is i can this works the same as python
so you know if i
put a string literal i can embed it i
can i can
put it in the middle somewhere you know
it'll just
it it kind of does normal things if i
want to get an actual percent to show up
just double it up okay
okay so that's the way we're going to
output things
now we can't mix and match types so
if i have something like you know let's
say that i have
another string here like which is just
another string
and i try to take that another string
and output it using a percent d
integer specifier what's going to happen
it's not going to like it because you're
specifying it's an integer and then
you're feeding it a string
yeah the only difference is that c just
tries to do whatever you told it to do
it's not going to say i don't like that
it's just going to do some crazy stuff
it's going to say fine you want this
thing to be an integer i'll turn it into
an integer
okay well yeah not what i expected no
that's probably not what you expected
because likely what it did was it said
okay where is this thing stored in
okay i'll just print that out because
that's a number
okay so we don't want to do that so if
you start seeing some crazy output just
check that like
you know your format specifiers are
right so
and i can specify that nothing gets
output and bad things happen so i could
say like
a thing just like python by the way
percent s for strings
okay so i can get a thing that works the
way we think it's going to work so
if i want to assign that to a variable i
can say
you know before i was using the example
of jess
so we were taking
this would actually work the way it's
written right now
so this will output jess um or you know
let's see
is yes
okay now um let's be more tron like
okay so um if we wanted to store that
well the only two types you're going to
be working with most of the time are
going to be integers and
characters aside from some special
things we're going to get to a little
later but
if i want to actually store that thing
in a string like
a string then i would have to have
enough space specified because it's
these four characters right j-e-s-s
wait you're joking you don't have like
it's there there isn't like a
there is a convenience operator but okay
oh no there is not a string type in c
because a string is literally a string
okay so if i want to do this i would you
guess from your earlier comment that you
said that this was this needs four
okay it's a trap by the way i'm gonna
tell you right now
and i'm gonna just say like okay let's
specify this thing as jess
and we try to output this the thing is
c has a convention which is that how do
you know when the string is over
in memory you reach the end of
how do you know where the spaces you
gave it like i mean we gave it four
spaces so it knows after four characters
stop but c doesn't know that c doesn't
seem very
smart quite honestly no actually it
doesn't try to be smart at all
trying to get a toddler to do something
i'm like okay exactly this and they
still find some
and you're like that is a very good
analogy because that is exactly what
it's going to do
because the way it actually stores this
thing in memory is
it stores it with a null byte at the end
it uses a
null byte which corresponds to the 0
that i told you we were going to get to
in our ascii table that means
end of string okay now i don't need to
specify because the compiler will do
that for me
so anytime it sees a string which is
this double quotes it automatically
that character at the end okay you can
imagine how bad that is in the line that
i just wrote
because how many locations did i give it
uh six well i in a string how big is a
uh four right how many characters am i
trying to shove in there oh yeah sorry
six yeah yeah right now well except that
this is just a convention this this uh
that just means so i guess one
yeah yeah that counts as one character
just like this only counts as one
character that's just us telling the
insert this character which from the
ascii table
you know just insert this one character
which is just
corresponds to digit 10. okay
so this actually takes
one more location than what i've
so i would get my first crash if i did
really yep because i specified that this
thing has
four memory locations and i wrote five
in there
so it even if you even if you don't add
in because you said the compiler will
kind of put that null byte at the end we
have to remember that the compiler is
going to do that and give it
five yes and if we don't you get to hold
both pieces
you get a nice little crash it'll go no
you're writing into other memory because
literally c doesn't do any thinking
unlike python
it will just go oh well i'll just try to
write into the next piece of memory
which is not yours like
it'll just go writing into another
process and then you'll get a security
violation it'll go
core dumps you know segmentation fault
because you wrote past your segment
and that's actually the original seg
fault so this is where programmers
always kind of start off off by one now
the compiler is smart these days so it
does allow this syntax
this is a convenience so you can
say like you don't specify the size like
we're doing
right here and it'll go okay
i'll just allocate enough memory for you
oh okay which is really cool right
yeah until i try to do something like
this right after it
right because it's allocated enough
memory for the first thing
and if you try to change the size it's
going to freak out exactly the same
problem we have before it's going to
write over
someone else's memory okay so
this part this is why allocation is so
important because i i told you see
you're going to be handling memory
so this will tell you like this it'll
let you do it
you know it's just going to try to write
into somebody else's memory and
probably crash at best at worst it
doesn't crash because if it crashes
you'll know you have a problem
i just have never felt so grateful for
python's print function
this is why i told you so many times in
your stream like
you don't want to manage memory if you
don't have to
maybe this whole course is really just
me trying to convince you not to learn c
but no no no i'm
going to tell you i mean it's funny
though because it like
i think some people and and i mean some
people are
are well-meaning with it but some folks
i think who are are very very
well versed and see definitely there's
kind of a a little bit of like a like a
condescending kind of like well you
don't know you know and i'm like well
that's fine because you've been hazed by
your programming language
and so you just want to feel like it
means something and that's okay
but like you're getting hazed you're
getting bullied by your programming i
completely agree with you and that's
part of why i i generally take the
assessment like
you don't really know c do you
because i find that the more experienced
the c programmer is the more they like
i sir i would like i would put that on a
t-shirt but i feel like i would get
yeah no i it's true though because it's
like do you really want to manage this
memory it's like it's work
you know so anyway the the nice thing
and the reason we usually do it is
first of all i mean those languages had
to be written in something you know
like c was written as a bare minimum
sort of over the hardware because we
didn't want to actually write like
you know because assembly we're really
going low level you know we're
we're going like okay you know load into
the you know let's let's define a label
you know that's going to be oh anyway
different class
so um the point is that it was far worse
than this
and and so this was written so that we
could write
slightly higher level you know stuff
but still know what we're going to get
out if we actually look at the machine
you know so it's it's kind of like a
minimum abstraction as opposed to
something like
python which is a really high level of
abstraction that makes a lot of sense
okay so it does mean that like you know
it'll break in interesting ways it'll
haze you in interesting ways but this
particular program is not valid
and i can go ahead and try to run it and
okay there's actually a separate problem
with this line because i've been doing
python all day but you can't do this
this whole equals a string
is not valid c what it's only allowed
on the initial assignment and that was
because of convenience
right so i'm allowed to say like a
string is just right i can i could do
another variable i could say b string
is some guy and this is fine too
right but you're kind of trying to like
reassign it yeah if i want to take a
and set it to b string
this doesn't do at all what i think it's
going to do
so aside from the fact that it's going
to fail
because it goes i mean the error that
you're going to get just
just for your reference which you're
going to get as soon as i can link up
the error messages to
the learning tool but you're gonna
you're gonna see this if you looked at
okay you're not allowed to do this and
the reason you're not allowed to do this
is because you're operating
on a complex data structure which is
like c doesn't really know
what you want to do here like do you
want to copy each of these letters do
you want to transform them when you're
do you want to just have a reference to
it you know like c doesn't
try to interpret what you're doing like
python will go i'm just going to
interpret that as anything that looks
and if it looks unreasonable i'm going
to fail c does not do that it's going to
literally do whatever you tell it to do
yeah so it's just it's a little bit of a
mind shift change so all right well we
can actually do this
it there is something there's another
convenience routine so
just so that we can kind of like get
that started so
here i assigned a string and this will
work the way that we wanted the other
one to work earlier
so this is a string specifier this thing
is a normal string of characters
and that does not what i think it's
going to do because
we still have the a string equals yeah
let's get rid of that thing
so we run that programmer is jess right
and if i actually look and i say show me
the value of like if i if i go and i
at the first element of that string
what am i going to see
i want to say it's going to be a j it's
a j but it's not
going to be in j form right is it going
to be the number
yeah exactly it's going to be the ascii
value of j
so if i go and i look you know it well
did i do a lower case or an upper case
it's lowercase so let's let's leave this
newfangled world and go to the old
fashioned world where
everything had to be uppercase
so if i go here and i look and i see the
j it's going to show up as 74.
i run this thing okay it's actually
being kind to us it's showing us the
thing that's
oh because it's a character and i didn't
say put the number
but if i actually cast the thing and i
said okay i don't want you to
treat that as a character i want you to
treat that as a number
so i can cast it and you do have casting
in python right
um i think so okay like what do you mean
well see here it's interpreting this
thing as a as a character meaning using
put character
but um oh this this this right here i
meant this is a this is a cast
i'm saying take this thing and
treat it as this type so okay
so if i have like me let me break that
apart for a moment um
if i if i want to operate on number and
the way i showed you to do
number formatting was to do a d um
so if i want to actually say like show
me like
this as a number so in c and let's just
make that 0
except we're going to set that equal to
is really a string the first character
of a string
sound good well so
i thought okay so so two things first
thing i thought that
you know you couldn't change typing and
so there you're changing from an integer
to a character or is it just gonna kind
of do the i
i'm going to do exactly what you so how
do we say i want you to become an
um and we cast it
okay that's exactly what we do this is a
hint to the compiler i'm saying
i'm not willy nilly turning a character
into an integer i'm
telling you to turn a character into an
and it just defaults to ascii well it is
actually it's not default in ascii it is
an ascii
because this is ascii like by standard
so like this actually is not
showing up in the computer as j-e-s-s
it's actually showing up as those
that correspond to jess so
this is actually just going to show me
what is that number
and then print it out well it's 74.
okay so if i want to see like the first
that's the e right and how would i get
the next you know the s how would i see
yep and unsurprisingly if i look at the
third one
i'm still going to see the same thing
because an s is an s
right and then i told you there was that
hidden last character so if i look at
the fourth entry
there's my null that's so cool all right
so it is there it's just this is we're
using kind of a convenience method to
sort of assign it all quickly so that
we don't have to go and do a string sub
is j and a string sub one
is e i mean that's
that'd be pretty arcane with modern yeah
technology so
so c does do it tries to do some
convenience stuff but it doesn't want to
break that like
you only get whatever you pay for kind
of mentality
that makes sense okay so um
great that's enough me programming um
now i think we need to get you
programming a little more
uh so oh cam
yes we are gonna get to that but i wanna
i wanna introduce some uh
looping stuff first uh so
now right here we've sort of implicitly
said like
make this thing i'm gonna just clean up
our program a little bit so
we're just down to the basics here
so here i'm going to print f and i'm
going to change the design
i like to leave a little space between
see up here we have kind of like our
variable declarations and
here we're doing some business and we
try to keep those separate
so if we go and we print this thing out
great it does what we think
it's going to show us jess and if we do
those one at a time
it's going to show them to us so how can
i actually print out
those letters so one thing that you
probably haven't seen
in c but fortunately your instincts are
going to be right because you've seen it
in python
is a for loop and it works
pretty much the same way but you've also
used while loops right
yep okay so how can i
iterate through every character in this
one at a time i can do it in python
i'm not sure well let's do it here um
so i'm not sure how would you how you
would and see
well let's make this a little bit
simpler let's say like okay i want to
um you know current character
let's do current care um we're not going
to initialize it because we're
we're like that and we already know that
this is how printf works so i'm just
going to comment that out
okay so how do i know when i'm at the
end of the string what is always going
to be at the end of a c
string hopefully you have that that null
yeah the null byte so let's use that as
loop termination so let's set the
current character
to the first entry in the stream which
would be what
um are you asking like for an index are
you asking like
do you want to set it hold on so do you
want to do it like this or because oh
no let's use the actual a string yeah
yep and we have to terminate our line
because it's not python
like some muscle memory yeah that's some
muscle it's it's
you'll see why i have trouble switching
over from like python to see it back all
the time but
um okay so that's our first character
and then
basically we want to kind of like loop
through this a string
until we hit the null byte okay
so i can say while and
always it's python so use it's not
so always put parentheses around
whatever your condition is
okay like python you can just do like
while true or whatever but
um so let's say that this is while
current character is and by the way the
conditionals work exactly the same as
what you're used to in
python so as long as this is not equal
the null byte okay because python i
would do that but
i have to put it in a block okay
i want to actually show each character
so how do i do that we want to see the
for what the character is
you want to see the number for what the
character is for all of them
um well actually we can start with just
printing the characters out one at a
time let's start there
um what's the what's the in indentation
convention oh just four yeah we'll stick
with that
okay um so
all right because my you want them one
at a time right
okay printf would do them all right
oh okay hold on hold on hold on wait
okay i would do this so differently i'm
like having trouble because wouldn't we
so you have current character is
is this but wouldn't instead you want to
like have
this be the thing that you're iterating
over because you're like continuing to
index the string right so
yeah we're going to need an index so
let's give ourselves an index
right so like wouldn't you want to go
yeah okay
okay so yep
and then so you're going to want to go
and then i don't know if this is the way
that you write this
in strange c land yes that is totally
valid for the
uh increment that you've got here this
is actually a good way to write
um you can also write it like this you
can write it like this
don't don't do that i'll stick with my
happy python right
yep yeah this is fine but the problem we
have here is you're indexing
um current care and current care is
actually just a character
good call so we actually need this to be
yep oh no no no hold on hold on hold on
we could do that
i goofed there we go and then this
instead will be
all right yep except printf always
requires a format specifier
ah crap i'm sorry i forgot what no
that's totally fine
even better put care doesn't so i'm just
going to change her
okay perfect
okay so that's going to write out those
characters one at a time right
okay one little
logic thing is we've already assigned
and the first time we run through the
loop we've already assigned curcare
yeah so i was going to ask you how do
you do reassignment then
no no we could reassign this is all
correct i'm just saying that
i'd probably put it down here
is that showing oh i see yep yep yep
okay i see what you're saying yeah and
the idea is just that
like you know we've already assigned it
up front so that we could build our
while loop
so we can actually do this we can say
okay put out the current character that
i've assigned
and increment i and then
take i a string of i to curcare
and then the next time through the loop
we're going to say current care is not
equal to
null byte right all right run it
oh wait wait wait wait because you
wanted hold on no that's it that's right
you got it okay
i didn't want him on a separate line so
i was like oh no do i have to add that
oh yeah you could totally do that yeah
go ahead give me the new line
oh god i don't i don't that doesn't work
does this work with put but we don't
want to put it putcare only does one
character at a time
so if i want to put out the new line it
which it will work with it
okay so then
uh i really don't it's i know i know
that it's like this but i just don't
this is like a variable so
which what i don't think that updated
can you i think we're desync can you hit
reload oh yep hold on
okay sorry now where was it okay no no
you're fine so um
current care is a variable name and so
i can't just like add like backslash n
on there
so right i can't concatenate
right so i mean python would allow you
to do that but
i have to be explicit and see and even
if i did stick it on
i would end up needing memory for it
because where is it going to put that in
memory because it's going to be another
character right that's another character
so why don't we just go simple for now
and just do it on a separate
call put care twice
yeah we'll just call it for one
character at a time
yep yeah and because
we want to sort of leave our loop
business separate
i'll just add a new line but yeah look
at that output
oh yeah all right it does exactly what
you think and if i wanted to put like
a little you know greater than like i
have in the output
because someone had to write this thing
i could do it this way
and if i wanted a space there i could do
right all right and so i can execute
and that's all fine so that first line
has the one that comes from the program
and then the
the other stuff you know gets that
format yeah
now obviously that's easier if i do it
with a printf
so let's just say we're using printf to
go a character at a time
which the specifier is c unlike
python just treats everything as a
but but c doesn't right it
differentiates between a character
and a string of characters so i can do
c thing which means i'm going to provide
a character and then i'm going to
provide a character
because they have to match and if i
wanted to get this output like i just
did i could
put that right there as a literal right
okay so now you also had your new line
okay which i can stick in the string so
i can i can run that
and we get the same output cool
that makes sense yeah oh yeah okay um
then while is a little bit
weird here because we're usually you use
a while loop when you don't have an
iterator like in this case i so
this would naturally be done but it
helps us focus on the logic so
nice job with that by the way um yeah
normally it would be a for loop right
yeah we can use a for loop but the catch
is then in c
see like in python we have this nice
like four in syntax yes
happy place yeah it's a happy place
there we go
it's like always i reckon we don't get
foreign in c
in implies that we know something about
the data structure which we don't
in this case like we know it's a string
because we said so but
c at every line just goes all right i
got characters you want me to work on a
character is that a string that's up to
it may or may not be terminated by a
null character so
um in order to be able to loop through
the thing
and we're going to keep the same logic
but we're going to convert this to a for
so for loop we're only allowed
one type of for loop in c and that's the
three argument for loop
so it always looks like this and
this is our terminating condition this
current care
is not equal to null byte so we're going
to pick that up and we're going to move
it exactly there
the first part is whatever your setup is
so in this case i want i to be zero
and the last part is whatever i want you
to do on every iteration through the
so this is where we usually do yeah we
we could use i plus equals one if you're
comfortable with that
this is where you'll see um increment
syntax most often
okay um but you know we can actually
we'll stick with your we can we can
unroll your thing and
really write it out oh lord okay okay
so um this is a valid for loop but now
we have to modify the rest of this loop
so what do we need to do here
okay so let's think about this so we'll
first get rid of the while right
yeah yeah um
so what is it doing so right now what
it's doing
is it's printing this which would be
and then we don't need this
i'm guessing because i think that's
probably what this is doing is
you're kind of telling it how to iterate
up here
um and so
also is it just convention for this to
have the spaces after you're very
uh you cut out there at the end is it
convention to have what uh the spaces
after the various conditions for your
spaces after the various conditions um
so you see how like here
oh oh oh yeah yeah sorry that's
convention okay yeah just curious
c c totally allows this by the way
ew i don't like that no no i don't like
it either i don't feel like it's very
readable but
yeah i know some people do this which i
don't like that either
that's that would probably be my
inclination just from i don't like it
because this to me
is like i've terminated this statement
sort of like up here
okay i see what you're saying so i mean
the space kind of differentiates yeah
like this to me
says like okay this is part one part two
part three
okay that makes sense all right so let's
see then for it
all right so we're starting there fine
would iterate okay
and i'm just writing up four on the
whiteboard for you
would would this work
uh well one way to find out right all
right i'll run it
uh interesting oh okay yeah so
yeah i'm seeing output i'm seeing output
but i think
i know you said this needs to be at the
bottom but i think for this
for the for loop i think i need here
because now when we do it
yeah now i get one j yeah and you got
something else there at the bottom
oh i got the yeah you got the null byte
so i guess we got to find out what what
happens when we put the null bite out
because i wasn't really sure
so there's theory
so wouldn't it wouldn't it terminate
though when
the the current care equals
the null byte or is it because i see
because it's getting assigned
yeah so our issue
here is that we're you know
we're going through until we see the
null byte
but we're already running one more time
through the loop because
i is already like the next value
right so i i can i can do this is y and
c we usually do like this notation
can i do this so could i go um
okay hold on give me a second jess
sure all right so if we do that
no i still get it hold on okay one two
three right four there we go yep
yeah c program you're always off by one
all right um i was like wait did i add
one and then no i
anyway um so would that be a better way
then to have that terminating condition
so that it's like really explicit
uh yeah the only problem is that
now that i'm specifying that it's four
what happens if i change my program to
um you just never change your programs
ever okay
well that is actually what most c
programmers do i don't want to change
the program it works
don't touch it okay yeah if i run this
then i'm not going to see my whole
string so that was the reason we were
using the null byte
i see okay but it wasn't so okay so hold
on i got an idea i got an idea yeah
let's do um hold on
yeah we can add one sure so
it's always checking the next yeah i
like it right
okay oh
wait what did i screw up here hold on
so i equals zero can't
be this
um i'm gonna refresh to get clear the
output window
yeah i'm gonna actually do the same
thing i'm gonna add a button for that
no no you're fine i was just having
trouble i'm going to re-run it because
i'm probably seeing
okay there we go that makes a lot yeah
did it just print out en
okay so that means um
that should actually be enough then i
okay so because that string of i
is not equal to this null byte
because we're so pre-increment i
so it's going to do that each successive
time yes
okay so we yeah because it i think the
as how i'm understanding it is that when
we have the
the terminating condition include
current care
the problem is that we're not like
reassigning current care until it's
actually started the loop
and so we need to we need to have the
being like kind of outside of that
assignment yeah i guess
so yay
there we go bullseye right okay yay
you got it so this is good um
now if we screw ourselves up and we use
the post increment should be exactly the
same right
um just increment
uh i looked down to write something down
what did you change
um i took here we had this
okay so we had pre-increment i and if i
change that to post-increment i which
means like
do the increment at the end of the loop
that's what that means okay all right
yeah okay so hold on so then if we're
changing the i
um what's changing at the end of the
i don't think that will work what oh god
run it
no all right but i think that way your
your changing of the eye might be a way
keep the current care maybe
anyway uh this i'm just going to warn
you right now
this will get you into trouble you're
okay in this program
um but what will get me into trouble i'm
sorry the
the post oh sorry there you go selecting
the text yeah no i forgot to look over
here no no it's my bad
um i'm gonna add selection don't worry
uh so
using post increment will often lead to
like kind of off by one
errors it's a lot easier to reason about
this thing if you use a pre-increment
but it's basically where c is
setting the the value whether it does it
at the top of the loop or the bottom
um so let's just or i'd say like either
just use this
or all or this is fine you know like if
you want to use your plus
equals one or spell it all out and say
you know like that um those are
clear okay it's it's clear what's
and it's pretty portable across all
different c implementations
pre-increment post increment they're a
little bit funny so
okay um i'm gonna just
punt on that for now we're not gonna get
too far but like you know so
we can we can use this plus equals if
you're used to that in python
yep all right so we run that i really
i really like the the four string
setup with the three the three
conditions because that's actually
that's really really nice because one of
the things that i've run into
in python is certain situations where
you want like a for loop but really you
want that ability to have like a more
yeah while loop condition to terminate
it's funny that you say that because
python explicitly doesn't want you to
write this
ever this is the first thing i like
c they were trying to get away from this
for exactly the reason that we did the
first time we ran this loop
it's very easy to read into the wrong
memory space
it's very easy to be off by one it's
very easy to write into memory that's
not yours
and okay you can count on the operating
system catching it and crashing for you
or in older operating systems that
didn't have protected memory like
they didn't catch it you know you just
ended up screwing someone else's program
up oh geez okay all right that makes
sense because python is definitely i
think more
hand-holdy in that way but they were
trying to cut down like language-wise
how do you cut down
off by one errors don't give the
programmer a counter
you know which is why like the default
four in python is kind of like you know
four like uh x in
range five and that means you're going
to get
five okay don't don't ask what they are
because they're going to be
zero through four but you know like
you're only going to get
five entries okay
um so that was them trying to like
prevent you and then if you really
needed to see which
loop you were on they added this
enumerate later so that
you could say like okay like oh yeah
like i mean so
that way you're not doing the math and
you're not doing the iteration stuff
they're trying to pull that
out of the language but c gives it to
and when your program breaks you know
just be careful it's one of those things
about c
okay so that's all making sense so far
yes oh yeah
yeah all right so we've got our basic
four we've got a basic while
math works pretty i'm i'm punting on
math but math works basically the same
way that you think like you know
if i want i to be you know
10 times 47 it'll do that i can
divide i can multiply i can't do this
this is not portable c
so i can't say 10 to the 47th okay how
would you do exponents
uh well you need the math library oh
okay well
because nothing's free i'm learning that
so there is a function in there which is
called pow which
should be in section three of the manual
uh i kind of like that pow yeah
pow is what we think and i'm just
pulling up on stream here but
you can see that like pow and again
you'll notice everything is defined
right so i have a return type i have the
name of the function
and i have whatever parameters it takes
and pow does
power so okay you know if i want 10
there are floats i was going to ask oh
yeah floating point math was like
invented around the time of c
like well later well i didn't know if
this i was about to say this
like this yeah no they're they remember
that yet they definitely are floats so
we can we can have our constants like pi
i don't know you want to give me a few
digits how's your pie oh god 3.14
i was just honest that's my pie i was on
a tear today
about like tao and the politics of pi so
i'm not going to get back into that
we're just going to go with our 3.14 so
by the way that is defined in here there
is there there are constants for
for that available but they're not
they're they're in hard to find places
um so this and they're not portable
they're not part of the standard c
includes nothing in the standard that
doesn't absolutely have to be there
like the whole design of the language is
like super super minimal
so everything that you want to do like
that would be reasonable like
you want to print out numbers and turn
them into strings those are added in
like in stdio or you know math
operations that are complicated they're
going to be in the math
like lib or and you know if you want
different math like if you want the
fastest fourier transform in the west
oh my god so we're gonna
be really happy we're gonna get to the
fastest fourier transform in the west
later but
um so i can have a float here and the
only real thing that i have to watch out
now here we were sort of like writing
out like
piece at a time so i'm just going to
comment all that out
so we'll leave it there and
let's say we wanted to just specify like
i want to see some fl so if i wanted tau
is i use this for my format specifier
for float and i think that's the same
in python and there we go even though
floats are actually doubles in python
which can't be d because that'd be an
okay so how do i get a double
well we'll get to that in a moment but
um yeah i can cast it
so i can do this and i can say like all
right give me pi
except i don't want pi i want tau so i
can do math and i can say
give me 2 times
pi and we'll just throw some extra
parentheses in there
so the order of operations are clear
and i get a nice error because
it's oh right i left this illegal line
in here this
showing that you can't do that for
um all right and i can tap okay
which is cool um so
yeah i mean you do have the pow function
that we just briefly touched on so like
and it returns a double but you can cast
a double to
a float you'll just lose some precision
so i can say like if i want 10 to the 47
i can do
that and you know i'll get a nice big
if i actually want to output that
because you know pow actually returns a
and a double is double the size of a
one way i could do it is i could
actually just change the type on pi to a
wait hang on hang on hold on so we did
we did is that
that's 10 to the power of 47.
yeah so like python gives you that
so why is it not one with 47 zeros after
ah floating point math okay i haven't
done a lot of it so i was like
i don't even see floating point in
precision i'm like yeah it's
it's right there
because ieee 754 you know
sucks i mean it's like it doesn't
it intentionally doesn't have like you
know decimal type math like if you
wanted a precise answer you'd have to
use like a big decimal library
okay like floats are literally floats
like i mean
it you know it floats typically
implemented as like
you know 32 bits so the same size as an
integer in its original implementation
or you could be intel about it and go 80
bits and you know
have fun with the extra bits in your
register when you change them
but if you want a little more precision
you can bump it up to double
um the downside is that here i'm i'm
turning it back into a float when i'm
outputting it
like back on line 20. yeah so if i want
to see that
as an actual double and if i want to
tell printf
no it's not a float i want the whole
double then
that's a long float
okay um so it's like use the float
formatting but make it really big is
kind of what the l
is for um and yeah i still get
you know this was actually giving me
double before but
okay so um that is my double type but
you know you you actually can you know
in it
well this is kind of a fun example
but um we can actually before we
uh touch on that real quick there is one
more type that we're probably going to
be playing with which is
actually what that comes from which is a
okay and along is really now in the
original standards
these things were a little bit
non-specific and that led to
trouble for c programmers through the
ages so
like in the original standards like
because the problem is like different
machines had different sizes
you know so like how how big was like
you know a character
well in modern c it's eight bits so that
gives you 256 values and that's exactly
the ascii space so when i was looking at
ascii before
like you know i've got there's this is
seven bit ascii the upper
the upper um 128
entries are reserved for well
other stuff you know ask he's like we
only need seven bits to
encode this stuff so it's a seven bit
encoding but everybody tried to stomp on
top 128 places but that's sorry getting
a little bit esoteric
um so they they said well you could have
a seven-bit character
you know and most of the standards said
no that's crazy like we want powers of
two so let's go with eight bits
so character is eight bits um and it
still is
so i can have something like you know
care x
b oh there it is sorry i had a delay so
i was like where am i looking but no
okay go ahead
cam by the way nice to have that
microcontroller um
so this care x is like
you know kind of a fun oh did i lose
oh yeah sure um
yeah th this th this krx basically like
that's that's your just normal like
eight bit value
um int was a long was defined as a four
byte value
so like you know in the case of like
long j
4 this was kind of like you know
this would be a nice long number there
was a short
which was which is not used that often
anymore and
it was defined as sort of half the size
of a long
okay so i can i can do that down in
these numbers they're all sort of the
same size
but these days this is typically 16 bits
this is typically
um 32 bits 4 bytes um
so a long is four times the size of a
some platforms like if you're on a
64-bit platform long would be 64-bits
that led to a whole bunch of
standardization problems because
if you are running on an 8-bit platform
or if you were running on
a 60 you know a 64-bit platform
those were different sizes so you had to
know where your program was running
okay but yeah that makes a lot of sense
it's kind of like a you know it's a
reference and so it depends on this
yeah and int was defined as somewhere in
a long and a short you know so somewhere
in between yeah it was it was just
the literal standard said any convenient
for an integer so
in was horribly non-standard like so
you're gonna see all sorts of like
obfuscated c syntax because
sometimes you care what the size is and
pretty much every other time you're
going to have some sort of portability
issue between platforms so
like you better care like so nowadays
you'll see stuff like you know the new
types which
which we'll say like okay and that's
literally saying like i want this thing
to be a number but
it's only eight bits or i want this
thing to be a number but it's 16 bits
or i want this thing to be a number but
it's 32 bits and that's kind of nice
because it's explicit so
standards usually use these types but
the old c
would use these types and they would
change on different platforms so
if you're looking at like really old
code like for example say you like
png images right okay
and you wanted to open up the code for
libpng which is c
by the way all right you might notice
in the code is a whole bunch of
defensive checks about we don't know if
we're running on a machine with 12 bits
as a byte or 8 bits as a byte
or 24 bits as a byte so all the code is
in there to handle those different
really interesting
yes that's why like later on they said
this is just sort of nonsense so like if
we really want to declare memory and say
like this thing is 16 bits
then just say it's 16 bits and leave it
up to the platform to define
this to be the right underlying type so
like gcc does this for you
this is like the gcc extensions
but you know this case like uint16t is
actually going to turn into
a short because it's actually on this
platform it's 16 bits
that's really interesting though okay
well and that's also
where we're going to kind of wrap up so
um is that making
sense i mean types are this i think the
hardest thing
for somebody coming from python to start
to recognize is that
this stuff doesn't happen for you
like this is literally something that
you have to spell out like
this is the memory it's going to be this
is how much location size i'm going to
this is where this thing's going to go
and everything has to be sort of spelled
out there are some convenience routines
like we ended up using here with
a string you know so we didn't actually
have to specify the
the null byte at the end and yeah let me
tell you right now
most security exploits are related to
to to c code where people didn't get
like convenience
yeah well not not the convenience
routines they're usually safe but
later on they're operating on this
string and they're doing something like
okay i'm gonna have a buffer here and
it's 256 bytes
okay and then i'm going to put some
stuff in the first characters and i'm
going to forget that the null byte
counts as a character and then
boom buffer overflow
that's really interesting i mean i could
i could totally see
i mean maybe people who've been doing
this for for years and years and years
that kind of
is an intuition but i can definitely see
myself forgetting that for sure
especially coming from python
um just looking at the chat real fast
they're pointing out that um
i'm cheating a little bit because i'm
not talking about the signed versus
unsigned types
all of these things that are up here
that i just wrote out
these are all signed types so
they all have a sign so i can have you
know negative numbers here
i can you know whatever um if i actually
want th
there's unsigned versions of all of them
so i can
have an unsigned you know that's
actually an unsigned int but
unsigned counts as an in and that would
be this would be
a fun line the compiler would probably
say well i'll do it
but this thing is going to not be what
you expect so
i'm trying to avoid a little bit of
complexity there uh chat so
we'll we'll get more into signed bits
next time but you are right um
the joy of streaming is trying to
pronounce people's names
yeah no i mean it's absolutely right but
yeah so like the actual literal uh i
should be correct because you're
learning this kind of for the first time
um yeah the equivalent like if i wanted
a short well
i mean this particular in would be a
uint um
if i had it as unsigned
unsigned this this would show up as a
32 typically
modern standards so like this these two
would be equivalent
okay the unsigned version of that would
like that or sorry that's the signed
version of it so both of these allow
negative numbers and that's
kind of what's going on so a little bit
of flair but i do want to
point out that wiger is correct
all right so all made sense you ready to
library authoring and compiler work
good um we're gonna be doing a little
bit more with um
well we're gonna we're gonna get down
and dirty with strings and um
some i o processing next time because
we've got to kind of get you the point
where you can sort of see what's
as you're transforming things um and
then after that well we have a whole
bunch of stuff
on the horizon i'm so excited this is
this is really fascinating and i mean
it is it it's that kind of com it's how
to put it it's the um
the fact that it it's making you be so
explicit that i feel like is gonna make
a better programmer it will just like be
a lot more
you know i feel like so honestly i feel
like python is is
the perfect thing for an astronomer
because we're all about just like
order of magnitude estimations and so
it's like you know
it's fine everything's a sphere and we
can just ignore it yeah
it's mostly spherical magnitude close
and so i feel like it's very like
pythonic in a way and it's
going to make me a better a better
programmer just like maybe being more
explicit in some ways might make you
more accurate
totally i mean and you're absolutely
right c is
fairly unforgiving um it and that's
mainly because it's really just a thin
abstraction on top of the metal like the
underlying computer program
is pretty close to what you're writing
especially if you turn all the
optimizations off
like if you if you look at the assembly
output like it'll
comprehensibly translate into these
we'll we'll do that at one point in the
future but it will it will
make you better because it will show you
literally like what the computer's doing
it does force you though on the flip
side because it doesn't really do
for you there's no magic it forces you
to really be explicit and
to think about like all those different
things so like one of the things like
maybe with c you probably i mean who
knows what you're going to use
you know in your life but it is one of
those things like it will make you
better in any other language because
you'll kind of know what the computer is
really doing
and you also even in python if you never
use c
you'll just be like i'm really glad i'm
writing python
and i'm not dealing with any of that
stuff i
have a python project that i'm working
on right now that i was gone after this
and i'm like i am so
appreciative like every line of oh
i'm so glad it's python well
thanks for coming to the show and thank
you everybody for
showing up and wilger for finding my
signed uh
issues i appreciate that um and
yeah like i said the tool is evolving so
you know as soon as i get like you know
some error output and some more
stability and the thing like
isn't a hot mess like it is right now
i will get that open source so it will
be available for anybody who wants to
use it for you know learning sessions um
and jess thank you very much for joining
us thank you i mean this is
amazing and i really appreciate
because it's very very interesting and
i'm excited to learn more
i think i just lost part of that but it
sounds like you're excited
i'm excited yeah okay
yay that's basically what i meant all
right are you uh are you gonna be
streaming now should we come
raid you yeah i'll need like uh like
maybe five minutes ten minutes
but um i'm gonna be not not coding in c
i'll i'm gonna be working on python but
yeah um i don't i don't know if i can
well yeah i guess i can't
i'm like a noob with twitch so
me too don't worry why don't we just put
a link to your channel in the uh
do you uh can you type in this stream
yeah um so
all of you who don't know um jess is a
astrophysicist and she does all sorts of
interesting things
with um the sky
basically my terrible description
it's like see yeah you do this thing
with the computer right
yeah okay but anyway it's in chat so
check it out
and she'll be on in a little bit thank
you all right
great well have fun everybody and thank
you jess
of course bye guys bye