[CS61C FA20] Lecture 03.4 - C Intro: Basics: C Syntax — Transcript
Full transcript
- 0:00and welcome back now let's take a look
- 0:02at some c
- 0:03syntax to actually teach you more about
- 0:05the language that we haven't already
- 0:08so the first is we talked about before
- 0:10that there's a new way in c99
- 0:13to have a boolean type but if you don't
- 0:15include that you just have to go with
- 0:16kind of more ansi c
- 0:18what is true and what's false so what's
- 0:20false in c is
- 0:21zero only zero zero is false
- 0:24that's it turns out null is actually
- 0:28defined as zero so if you say a pointer
- 0:30i got a pointer here and if i set the
- 0:31pointer to null which means it's not set
- 0:32to
- 0:33pointing to anything that really means
- 0:34the pointer is set to zero
- 0:36um and i mentioned before that standard
- 0:38bull.h defines official boolean types if
- 0:39you want to use that
- 0:40what's true everything else this is a
- 0:43little different than python because
- 0:45python has kind of
- 0:46what what false is for every type so
- 0:48empty string is false for
- 0:49strings zero is false for numbers uh
- 0:53you know empty sequences falls for other
- 0:54things and anything that's not that is
- 0:56true
- 0:57in scheme it's actually closer to what c
- 0:59does because scheme actually has hashtag
- 1:01f is false and everything else
- 1:02even zero is true for scheme even m this
- 1:05is actually a
- 1:06this is actually a a matter of debate
- 1:08you want to get a scheme person riled up
- 1:10ask them whether the empty list is true
- 1:13or false
- 1:13some people say it's not hashtag f some
- 1:15people say no no empty list it should be
- 1:17false also so there's a lot of
- 1:19get get two people who are you know old
- 1:21scheme folks together and have them
- 1:23fight over whether
- 1:24nil nil is a is false or not it's kind
- 1:26of a fun fun game to do
- 1:29so in terms of variables you've got to
- 1:32have type so you know from java you have
- 1:33types
- 1:34you have to declare what type a variable
- 1:36is before you use it and you can't
- 1:37change it it's locked in for the life of
- 1:39the program
- 1:40types can't change so here's an example
- 1:42of
- 1:43some of the common types people are
- 1:44using you saw ins for integer numbers
- 1:46negative numbers as well
- 1:47you might say well how big are they
- 1:49we'll show you a slide how this is all
- 1:50ambiguous
- 1:51and this is why we added in types which
- 1:52declare exactly they are eight bits wide
- 1:54or 16 bits wide or etc etc
- 1:57unsign ins for unsigned numbers floating
- 1:59point for floating float for floating
- 2:00point numbers
- 2:01we'll actually have a whole lecture on
- 2:02how how that actually gets stored that's
- 2:04a really cool
- 2:05lecture to see doubles for bigger
- 2:07floating point numbers
- 2:08uh characters for single characters for
- 2:10you know axi characters
- 2:11uh longs for longer in so i got an in
- 2:14but i need i need more space for
- 2:15for a two's complement value i do a long
- 2:17i need more space than that it's a long
- 2:19long it's like let's just keep writing
- 2:20the word long in front of things when
- 2:21it's almost like you receive a file you
- 2:23save a file you're working with a friend
- 2:25or something
- 2:25back in the days of sneakerwear when you
- 2:27had a file you called it final and then
- 2:29you give it to them and they make a
- 2:30change they call it final final
- 2:31it's like long long that's a big little
- 2:33long that's what it is um
- 2:36python versus java versus c so um
- 2:40interesting thing about c is see they
- 2:43decide that
- 2:44int feels like the the most efficient
- 2:48way you would store a two's complement
- 2:51number
- 2:51so if here's an example if it's a 32-bit
- 2:54machine
- 2:55which means the data kind of is the
- 2:57whole data path is meant for 32 bits
- 2:59then probably it's going to be 32.
- 3:00if it's a 64-bit machine built so that
- 3:03bits are what all
- 3:04the everything is floating around in
- 3:05that way that's the width of the data
- 3:07path
- 3:08then probably uh ins are going to be 64.
- 3:10it's really what's called the word size
- 3:11which is the width of
- 3:13the majority of the data you're flowing
- 3:14with um
- 3:16here's what i don't actually don't like
- 3:17this slide so much because you have to
- 3:18remember i don't want you to memorize
- 3:19these things i mean
- 3:20the only guarantee is that a long long
- 3:22is the size of
- 3:24size i'll try i'll show you in a second
- 3:25what sizeof does it tells you how many
- 3:27bits wide this thing is um actually how
- 3:30many bytes wide this thing is sorry how
- 3:31many bytes wide this thing is
- 3:33um so if it's 32 bits the size of we'll
- 3:35say four okay just remember that then
- 3:37okay so the only guarantee is that size
- 3:39of a long long is bigger than equal the
- 3:40size of a long bigger look at the size
- 3:42of an inside basically
- 3:42the size of a short but you don't know
- 3:44other than that they could all be 64.
- 3:46they could
- 3:46i mean it's a little crazy um i mean i
- 3:49guess long has to be bigger than 32. i
- 3:50just don't like this i'd like to use in
- 3:52types and declare
- 3:53i need to know i don't want to be i'm
- 3:54going to be guessing i don't have code
- 3:56that runs on one machine
- 3:57and i move to the machine and because of
- 3:59some way i understand things it doesn't
- 4:00run anymore no
- 4:01i don't need to know how wide these
- 4:02things are i think in types we should
- 4:04always be using
- 4:05they could all be 64 bits but i i would
- 4:07prefer you and encourage you very
- 4:09strongly to use in types as much as you
- 4:10can
- 4:11it gets a little annoying how you printf
- 4:12and scanf values within types is a
- 4:15little bit annoying if you use macros
- 4:16for that
- 4:17uh not macros but constants for that
- 4:18it's a little annoying
- 4:20but do it it's cleaner uh and you'll
- 4:22have code that then migrates really
- 4:23cleanly you'll have it work so which
- 4:24means
- 4:25you want to live in a 32-bit world even
- 4:26a 13-bit world if you move to 64 it
- 4:27won't just
- 4:28change size integers just to be 32
- 4:30because you said explicitly i want this
- 4:31into be 32 bits wide
- 4:32great um his little comparison about the
- 4:36three languages python java and c
- 4:38of the size of what an int is uh python
- 4:40there are at least 32 bits java they are
- 4:42exactly 32 bits which is interesting
- 4:44and c depends on the computer in the
- 4:46early days ants were 16.
- 4:48it was a big deal when it became 32
- 4:50because the machines back in the day had
- 4:51a data path and a word size it was 16
- 4:53bits it was two bytes wide
- 4:54so c has changed and evolves his old
- 4:57language had that
- 4:58and i don't like the idea that it
- 4:59changes use in types and lock it in
- 5:02you can also turn the lock we're talking
- 5:04about locking things in you can also
- 5:05lock in the value of variables
- 5:06um if you have a variable that you know
- 5:08is going to change it's going to be a
- 5:09constant it is reference value it's the
- 5:11golden ratio it's the days of the week
- 5:12it's
- 5:13you know it's the speed of light it's
- 5:15the law lock that in
- 5:17and say constant so if you add constant
- 5:18in front of it it says the value won't
- 5:19change if you try to change it your
- 5:21compiler will say hey you can't change
- 5:22this const
- 5:22very useful so you can buy those like
- 5:24your friend in that way so if you know
- 5:25something's not going to change
- 5:26lock it in as a value as a constant an
- 5:29enum is a nice
- 5:30feature of the language it lets you kind
- 5:31of have an enumeration so enumerated
- 5:33constants it gives you
- 5:34each a value so if i say enum color
- 5:37red's gonna be zero color
- 5:38green is gonna be one and blue's gonna
- 5:40be two if you ever use an enum i
- 5:42strongly recommend
- 5:42you not peek under the hood to find out
- 5:44what those bits are of each one of those
- 5:46i recommend
- 5:46you always not know your code should
- 5:48work even if they were to rearrange
- 5:50the numbers of those guys if i would say
- 5:52go back to your code and change
- 5:53to be blue red green then each of those
- 5:56will be different values your code
- 5:57should still work
- 5:58type functions in c so
- 6:01all the functions that you declare have
- 6:03to have a return type
- 6:04uh they need to you need to explicitly
- 6:06tell them in advance usually above the
- 6:08function
- 6:08what its expected return type is and
- 6:10then when you're actually using it
- 6:11you'll
- 6:12oh i get it your it can match them up
- 6:14and try to do some kind of matching to
- 6:15say well you know what you're
- 6:16you told me it returns an int but you're
- 6:18actually using it as an unsigned end are
- 6:19you sure you want to do that
- 6:20and so it'll give you a warning or give
- 6:22you an error that which is nice you can
- 6:23also say look this is supposed to be a
- 6:25command this would be something that's
- 6:26supposed to
- 6:26do have a side effect so the return
- 6:28value can be void meaning return nothing
- 6:30um and variables and functions need to
- 6:32be declared before they're used here's
- 6:34an example of number of people and
- 6:35dollars and cents is kind of declared as
- 6:37ins and a float
- 6:40a struct is the way to make abstract
- 6:42data types you can have
- 6:44a series of fields this feels very much
- 6:46like python uh where you have a you know
- 6:48a class and you have
- 6:50dot fields same idea but you have a lot
- 6:53more control about what the bit widths
- 6:54are of that
- 6:55uh for the most part um so the other
- 6:58thing i want to explain to you is
- 6:59typedef
- 7:00allows you to declare a new type so you
- 7:02know the types we talked about are
- 7:03floats and ins and et cetera and cares
- 7:05you can make a new type that's pretty
- 7:07cool so i say you know what
- 7:08i want to bite i know a bite's going to
- 7:10be eight bits so i'm going to say u and
- 7:11eight underscore t as byte and now i can
- 7:14just say
- 7:14byte b1 and b2 that's really cool so
- 7:18that's really useful
- 7:19um the way you can do it a struct is to
- 7:21say i have a structure
- 7:22it's going to have two parts to it it's
- 7:24kind of like a ball of two pieces of
- 7:25data
- 7:26together here is uh what's a song what
- 7:29would a song be well the
- 7:30length in seconds for how long the song
- 7:32is and the year is recorded that's a
- 7:34structure
- 7:35two wins together i call that a song and
- 7:37i typed it as a straw
- 7:38as a song and now i can just say song
- 7:41song one
- 7:42and that thing that's going gonna have
- 7:43two fields how do you get to the fields
- 7:45dot notation just like python so song
- 7:48one dot length in seconds set that one
- 7:49song one dot er recorded do the same
- 7:51thing for song two so pretty cool so
- 7:52structs are kind of cool
- 7:53um we'll talk about them more a little
- 7:55bit later when we talk about pointers
- 7:56and how to access those fields
- 7:58again most of this is from c and java
- 8:01being the same
- 8:02within a function it's remarkably close
- 8:04in terms of its syntax
- 8:06if looks very similar you've got an if
- 8:08by without an else
- 8:09you get an if with an else but that's it
- 8:11you don't have an l if like you have in
- 8:12python
- 8:13um and you'll notice that sometimes you
- 8:16don't have to have if you only have one
- 8:18statement you don't have to have
- 8:19brackets around it that's great except
- 8:22when you
- 8:23think it's indented so you're thinking
- 8:24what's python right because you're in
- 8:26python mode it's indented it must be
- 8:27part of the if
- 8:28and you don't put brackets so you have
- 8:29one thing that's right but then you put
- 8:31a second thing it turns out that second
- 8:32thing even though it's indented is not
- 8:34part of the if because if without
- 8:35without brackets
- 8:36only takes one statement after it one
- 8:38semicolon that's it so you think it
- 8:40looks and that's a huge source of bugs
- 8:42for new programmers where i don't use
- 8:43the brackets but i have two lines in
- 8:45there
- 8:45and i think they're both you know
- 8:47indented so they're part of the if
- 8:48they're not part of the f
- 8:49you need to have brackets so get used to
- 8:51always putting brackets around your if
- 8:52even if you only have one
- 8:54one line i don't think we've done it
- 8:55always in this code that we're going to
- 8:57share with you because we had to pack it
- 8:58tighten the slides but try to do that
- 9:00just in fact most editors will actually
- 9:01add the brackets anyway for you if you
- 9:03go into sublime we'll do it
- 9:04for you so get in the habit of putting
- 9:05the brackets around it we also have
- 9:07while and
- 9:08do while do the statement while this
- 9:10expression is test so
- 9:11if you want to test it before test it
- 9:13before you go into the
- 9:14the set then that's the while if you
- 9:16want to do it before you do the test
- 9:17test after use the do it's kind of cool
- 9:21four looks very similar for
- 9:22initialization a check and an update
- 9:24then a statement
- 9:25you have a switch statement which says
- 9:27here's an expression and then i have
- 9:28this
- 9:29cases and if the cases match you do
- 9:31these statements and here's the problem
- 9:34you have to have a break statement
- 9:37after each of the cases so case uh case
- 9:40it's uh uh one
- 9:41let's just say hey maybe switch on a
- 9:43letter you type in some letter
- 9:45case q because they want to quit well
- 9:47then do sorry you want to quit
- 9:49sorry please you want to come back if
- 9:51you don't have a breaks
- 9:52a line below that last case it'll roll
- 9:55into the next case statement you have to
- 9:56have a break after each of these cases
- 9:58in there
- 9:58so that's actually important people have
- 10:00done this before where they'll just roll
- 10:02through like why is it running both of
- 10:03them yeah because it's
- 10:04case kind of jumps into it and then it
- 10:06just rolls until it sees a break and
- 10:07that's a problem
- 10:09there is a default that says look it's
- 10:10everything else but the cases have cases
- 10:12but the cases are checked through the
- 10:13default case c also has a strange syntax
- 10:16called a go to
- 10:18do not use a go to at least in 61c go to
- 10:21is reserved for
- 10:22kind of expert c programmers who know
- 10:24what they're doing you can basically
- 10:26make c look like basic where you go here
- 10:28and you have all these labels and you
- 10:29can go there and go there and go there
- 10:31and it's
- 10:32it's it's it's non-local control flow
- 10:35which means jump around just randomly
- 10:37don't do that it's like the worst code
- 10:39you'll ever have
- 10:40so don't use a go-to until you get to
- 10:41have like until you upgrade to badge
- 10:44level 17
- 10:45of the badges you've earned for
- 10:46programming at c it's bad it's bad case
- 10:48but he has a go too so you might see
- 10:50some go-tos and some
- 10:51code that you might inherit we're going
- 10:53to end this this lecture with a look at
- 10:55actual c code stop talking about the
- 10:57format actually show me how this works
- 10:59well let's read it together and in fact
- 11:01i have
- 11:02i think i have this set to be a pen
- 11:06check this out so i can actually
- 11:07highlight each of these lines and we can
- 11:08talk about them
- 11:09so you saw the easiest of my includes of
- 11:11that that's nothing new before let me
- 11:13make this white just
- 11:14just to give you some juice here all
- 11:17right
- 11:17so we declare some we declare some
- 11:21values
- 11:22angle do this is this is kind of old
- 11:23school ansi style because i'm declaring
- 11:25it at the
- 11:25top before i actually really use it i
- 11:27could have declared it just at the
- 11:28moment
- 11:28i literally could have declared it int i
- 11:30could have said int
- 11:32right here in front of angle degree and
- 11:33i wouldn't have to do any more than that
- 11:35so a lot of people declare it locally
- 11:36rather than here but this is like more
- 11:38old school style to do this let me clear
- 11:40that up
- 11:41so we can take a computer table of prime
- 11:44of sine of sine function okay so first
- 11:46here's pi pi is going to be a double
- 11:48that means it's a float but it's a
- 11:49little bit more space for than that
- 11:50we'll talk about the details in a slider
- 11:52in a lecture or two
- 11:53um i set uh a tan eight hand is in
- 11:57math.h
- 11:58so i could just get access to that you'd
- 12:00be like well dan you mean in python i
- 12:01used to have this dot notation if i
- 12:03import math i have to say math.atan
- 12:05nope all of those in the same shared
- 12:07namespace welcome to one namespace
- 12:10have a good time um
- 12:14i'm going to print this out print what
- 12:15pi is i'm going to print out this little
- 12:18table header that's
- 12:19nothing special there and now here's my
- 12:21first in my first actual
- 12:23control structure while angle degree is
- 12:26less than or equal to 360.
- 12:29that's fine um you never this is like an
- 12:32fyi i'm going rogue a little bit you
- 12:33never want to have one of these test
- 12:35cases be based on a floating point value
- 12:37because the floating point value
- 12:38sometimes can be 0.001 sometimes 0.99999
- 12:41and that means the number of iterations
- 12:42can actually be off by one so you always
- 12:44want to have kind of a while loop be
- 12:46checking out an integer value not a
- 12:47floating point value this is an
- 12:48important thing
- 12:49so i'm doing this while which is on
- 12:51angle degree and angle degrees in int so
- 12:52yay thumbs up on that one
- 12:54so what the angle radian is a little
- 12:56expression say what the value is call
- 12:58sine i print this out the value of angle
- 13:00degree and value
- 13:01i increment angle degree and i stay in
- 13:02my loop until i'm done return zero
- 13:04so pretty simple piece of code but
- 13:06that's an actual working piece of code
- 13:07you can copy and paste this and have it
- 13:08run and print little sign tables in fact
- 13:10there's my sign table that's what you'd
- 13:12get there's a little pie and some little
- 13:14angles and signs kind of nice and it
- 13:15goes on till 360 degrees all right
- 13:17that's c we're a little lot more but
- 13:19we're kind of getting our hands dirty i
- 13:21love it enjoy by the way grab
- 13:22you better be if you haven't if you've
- 13:23been watching up to now you better go
- 13:25grab your editor and copy this stuff in
- 13:27come on i told you that let's have some
- 13:28fun we'll see the next video
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 03.4 - C Intro: Basics: C Syntax by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,245 words across 487 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.