[CS61C FA20] Lecture 03.3 - C Intro: C v. Java and C Syntax — Transcript
Full transcript
- 0:00and welcome back since so many of you
- 0:03have come from
- 0:03a java experience in 61b or some from
- 0:07another community college or from
- 0:08another university
- 0:09let's actually show you that you
- 0:11probably already know most of what you
- 0:13need to know for c
- 0:13there's a couple of details we'll spend
- 0:14a lot of time talking about the
- 0:15differences but let's talk about some of
- 0:17the similarities as well and
- 0:18do a comparing and contrast kind of
- 0:20thing so here's a beautiful table
- 0:22uh java is an object-oriented language
- 0:25you know that c is mostly a function
- 0:26language you can do some object stuff
- 0:28but really that's what uh c
- 0:30plus plus was for so see the idea the
- 0:32core id
- 0:33is a function the job of the core idea
- 0:34is is uh is a class or an abstract idea
- 0:38abstract data type you can make apps
- 0:41data types in c
- 0:41but it's a lot harder to do we haven't
- 0:43it's not as clean and simple it's just
- 0:44screaming you here's an absolute data
- 0:46type boom there's a factory
- 0:47instance that's a new object abstract
- 0:49data type harder to do in c unless you
- 0:51really do something to
- 0:52really do it right the way you compile
- 0:54in so i guess i mentioned before java is
- 0:57compiled java c
- 0:58hello.java creates this independent and
- 1:00machine independent byte code
- 1:01the way you compile c is gcc hello.c
- 1:04that could create some machine
- 1:05independent code machine language code
- 1:07machine dependent code
- 1:08c is the machine dependent java java's
- 1:11output java c's output is machine
- 1:12independent code
- 1:13the way you run it is you just have to
- 1:14say java hello on the java side you say
- 1:17that out and it out loads it into memory
- 1:19and then runs the program that's the
- 1:20more details of what the loader does
- 1:22we'll talk about that
- 1:22but 8. out is where you run the program
- 1:24by the way there's a way to specify the
- 1:26name of the output file
- 1:27if your source code is hello.c
- 1:30dash o put the word put the name of the
- 1:33executable i don't like to see any 61c
- 1:35student ever using a dot out they should
- 1:37say
- 1:37gcc dash o hello hello.c
- 1:40if it's just a simple program like hello
- 1:42world or something fun very small
- 1:44the point is you shouldn't be having it
- 1:45out to name your executable then you'll
- 1:47have
- 1:47if you have ten active deals what do
- 1:48they all do i don't know let's run it
- 1:50all of a sudden
- 1:50the robot starts moving and this you
- 1:52know printer starts no name your
- 1:54executables you know the heck that these
- 1:55things do
- 1:56the picture on the right is the you know
- 1:58you get to job the reason i don't like
- 1:59java as a first language in apcsa
- 2:01i advocate for using a blocks language
- 2:03or something else
- 2:05python would be great for for apcsa in
- 2:07high school is
- 2:08you have to teach the kid on the first
- 2:09day all the elements of the picture in
- 2:11java it's a little crazy
- 2:12and c the amount of things you need to
- 2:13teach them is not much you know there's
- 2:15this library that you're including that
- 2:16has a standard library standard io
- 2:18libraries you can have printf and some
- 2:19other things
- 2:20main we'll talk about the details of the
- 2:22main program but main
- 2:24void just says i'm not going to use any
- 2:26arguments for now just main
- 2:27i print f you can't you don't use print
- 2:30print f
- 2:30uh quotes you have to specify a new line
- 2:33not a print line for java gives you a
- 2:35free new line
- 2:36you have to actually specify the new
- 2:37line yourself in c and
- 2:39you have to return a value main returns
- 2:41a number we'll talk about
- 2:43functions returning this particular
- 2:44value and declaring that but main
- 2:45returns an integer
- 2:46and so 0 means success just as a little
- 2:49fyi
- 2:50zero is always success in unix because
- 2:52there could be a lot of ways things fail
- 2:54but only one the way they succeed
- 2:55so they decided well zero is going to be
- 2:57the success number and any other number
- 2:59might be something didn't go wrong it's
- 3:00kind of fun to see that
- 3:02so that worked so the return zero was
- 3:04the right idea when it worked uh if you
- 3:05didn't work return some other number and
- 3:07that would be a special code that you
- 3:08and the person calling you might agree
- 3:09oh if
- 3:10it's if it's seven that means file is
- 3:12not found if it's 12 that means
- 3:13i couldn't find the printer or something
- 3:17storage this is what differs this is why
- 3:20i love to t61c in the third class not in
- 3:22the first or second class because
- 3:24you want to be able to have layers of
- 3:25abstraction one of the layers of
- 3:26abstraction you have in java is that it
- 3:28manages the memory for you
- 3:29you say new it just works it handles it
- 3:32by your garbage collection hopefully you
- 3:33learned that in 61b
- 3:35you have to do this all yourself so all
- 3:37the
- 3:38kind of all the safety belts all the
- 3:41padded rooms are gone you're doing all
- 3:42the memory management yourself
- 3:44you can get in trouble another
- 3:46difference
- 3:47you've got c in java in terms of
- 3:49comments so this is old school and cc
- 3:52uh but in something we'll share with you
- 3:54c99 within a couple of slides
- 3:56talks about that actually they allow you
- 3:57to have the same comments as java so for
- 3:59now
- 4:00java and c are the same in comments if
- 4:01using c99 or later
- 4:04you have to say final if it's if it's a
- 4:05constant in java and pound define or
- 4:07const for a variable that's not a
- 4:09pound-defined global variable
- 4:11in c do you have a preprocessor yes you
- 4:14do in c
- 4:15you do not in java so we talked about
- 4:16that a moment ago
- 4:18you have to have a definition the
- 4:19beginning of the block and c in old
- 4:21school and cc
- 4:22but in new schools c99 and built and
- 4:24beyond you don't do it you can declare a
- 4:26variable right before you use it just
- 4:28like you can in java
- 4:30naming conventions this is still not
- 4:32clear there there are lots of people who
- 4:34have different naming conventions in
- 4:35java and instill in c as well in fact
- 4:38a lot of people it's not as clear python
- 4:41typically universally has the
- 4:42word with underscores looks like c but
- 4:44there's still a difference of opinion on
- 4:46how c is done so sometimes you'll see
- 4:47even our own projects will have
- 4:49um camel case or other different
- 4:50conventions but typically people have
- 4:52mostly agreed that it's the same
- 4:54kind of underscores separate words in c
- 4:56but actually most of the time
- 4:58seed what they do what they do is
- 4:59they'll come up with a
- 5:01short hand so rather than saying um
- 5:05let's see i don't know uh ascii
- 5:08ascii to integer you wouldn't write
- 5:10ascii to integer you say a-t-a-t-o-i
- 5:14so my point is sometimes the c
- 5:17the kind of old-school c is to convert
- 5:21the word into a really tight word like
- 5:22mv like unix commands like real that's
- 5:24the old school back in the 70s and 80s
- 5:26that's how they were thinking
- 5:27mvcp like a couple of letters for things
- 5:30so a lot of times a lot of
- 5:31the the words they should be more
- 5:33verbose and we've realized it should be
- 5:35more
- 5:35verbose but they're shorter in c
- 5:39the way you access the library is you
- 5:40import the module in java and you access
- 5:42the library access library with the
- 5:44pound sign include
- 5:45with either the curly list the angle
- 5:47brackets or the quotes
- 5:50finally most of the operators you see
- 5:52are identical you know
- 5:53assignment arithmetic uh augmented
- 5:56assignments you know plus
- 5:57equals minus equals there's a lot more
- 5:59than you probably haven't seen before
- 6:01uh bitwise logic that means operating
- 6:02the actual bits of things not on
- 6:04kind of logical values but like boolean
- 6:06logic below is
- 6:08the actual boolean value true or false
- 6:09and doing things with that but bitwise
- 6:11says work on the actual bits themselves
- 6:12which is nice it kind of works in
- 6:14parallel very powerful
- 6:15you can move things around shift them
- 6:16around with a bit by shift
- 6:18testing for quality is the same you
- 6:20group them with parentheses
- 6:21the relationships orders you can
- 6:23increment a decrement with plus plus and
- 6:24minus minus
- 6:26member selection we'll talk about this
- 6:27some more this is how you kind of get
- 6:29into the fields say of a struct we'll
- 6:32talk about that a little later
- 6:33conditional evaluation which we call uh
- 6:34the inline if or ternary if
- 6:36uh is the same as well has there been an
- 6:39update to the ncc that i
- 6:41picture i showed you the beginning yes
- 6:42there has been it's called c99
- 6:45so if you hear me saying c99 or beyond
- 6:47in 1999
- 6:48a consortium got together and says we
- 6:50gotta upgrade c he said all right let's
- 6:51do this before remember
- 6:52ansi was in ncc spot about 86 1986.
- 6:56so they decided to do the by the way i
- 6:58think i still think that they should
- 6:59have used uh
- 7:00why why why why i mean 99 we keep
- 7:03working with this for another 100 years
- 7:05we're like do you give me 99 in 1999 or
- 7:072000
- 7:08it's a little crazy that they didn't
- 7:09think about the y2k problem and i
- 7:10already tried to have four digits for
- 7:11their but they didn't
- 7:12remember this is still the very abridged
- 7:15way of naming things so 99 is fine all
- 7:17right
- 7:17i guess i'll be fine let's see how we
- 7:19maybe they'd say oh c won't last 100
- 7:20years who knows
- 7:21um the way you can find out what version
- 7:23of c you're running is this little print
- 7:24left if you say printf
- 7:25percent ld long decimal and you have
- 7:27this constant standard c version it'll
- 7:29tell you what number that is and that
- 7:30that is the number which is 1999.01 it's
- 7:33just a big number
- 7:34but that's the actual date of that the
- 7:36release of that version you see
- 7:38you're working with actually the
- 7:39compiler you're working with really
- 7:41references there's a wikipedia link to
- 7:42that here's some things that you'll see
- 7:44that are different from ncc so if you
- 7:46see old school c you're probably not
- 7:47going to see a lot of old school c
- 7:48probably the code did it but maybe you
- 7:49get a job that you see
- 7:50old ncc you need to know that's ncc and
- 7:53maybe
- 7:54hopefully you'll have a c99 or above
- 7:56compiler that you're working with
- 7:58but if you see old school c that's doing
- 7:59something weird you'll see that's
- 8:01because of these reasons that you had to
- 8:02have declarations at the top
- 8:04now you can have declarations anywhere
- 8:05thanks to c89 declarations can be
- 8:07anywhere java like comments we talked
- 8:08about
- 8:09this is a big deal boy all the code i
- 8:11wrote from 86 to 99 was so bad because
- 8:14you couldn't have a variable length
- 8:16array you had to have that be a constant
- 8:18and there's nothing you could say
- 8:19hey user how big a radio you want oh 100
- 8:21okay make an array
- 8:22we'll talk about what a raise are later
- 8:24then of a hundred you couldn't do that
- 8:25you had to hard code the number into the
- 8:27code itself you couldn't ask dynamically
- 8:29what the size something is in an array
- 8:30that was a big deal so that third bullet
- 8:32down i'm like
- 8:33very happy another thing you're going to
- 8:35see in a couple slides
- 8:36is how big are things how big is an
- 8:39integer
- 8:40well it used to depend uh and in type
- 8:43said
- 8:43enough with this depending it should
- 8:45just be clear you should be able to
- 8:46specify how wide integers are i think
- 8:48you've said that you've seen that before
- 8:49in the number representation you can say
- 8:50i want it to be 8 or 16 or 32 or 64.
- 8:53um so they have that so please use n
- 8:55types rather than in
- 8:56i recommend and standard bull lets you
- 8:58actually have booleans we never had
- 8:59booleans at all we always had zeros and
- 9:01ones
- 9:01and we now actually have booleans that
- 9:02you can use if you use the standard
- 9:04boolean
- 9:04library has there been an upgrade to 79
- 9:07yes there has been it's called c11 and
- 9:09now actually the newest one is called
- 9:10c18 which actually is mostly c11 which
- 9:12has bug fixes
- 9:13and here's the two numbers you get 20
- 9:162011 and 2017 for the longs
- 9:18there's references here now is just one
- 9:20reference for all c standard revisions
- 9:22you now have multi-threading
- 9:24multi-threading support which is amazing
- 9:26you now have unicode you really we
- 9:27talked about unicode the last series of
- 9:29lectures we can now support unicode in c
- 9:31that was a big deal
- 9:32get-ass was a huge security hole for so
- 9:34many years they got that out of there
- 9:36and some more some more things that are
- 9:37not so complex values if you care about
- 9:39that
- 9:40some type generic macros some other
- 9:41things like that pretty cool stuff so
- 9:42there's a lot of things
- 9:43check out the wikipedia if you want to
- 9:45use those you probably won't use most of
- 9:46those in this class but it's pretty
- 9:47exciting
- 9:48as i mentioned before you've got this
- 9:50main before we had a vote in there
- 9:51saying you know what i don't need any
- 9:52arguments i'll just say void
- 9:54if you want to be able to process
- 9:55arguments you have to say the following
- 9:56into arg c
- 9:57care star argv open close square
- 10:01brackets what that means is
- 10:02rxc says how many arguments do you have
- 10:05however
- 10:06it starts arguing by the way and the
- 10:08index of that starts we
- 10:09you know i haven't told you this but c
- 10:10is zero indexed so the first guy in that
- 10:12array is index is zero just like python
- 10:15and unlike scratch and snap is which is
- 10:17one index the next one is a one the next
- 10:19is a two
- 10:20so here arg c is two why is argc
- 10:23two because there's two things there
- 10:25sort and my
- 10:27file interesting so arc v arc v
- 10:30of zero is sort it's the name of the
- 10:33executable it's the name of the program
- 10:34itself
- 10:36arc v of 1 is my file so that's kind of
- 10:38cool right so you can still go from 0
- 10:40up to but let up to less than in some
- 10:42loop and you'll process through all the
- 10:44things that were passed into you
- 10:45you don't you can't ever get to the arg
- 10:47v of two because if
- 10:48it's an array it has two guys it's zero
- 10:50one uh
- 10:52zero and one you only have two guys zero
- 10:54and one you can't get to arg v of two
- 10:55if you actually uh if you had handed arc
- 10:57c of two
- 11:00so why do you might think to yourself
- 11:03why would you ever care about getting
- 11:04the name of the program
- 11:06think about that i'm gonna leave that
- 11:08out there and see if you can figure out
- 11:09why you might actually want the name of
- 11:10the program
- 11:11in the program itself think about that
- 11:16we'll see the next video
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 03.3 - C Intro: C v. Java and C Syntax by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,562 words across 396 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.