Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
PROFESSOR
And welcome back.
Since so many of you have come from a Java experience in 61B, or some from another community college or from another university, let's actually show you that you probably already most of what you need to know for C. There's a couple of details.
We'll spend a lot of time talking about the differences, but let's talk about some of the similarities as well. And doing a comparing and contrast kind of thing.
So here's a beautiful table. Java is an object oriented language, you know that. C is mostly a function language. You can do some object stuff, but really, that's what C++ was for.
So C, the idea, the core idea is a function. The Java core idea is a class or an abstract idea, abstract data type.
You can make abstract data types in C, but it's a lot harder to do. It's not as clean and simple. It's just screaming to you, here's an abstract data type, there's a factory.
Instance, that's a new abstract data type. Harder to do in C unless you really do something to really do it right.
The way you compile in — so I guess I mentioned before, Java is compiled. Java C, hello.Java creates this independent — machine independent bytecode.
The way you compile C is GCCHello.C That creates some machine independent code. Machine language code, machine dependent code. C is machine dependent. Java's output, Java C's output is machine independent code.
Though you run it, you just have to say Java Hello. On the Java side, you say a.out. And a.out loads it into memory and then runs the program.
Lots of more details on what the loader does. We'll talk about that, but a.out is where you run the program.
By the way, there is a way to specify the name of the output file. If your source code is Hello.C, -O, put the word, put the name of the executable.
I don't like to see any 61C student ever using a.out. They should say Gcc-O hello, hello.c. If it's just a simple program, Helloworld or something fun, pretty small.
The point is, you shouldn't be having a.outs. Name your executable, then — if you have 10 exectuables, what do they all do? I don't know. I'll just run it.
All of a sudden, the robot starts moving, and the printer starts — no, name your executables to see what the heck that these things do.
The picture on the right is — you get the Java, the reason I don't like Java as a first language in APCSA, I advocate for using a block's language or something else.
Python would be great for APCSA in high school. Is you have to teach the kid in the first day all the elements of the picture in Java. It's a little crazy.
In C, the amount of things you need to teach them is not much.
There's this library that you're including that has a standard library, standard I/O library, so you can have printf and some other things. Main, we'll talk about the details of the main program.
But main void just says, I'm not going to use any arguments for now, just main. I printf, you don't use printf. Quote, you have to specify a new line.
Not a print line. For Java it gives you a free new line. You have to actually specify the new line yourself in C. And you have to return a value.
Main returns a number, we'll talk about functions returning as particular value and declaring that, but main returns an integer, and so 0 means success.
Just is a little FYI, 0 is always success in Unix, because there could be a lot of ways things fail, but only one the way they succeed.
So they decided, well, 0 is going to be the success number, and any other number might be something didn't go wrong. It's kind of fun to see that.
So that worked. So return 0 is the right thing to do when it worked. If it didn't work, return some other number.
And that would be a special code that you and the person calling you might agree on. If it's 7, that means file's not found.
If it's 12, that means couldn't find the printer or something. Okay.
Storage, this is what differs, this is why I love to teach 61C in the third class, not in the first or second class, because you want to be able to have layers of abstraction.
One of the layers of abstraction you have in Java is that it manages a memory for you. You say new and it just works.
It handles it with your garbage collection, and hopefully you learn that in 61B. You have to do this all yourself. So all the safety belts, all the rubber padded rooms are gone.
You're doing all the memory management yourself. You can get in trouble. Another difference. You've got C and Java in terms of comments.
So this is old school and CC, but something we'll share with you, C99, in a couple of slides, talks about, that actually they allow you to have the same comments as Java.
So for now, Java and C are the same in comments if you're using C99 or later. You have to say final if it's a constant in Java, and #define or const.
for a variable that's not a #define global variable in C. Do you have a preprocessor? Yes, you do in C, you do not in Java. So we talked about that a moment ago.
You have to have a definition at the beginning of the block in C, in old school and CC, but in new school, C99 and beyond, you don't have to do it.
You can declare a variable right before you use it. It's just like you can in Java. Naming conventions, it's still not clear.
There are lots of people who have different naming conventions in Java and it's still in C as well. In fact, a lot of people — it's not as clear.
Python typically universally has the word with underscores, looks like C. But there's still a difference of opinion on how C is done.
So sometimes you see, even our own projects will have camel case or other conventions.
But typically, people have mostly agreed that it's the same kind of underscores separating words in C. But actually, most of the time, C, what they do is they'll come up with a shorthand.
So rather than saying — let's see, I don't know. a to integer. You wouldn't write ASCII to integer. You say ATOI.
So my point is, sometimes the C, the kind of old school C is to convert the word into a really tight word like MV. Like Unix commands, like that's the old school.
Back in the '70s and '80s, that's what they were thinking. NV, CP, like a couple of letters for things. So a lot of times, a lot of the words, they should be more verbose.
And we've realized that it should be more verbose.
But they're shorter in C. The way you access the library as you import the module in Java and you access the library, access library with a pound sign, include either the angle brackets or the quotes.
Finally, most of the operators you see are identical. Assignment, arithmetic, augmented assignments, you know, plus, equals, minus equals. There's a lot more of them that you haven't seen before.
Bitwise logic, that means operating the actual bits of things. Not on kind of logical values, but like Boolean logic below is the actual Boolean value. True or false, in doing things with that.
But bitwise, it says work on the actual bits themselves, which is nice. It kind of works in parallel, very powerful. You can move things around, shift them around with the bitwise shift.
Testing for quality is the same, you group them with parentheses. The relationships orders. You can increment and decrement with ++ or — . Member selection, we'll talk about this more.
This is how you kind of get into the fields, say, of a struck. We'll talk about that a little later.
Conditional evaluation, which we call the inline if or a ternary if, is the same as well. Has there been an update to the ANSI C picture I showed you at the beginning.
Yes, there has been.
It's called C99, so if you hear me saying C99 and beyond, in 1999, a consortium got together and says, we got to upgrade C. They said, all right, let's do this.
Remember, ANSI was about '86, 1986, so they decided to do it. By the way, I still think that they should have used yyyy.
I mean, 99, we keep working with this for another 100 years.
99, and 1999, or 2000, it's a little crazy that they didn't think about the Y2K problem, and already tried to have four digits for their — but they didn't.
Remember, this is still the very abridged way of naming things. So 99 is fine, all right. I guess they'll be fine, let's see how, maybe they said, oh, C won't last 100 years.
Who knows? The way you can find out what version of C you're running is this little print left.
If you say printf (%ld/n) and you have this constant standard C version, it'll tell you what number that is. And that is the number, which is 1999, 01, it's just a big number.
But that's the actual date of that release of that version of C that you're working with. Actually, the compiler you're working with, really. References as a Wikipedia link to that.
Here's some things that you'll see that are different from ANSI C. So if you see old school C, you're probably not going to see a lot of old school C, probably the code.
But maybe you get a job that you see old ANSI C, and you need to know that's ANSI C. And maybe, hopefully you'll have a C99 or above compiler that you're working with.
But if you see old school C that's doing something weird, you'll see that's because of these reasons. That you had to have declarations at the top. Now you can have declarations anywhere.
Thanks to C99, declarations can be anywhere. And Java like comments, we've talked about.
This is a big deal, boy, all the code I wrote from '86 to '99 was so bad, because you couldn't have a variable length array. You had to have there be a constant.
And there's nothing you can say, hey, user, how big an array do you want? Oh, 100? Okay. Make it array.
We'll talk about what arrays are later. Of 100, you couldn't do that. You had to hard code the number into the code itself.
You couldn't ask dynamically what the size of something is in an array. That was a big deal. So that third bullet down, I'm like, whoo! Very happy.
The other thing you're going to see in a couple of slides is how big are things. How big is an integer? Well, it used to depend. And int type said, enough with this depending.
It should just be clear. You should be able to specify how wide integers are. I think you've seen that before in the number representation.
You can say I want it to be 8, or 16, or 32 or 64. So they have that. So please use int types rather than int, I recommend.
And standard Booleans, you actually have Booleans, you never had Booleans at all. We always had 0's and ones.
And we now actually have Booleans that you can use if you use the standard Boolean library. Has there been an upgrade to C99? Yes, there has been.
It's called C11, and now actually the newest one is called C18, which actually is mostly C11, which has bug fixes. And here's the two numbers you get, 2011 and 2017 for the longs.
There's references here, now it's just one reference for all C standard revisions. You now have multithreading. Multithreading support, which is amazing. You now have Unicode.
We talked about Unicode the last series of lectures, we can now support Unicode in C. That was a big deal. Gets was a huge security hole for so many years.
They got that out of there. And some more things that aren't — complex values, if you care about that. Some type generic macros, some other things like that.
Pretty cool stuff. So there's a lot of things. Check out the Wikipedia if you want to use those. You probably won't use most of those in this class, but it's pretty exciting.
As I mentioned before, you get this main. Before we had a void in there, saying, you know what? I don't need any arguments, I'll just say void.
If you want to be able to process arguments, you have to say the following, int, argc, char, *argv, open close square brackets. What that means is, argc says, how many arguments do you have?
However, it starts — by the way, and the index of that — I haven't told you this, but C is zero indexed, so the first guy in that array is index 0, just like Python.
And unlike Scratch and Snap, which is one indexed. The next one is a one, the next is a two. So here argc is 2, why is argc 2?
Because there's two things there. Sort and my file. Interesting. So argv, arg of 0 is sort.
It's the name of the executable. It's the name of the program itself. Argv of 1 is my file. So that's kind of cool, right?
So you can still go from 0, up to to, less than, it's in a loop, and you'll process through all the things that were passed into you.
You can't ever get to the argv of 2, because if it's an array that has two guys, it's 0 and 1, you only have two guys, 0 and 1.
You can't get to argv of 2, if you actually hand it argc of 2.
So why do — yo might think to yourself, why would you ever care about getting the name of the program? Think about that.
I'm going to leave that out there, and see if you can figure out why you might actually want the name of the program in the program itself. Think about that.
We'll see you at the next video.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.