Assembly is important for the extreme low-level micromanaging that it allows us to do but it takes a ton of time and thinking to create anything more than simple programs. On the other extreme, Python lets us do nearly anything we want relatively easily at the expense of us not knowing exactly what the computer is doing at the basic memory level. Somewhere between these two extremes falls the C programming language. Briefly put: C is the most widely used programming language of all time, and many think that it is one of (if not the) top languages that everyone should know. Because you already know some Python the syntax of C won't surprise you all that much and because you know some Assembly the memory management aspects of C won't be all that troubling to you. Nonetheless, this is not a course in programming, so we will not spend much time with C, but because so much code is written in C it will be well worth it to invest a bit of time.
This site is wonderful. Usually when we write programs in C, we have to compile them (like we did with assembly programs when we did that as -o filename.o filename.s thing!) but this website has a little fake computer on the bottom where you can write up code, click play, and it automatically shows up! Do the basic section first, then stop and do the exercises below.
Important!: First, you should use gedit on your Ubuntu terminal to write and edit your C code (the programs you'll write here will be done in Ubuntu, not on the tutorial website). The way to make a file with gedit is to type:
sudo gedit (filename)
So, for example, to make a C file called "test1" we'd type sudo gedit test1.c and Ubuntu will open up the text editor gedit. Because gedit is so similar to text editors you've seen before (think Notepad) it shold be easy to work in it. When you're done, just close it and go back into your terminal. Note that if you open gedit in a terminal, that terminal will be "busy" as long as gedit is open, so if you want to test code out a lot you may want to open an additional terminal (right-click the terminal button and select a new terminal) so that one terminal can be "busy" with gedit and the other can be used to compile your programs.
Second, the way we compile C programs is similar to Assembly ones; we will use the command
gcc -o CompiledFileName FileName.c
to compile it. The "CompiledFileName" is whatever you want to actually call the program. For example, if my file was named SadProg.c I'd write
gcc -o SadProg SadProg.c
in the terminal. This will make an executable program called, "SadProg" which will be available in whatever directory I'm in. To run the program, you would type
Notice here that we must use "./" to tell Linux that we want to run SadProg and that it is a program which is in the current directory we're in (which is abbreviated by ./).
Write a C program that prints out "I love you!"
What is the difference between the commands printf("Hello!") and printf("Hello!\n"). Make a program to test them out if you don't know!
Write a C program that asks the user for their name and then prints out, "I love you (their name)!"
Make a C program that prints out the multiples of 5 up to 500.
Make a C program that asks the user for a number, then multiplies that number by 4 and prints that.
Make a C program using an array with three different names in it ("Patricia, James, Amanda") which prints out "Hello (name)!" for each person in the array. Use the array!
Make a number guessing game in C with the numbers 1 to 10. Make 7 the "correct" number, and have the user input values until they get it right. You will need to use a While loop and an If statement here. For a bonus, you may also tell the user if their answer is "too low" or "too high".
[Hint: Your While loop will look something like this: While correct == 0..., where correct is a variable that stays 0 until the user guesses the correct answer after which you can set correct = 1, which stops the "While" loop. ]
Make a C program which has two functions: one function takes in four numbers, adds them, then prints the sum and the other function takes in three numbers, multiplies them, and then prints the result.
Now you're going to finish up with some more advanced stuff. Read over and work out the first four parts of the "Advanced" section: Pointers, Structures, Function Arguments by Reference, and Dynamic Allocation. Don't worry if this stuff is a little crazy, just get the gist of it and practice with the examples they give.
|⇐ Back to 2.4||Home||Onwards to 3.1 ⇒|