YouTube2Text

[CS61C FA20] Lecture 03.4 - C Intro: Basics: C Syntax — Transcript

by CS 61C Departmental · 3,245 words · 487 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's take a look
  2. 0:02at some c
  3. 0:03syntax to actually teach you more about
  4. 0:05the language that we haven't already
  5. 0:08so the first is we talked about before
  6. 0:10that there's a new way in c99
  7. 0:13to have a boolean type but if you don't
  8. 0:15include that you just have to go with
  9. 0:16kind of more ansi c
  10. 0:18what is true and what's false so what's
  11. 0:20false in c is
  12. 0:21zero only zero zero is false
  13. 0:24that's it turns out null is actually
  14. 0:28defined as zero so if you say a pointer
  15. 0:30i got a pointer here and if i set the
  16. 0:31pointer to null which means it's not set
  17. 0:32to
  18. 0:33pointing to anything that really means
  19. 0:34the pointer is set to zero
  20. 0:36um and i mentioned before that standard
  21. 0:38bull.h defines official boolean types if
  22. 0:39you want to use that
  23. 0:40what's true everything else this is a
  24. 0:43little different than python because
  25. 0:45python has kind of
  26. 0:46what what false is for every type so
  27. 0:48empty string is false for
  28. 0:49strings zero is false for numbers uh
  29. 0:53you know empty sequences falls for other
  30. 0:54things and anything that's not that is
  31. 0:56true
  32. 0:57in scheme it's actually closer to what c
  33. 0:59does because scheme actually has hashtag
  34. 1:01f is false and everything else
  35. 1:02even zero is true for scheme even m this
  36. 1:05is actually a
  37. 1:06this is actually a a matter of debate
  38. 1:08you want to get a scheme person riled up
  39. 1:10ask them whether the empty list is true
  40. 1:13or false
  41. 1:13some people say it's not hashtag f some
  42. 1:15people say no no empty list it should be
  43. 1:17false also so there's a lot of
  44. 1:19get get two people who are you know old
  45. 1:21scheme folks together and have them
  46. 1:23fight over whether
  47. 1:24nil nil is a is false or not it's kind
  48. 1:26of a fun fun game to do
  49. 1:29so in terms of variables you've got to
  50. 1:32have type so you know from java you have
  51. 1:33types
  52. 1:34you have to declare what type a variable
  53. 1:36is before you use it and you can't
  54. 1:37change it it's locked in for the life of
  55. 1:39the program
  56. 1:40types can't change so here's an example
  57. 1:42of
  58. 1:43some of the common types people are
  59. 1:44using you saw ins for integer numbers
  60. 1:46negative numbers as well
  61. 1:47you might say well how big are they
  62. 1:49we'll show you a slide how this is all
  63. 1:50ambiguous
  64. 1:51and this is why we added in types which
  65. 1:52declare exactly they are eight bits wide
  66. 1:54or 16 bits wide or etc etc
  67. 1:57unsign ins for unsigned numbers floating
  68. 1:59point for floating float for floating
  69. 2:00point numbers
  70. 2:01we'll actually have a whole lecture on
  71. 2:02how how that actually gets stored that's
  72. 2:04a really cool
  73. 2:05lecture to see doubles for bigger
  74. 2:07floating point numbers
  75. 2:08uh characters for single characters for
  76. 2:10you know axi characters
  77. 2:11uh longs for longer in so i got an in
  78. 2:14but i need i need more space for
  79. 2:15for a two's complement value i do a long
  80. 2:17i need more space than that it's a long
  81. 2:19long it's like let's just keep writing
  82. 2:20the word long in front of things when
  83. 2:21it's almost like you receive a file you
  84. 2:23save a file you're working with a friend
  85. 2:25or something
  86. 2:25back in the days of sneakerwear when you
  87. 2:27had a file you called it final and then
  88. 2:29you give it to them and they make a
  89. 2:30change they call it final final
  90. 2:31it's like long long that's a big little
  91. 2:33long that's what it is um
  92. 2:36python versus java versus c so um
  93. 2:40interesting thing about c is see they
  94. 2:43decide that
  95. 2:44int feels like the the most efficient
  96. 2:48way you would store a two's complement
  97. 2:51number
  98. 2:51so if here's an example if it's a 32-bit
  99. 2:54machine
  100. 2:55which means the data kind of is the
  101. 2:57whole data path is meant for 32 bits
  102. 2:59then probably it's going to be 32.
  103. 3:00if it's a 64-bit machine built so that
  104. 3:03bits are what all
  105. 3:04the everything is floating around in
  106. 3:05that way that's the width of the data
  107. 3:07path
  108. 3:08then probably uh ins are going to be 64.
  109. 3:10it's really what's called the word size
  110. 3:11which is the width of
  111. 3:13the majority of the data you're flowing
  112. 3:14with um
  113. 3:16here's what i don't actually don't like
  114. 3:17this slide so much because you have to
  115. 3:18remember i don't want you to memorize
  116. 3:19these things i mean
  117. 3:20the only guarantee is that a long long
  118. 3:22is the size of
  119. 3:24size i'll try i'll show you in a second
  120. 3:25what sizeof does it tells you how many
  121. 3:27bits wide this thing is um actually how
  122. 3:30many bytes wide this thing is sorry how
  123. 3:31many bytes wide this thing is
  124. 3:33um so if it's 32 bits the size of we'll
  125. 3:35say four okay just remember that then
  126. 3:37okay so the only guarantee is that size
  127. 3:39of a long long is bigger than equal the
  128. 3:40size of a long bigger look at the size
  129. 3:42of an inside basically
  130. 3:42the size of a short but you don't know
  131. 3:44other than that they could all be 64.
  132. 3:46they could
  133. 3:46i mean it's a little crazy um i mean i
  134. 3:49guess long has to be bigger than 32. i
  135. 3:50just don't like this i'd like to use in
  136. 3:52types and declare
  137. 3:53i need to know i don't want to be i'm
  138. 3:54going to be guessing i don't have code
  139. 3:56that runs on one machine
  140. 3:57and i move to the machine and because of
  141. 3:59some way i understand things it doesn't
  142. 4:00run anymore no
  143. 4:01i don't need to know how wide these
  144. 4:02things are i think in types we should
  145. 4:04always be using
  146. 4:05they could all be 64 bits but i i would
  147. 4:07prefer you and encourage you very
  148. 4:09strongly to use in types as much as you
  149. 4:10can
  150. 4:11it gets a little annoying how you printf
  151. 4:12and scanf values within types is a
  152. 4:15little bit annoying if you use macros
  153. 4:16for that
  154. 4:17uh not macros but constants for that
  155. 4:18it's a little annoying
  156. 4:20but do it it's cleaner uh and you'll
  157. 4:22have code that then migrates really
  158. 4:23cleanly you'll have it work so which
  159. 4:24means
  160. 4:25you want to live in a 32-bit world even
  161. 4:26a 13-bit world if you move to 64 it
  162. 4:27won't just
  163. 4:28change size integers just to be 32
  164. 4:30because you said explicitly i want this
  165. 4:31into be 32 bits wide
  166. 4:32great um his little comparison about the
  167. 4:36three languages python java and c
  168. 4:38of the size of what an int is uh python
  169. 4:40there are at least 32 bits java they are
  170. 4:42exactly 32 bits which is interesting
  171. 4:44and c depends on the computer in the
  172. 4:46early days ants were 16.
  173. 4:48it was a big deal when it became 32
  174. 4:50because the machines back in the day had
  175. 4:51a data path and a word size it was 16
  176. 4:53bits it was two bytes wide
  177. 4:54so c has changed and evolves his old
  178. 4:57language had that
  179. 4:58and i don't like the idea that it
  180. 4:59changes use in types and lock it in
  181. 5:02you can also turn the lock we're talking
  182. 5:04about locking things in you can also
  183. 5:05lock in the value of variables
  184. 5:06um if you have a variable that you know
  185. 5:08is going to change it's going to be a
  186. 5:09constant it is reference value it's the
  187. 5:11golden ratio it's the days of the week
  188. 5:12it's
  189. 5:13you know it's the speed of light it's
  190. 5:15the law lock that in
  191. 5:17and say constant so if you add constant
  192. 5:18in front of it it says the value won't
  193. 5:19change if you try to change it your
  194. 5:21compiler will say hey you can't change
  195. 5:22this const
  196. 5:22very useful so you can buy those like
  197. 5:24your friend in that way so if you know
  198. 5:25something's not going to change
  199. 5:26lock it in as a value as a constant an
  200. 5:29enum is a nice
  201. 5:30feature of the language it lets you kind
  202. 5:31of have an enumeration so enumerated
  203. 5:33constants it gives you
  204. 5:34each a value so if i say enum color
  205. 5:37red's gonna be zero color
  206. 5:38green is gonna be one and blue's gonna
  207. 5:40be two if you ever use an enum i
  208. 5:42strongly recommend
  209. 5:42you not peek under the hood to find out
  210. 5:44what those bits are of each one of those
  211. 5:46i recommend
  212. 5:46you always not know your code should
  213. 5:48work even if they were to rearrange
  214. 5:50the numbers of those guys if i would say
  215. 5:52go back to your code and change
  216. 5:53to be blue red green then each of those
  217. 5:56will be different values your code
  218. 5:57should still work
  219. 5:58type functions in c so
  220. 6:01all the functions that you declare have
  221. 6:03to have a return type
  222. 6:04uh they need to you need to explicitly
  223. 6:06tell them in advance usually above the
  224. 6:08function
  225. 6:08what its expected return type is and
  226. 6:10then when you're actually using it
  227. 6:11you'll
  228. 6:12oh i get it your it can match them up
  229. 6:14and try to do some kind of matching to
  230. 6:15say well you know what you're
  231. 6:16you told me it returns an int but you're
  232. 6:18actually using it as an unsigned end are
  233. 6:19you sure you want to do that
  234. 6:20and so it'll give you a warning or give
  235. 6:22you an error that which is nice you can
  236. 6:23also say look this is supposed to be a
  237. 6:25command this would be something that's
  238. 6:26supposed to
  239. 6:26do have a side effect so the return
  240. 6:28value can be void meaning return nothing
  241. 6:30um and variables and functions need to
  242. 6:32be declared before they're used here's
  243. 6:34an example of number of people and
  244. 6:35dollars and cents is kind of declared as
  245. 6:37ins and a float
  246. 6:40a struct is the way to make abstract
  247. 6:42data types you can have
  248. 6:44a series of fields this feels very much
  249. 6:46like python uh where you have a you know
  250. 6:48a class and you have
  251. 6:50dot fields same idea but you have a lot
  252. 6:53more control about what the bit widths
  253. 6:54are of that
  254. 6:55uh for the most part um so the other
  255. 6:58thing i want to explain to you is
  256. 6:59typedef
  257. 7:00allows you to declare a new type so you
  258. 7:02know the types we talked about are
  259. 7:03floats and ins and et cetera and cares
  260. 7:05you can make a new type that's pretty
  261. 7:07cool so i say you know what
  262. 7:08i want to bite i know a bite's going to
  263. 7:10be eight bits so i'm going to say u and
  264. 7:11eight underscore t as byte and now i can
  265. 7:14just say
  266. 7:14byte b1 and b2 that's really cool so
  267. 7:18that's really useful
  268. 7:19um the way you can do it a struct is to
  269. 7:21say i have a structure
  270. 7:22it's going to have two parts to it it's
  271. 7:24kind of like a ball of two pieces of
  272. 7:25data
  273. 7:26together here is uh what's a song what
  274. 7:29would a song be well the
  275. 7:30length in seconds for how long the song
  276. 7:32is and the year is recorded that's a
  277. 7:34structure
  278. 7:35two wins together i call that a song and
  279. 7:37i typed it as a straw
  280. 7:38as a song and now i can just say song
  281. 7:41song one
  282. 7:42and that thing that's going gonna have
  283. 7:43two fields how do you get to the fields
  284. 7:45dot notation just like python so song
  285. 7:48one dot length in seconds set that one
  286. 7:49song one dot er recorded do the same
  287. 7:51thing for song two so pretty cool so
  288. 7:52structs are kind of cool
  289. 7:53um we'll talk about them more a little
  290. 7:55bit later when we talk about pointers
  291. 7:56and how to access those fields
  292. 7:58again most of this is from c and java
  293. 8:01being the same
  294. 8:02within a function it's remarkably close
  295. 8:04in terms of its syntax
  296. 8:06if looks very similar you've got an if
  297. 8:08by without an else
  298. 8:09you get an if with an else but that's it
  299. 8:11you don't have an l if like you have in
  300. 8:12python
  301. 8:13um and you'll notice that sometimes you
  302. 8:16don't have to have if you only have one
  303. 8:18statement you don't have to have
  304. 8:19brackets around it that's great except
  305. 8:22when you
  306. 8:23think it's indented so you're thinking
  307. 8:24what's python right because you're in
  308. 8:26python mode it's indented it must be
  309. 8:27part of the if
  310. 8:28and you don't put brackets so you have
  311. 8:29one thing that's right but then you put
  312. 8:31a second thing it turns out that second
  313. 8:32thing even though it's indented is not
  314. 8:34part of the if because if without
  315. 8:35without brackets
  316. 8:36only takes one statement after it one
  317. 8:38semicolon that's it so you think it
  318. 8:40looks and that's a huge source of bugs
  319. 8:42for new programmers where i don't use
  320. 8:43the brackets but i have two lines in
  321. 8:45there
  322. 8:45and i think they're both you know
  323. 8:47indented so they're part of the if
  324. 8:48they're not part of the f
  325. 8:49you need to have brackets so get used to
  326. 8:51always putting brackets around your if
  327. 8:52even if you only have one
  328. 8:54one line i don't think we've done it
  329. 8:55always in this code that we're going to
  330. 8:57share with you because we had to pack it
  331. 8:58tighten the slides but try to do that
  332. 9:00just in fact most editors will actually
  333. 9:01add the brackets anyway for you if you
  334. 9:03go into sublime we'll do it
  335. 9:04for you so get in the habit of putting
  336. 9:05the brackets around it we also have
  337. 9:07while and
  338. 9:08do while do the statement while this
  339. 9:10expression is test so
  340. 9:11if you want to test it before test it
  341. 9:13before you go into the
  342. 9:14the set then that's the while if you
  343. 9:16want to do it before you do the test
  344. 9:17test after use the do it's kind of cool
  345. 9:21four looks very similar for
  346. 9:22initialization a check and an update
  347. 9:24then a statement
  348. 9:25you have a switch statement which says
  349. 9:27here's an expression and then i have
  350. 9:28this
  351. 9:29cases and if the cases match you do
  352. 9:31these statements and here's the problem
  353. 9:34you have to have a break statement
  354. 9:37after each of the cases so case uh case
  355. 9:40it's uh uh one
  356. 9:41let's just say hey maybe switch on a
  357. 9:43letter you type in some letter
  358. 9:45case q because they want to quit well
  359. 9:47then do sorry you want to quit
  360. 9:49sorry please you want to come back if
  361. 9:51you don't have a breaks
  362. 9:52a line below that last case it'll roll
  363. 9:55into the next case statement you have to
  364. 9:56have a break after each of these cases
  365. 9:58in there
  366. 9:58so that's actually important people have
  367. 10:00done this before where they'll just roll
  368. 10:02through like why is it running both of
  369. 10:03them yeah because it's
  370. 10:04case kind of jumps into it and then it
  371. 10:06just rolls until it sees a break and
  372. 10:07that's a problem
  373. 10:09there is a default that says look it's
  374. 10:10everything else but the cases have cases
  375. 10:12but the cases are checked through the
  376. 10:13default case c also has a strange syntax
  377. 10:16called a go to
  378. 10:18do not use a go to at least in 61c go to
  379. 10:21is reserved for
  380. 10:22kind of expert c programmers who know
  381. 10:24what they're doing you can basically
  382. 10:26make c look like basic where you go here
  383. 10:28and you have all these labels and you
  384. 10:29can go there and go there and go there
  385. 10:31and it's
  386. 10:32it's it's it's non-local control flow
  387. 10:35which means jump around just randomly
  388. 10:37don't do that it's like the worst code
  389. 10:39you'll ever have
  390. 10:40so don't use a go-to until you get to
  391. 10:41have like until you upgrade to badge
  392. 10:44level 17
  393. 10:45of the badges you've earned for
  394. 10:46programming at c it's bad it's bad case
  395. 10:48but he has a go too so you might see
  396. 10:50some go-tos and some
  397. 10:51code that you might inherit we're going
  398. 10:53to end this this lecture with a look at
  399. 10:55actual c code stop talking about the
  400. 10:57format actually show me how this works
  401. 10:59well let's read it together and in fact
  402. 11:01i have
  403. 11:02i think i have this set to be a pen
  404. 11:06check this out so i can actually
  405. 11:07highlight each of these lines and we can
  406. 11:08talk about them
  407. 11:09so you saw the easiest of my includes of
  408. 11:11that that's nothing new before let me
  409. 11:13make this white just
  410. 11:14just to give you some juice here all
  411. 11:17right
  412. 11:17so we declare some we declare some
  413. 11:21values
  414. 11:22angle do this is this is kind of old
  415. 11:23school ansi style because i'm declaring
  416. 11:25it at the
  417. 11:25top before i actually really use it i
  418. 11:27could have declared it just at the
  419. 11:28moment
  420. 11:28i literally could have declared it int i
  421. 11:30could have said int
  422. 11:32right here in front of angle degree and
  423. 11:33i wouldn't have to do any more than that
  424. 11:35so a lot of people declare it locally
  425. 11:36rather than here but this is like more
  426. 11:38old school style to do this let me clear
  427. 11:40that up
  428. 11:41so we can take a computer table of prime
  429. 11:44of sine of sine function okay so first
  430. 11:46here's pi pi is going to be a double
  431. 11:48that means it's a float but it's a
  432. 11:49little bit more space for than that
  433. 11:50we'll talk about the details in a slider
  434. 11:52in a lecture or two
  435. 11:53um i set uh a tan eight hand is in
  436. 11:57math.h
  437. 11:58so i could just get access to that you'd
  438. 12:00be like well dan you mean in python i
  439. 12:01used to have this dot notation if i
  440. 12:03import math i have to say math.atan
  441. 12:05nope all of those in the same shared
  442. 12:07namespace welcome to one namespace
  443. 12:10have a good time um
  444. 12:14i'm going to print this out print what
  445. 12:15pi is i'm going to print out this little
  446. 12:18table header that's
  447. 12:19nothing special there and now here's my
  448. 12:21first in my first actual
  449. 12:23control structure while angle degree is
  450. 12:26less than or equal to 360.
  451. 12:29that's fine um you never this is like an
  452. 12:32fyi i'm going rogue a little bit you
  453. 12:33never want to have one of these test
  454. 12:35cases be based on a floating point value
  455. 12:37because the floating point value
  456. 12:38sometimes can be 0.001 sometimes 0.99999
  457. 12:41and that means the number of iterations
  458. 12:42can actually be off by one so you always
  459. 12:44want to have kind of a while loop be
  460. 12:46checking out an integer value not a
  461. 12:47floating point value this is an
  462. 12:48important thing
  463. 12:49so i'm doing this while which is on
  464. 12:51angle degree and angle degrees in int so
  465. 12:52yay thumbs up on that one
  466. 12:54so what the angle radian is a little
  467. 12:56expression say what the value is call
  468. 12:58sine i print this out the value of angle
  469. 13:00degree and value
  470. 13:01i increment angle degree and i stay in
  471. 13:02my loop until i'm done return zero
  472. 13:04so pretty simple piece of code but
  473. 13:06that's an actual working piece of code
  474. 13:07you can copy and paste this and have it
  475. 13:08run and print little sign tables in fact
  476. 13:10there's my sign table that's what you'd
  477. 13:12get there's a little pie and some little
  478. 13:14angles and signs kind of nice and it
  479. 13:15goes on till 360 degrees all right
  480. 13:17that's c we're a little lot more but
  481. 13:19we're kind of getting our hands dirty i
  482. 13:21love it enjoy by the way grab
  483. 13:22you better be if you haven't if you've
  484. 13:23been watching up to now you better go
  485. 13:25grab your editor and copy this stuff in
  486. 13:27come on i told you that let's have some
  487. 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.