CSC209Cramming

C programming?

I read an article how C is developed, but for me, What’s the point of knowing those thing? I’m living well without having taking much History classes. So I just extracted some Point that might be good to know.

C is called ‘C’ because it stems from a language “B” .

Q. Why we discarded B?

A. Well, It’s because obviously B had some drawbacks. One of  the drawbacks is that B doesn’t know much about Data types (i.e., string, integers etc). So what that mean is, even if you type 1/2 *cookie'(i.e., number * string), it will do multiplication of them and you will get really a strange outcome! Proper language should prevent programmers from doing that doesn’t make sense. Anyhow, B is gone. R.I.P B. So what’s the good point of C then? Why we need to keep this language?

To use C, we need to know bit more about how computer(that machine thing) interprets our code(just bunch of writing like a love letter). So when you make some program for machine things(including computer), you can easily manipulate machine with C than any other language. I think that’s the point ,because when I was learning Python, I didn’t learn about how the computer interpret my love letter. Now, It’s the time for me to learn about this bad boy.

Compiler(Computer-C language translator)

Before jump into the C language, we need to know about little bit of Compiler. Why? That’s what I exactly thought why the hell we need to bother to learn Compiler? But think in this way, Writing a code is simply same as writing a letter to our bad boy(aka computer). He’s living in a country called “Machine world” and you’re living in another country called “Human World”. So somehow you need to send the letter to this bad boy. However, sadly any human aren’t allowed to enter into the “Machine world”. So you need to find some other way to send your letter to him. What would you do? Fortunately, we have something called “Compiler”. It’s a company that is founded by immigrants from “machine world” . Like many immigrants or foreign students, like me, they can’t speak human language well, but they can understand you! Since they are not human, they can send your letter instead of you to the machine world. That’s it. That’s what compilers do. Just to make sure, what I mean by “sending the letter” is actually “translating” human language into the machine language and we call this translated letter as “object file”.(they have form of “name.o” like your picture file have a name “dirty_party.jpg”)   Be careful with the typo, because compiler check the quality of your letter quickly and discard it if it has too bad quality. (We can call it this as “Syntax/Semantic Analyzing”)

GCC(GNU Compiler Collection)

Since we talked about what is Compiler generally, Let’s talk about a specific compiler. There are several compilers ,but I’ll cover about GCC. Because my school labs use UNIX operating system and GCC works for UNIX. If you’re using window operating system and wan to use GCC, you need to make UNIX-like environment in your computer by downloading Cygwin. If you’re mac user, you can use Xcode and Xcode is built in and it is including GCC.

Anyway, all U of T computer science student MUST know about GCC for CSC209 course, because GCC is a standard compiler for C programming.  What is mean by GCC? Genie Compiler for Computer programmer? No, good try though. It sands for GNU compiler collection. GNU is some organization that made this compiler.(i.e., I don’t care.) It’s just compiler collection, why it is a compiler collection? Actually, it was called originally, GNU C compiler but it started to compile other language, too. So it became a collection of compilers that support different language including C. That’s it and here we don’t care about compiling other language.

But we want to know how to make it useful for us! So, How to use it? 

After writing your love letter whatever(aka your c program), you just need to type

“gcc -o <a title for your compiled  program file> <your program file, e.g., something.c)>”

Why we should type “gcc – o”? 

Well, gcc stands for GCC, the name of compiler and -o is for just naming your output file. Since gcc has a default name for the output file, “a.out”, so if you want to change the name to “myletter.out” then you need to put “-0”.

If you want to get some warning sign ,whenever something went wrong, you can just add “-Wall” option. What does “-Wall” mean? It’s not wall that you have your house(I thought so, when I saw that first), it’s saying, “Warn me all”.

ZZZ..I’m bored, Just tell me how to write C program or Shoot me!

(NO!…Put it down..I meant ‘water’ gun.)

Oh, I almost tried to shoot you with my gun that I got from Mr.bond last year. Well, wake up. It’s time to learn how to write C program. Let’s Print your favorite girl/boy name.

#include<stdio.h>

int main(){

    printf(“Someone that I don’t know yet”)

    return 0;

}

Yes, I’m single because I’m still waiting for the someone. Well, that’s my private life ,so let’s move on. If you don’t have any knowledge of computer program whatsoever, or you just simply forgot to how to write it (like me), the code above will look like bunch of gibberish. So let’s try to understand what those gibberish are actually meaning for.

#include<stdio.h>

Well, it looks like it’s including something called “stdio.h”. Why we put # and <>? Let’s think about what # means  first, in C language. “#blah” called as  “preprocessor command” in C language. What is pre-processing? It seems like, there is something that we need to first. Like we need to open our eyes first to start our day. So that means our  bad boy(computer) needs to include something first before process our code. <> is just like “” and it’s implemented simply because to make sure what is the file name that we want to exactly include. e.g., if we our file name is ”   Cookie”(some space in the front of the word) then we need to have some way differentiate  it with “Cookie” or “Cookie     ” (some space after the word). Then we can do it with <>. e.g., <    Cookie> vs <Cookie> vs <Cookie      >. Then what’s stdio.h? h stands for header file. Before what is stdio.h, let’s talk about what does mean by “header file”.

Header File

To know what is mean by “Header File”, actually you need to know about  “forward declaration” and “function prototype” but well.. Let’s meet all the cute boys together. Let’s imagine that you visit “Korea” and someone invite you in their party. Then your friend told you that in order to go to the party, you need to bring your “Soju and some snacks”. Well, that’s header file; The file that contains a list of things that you would need for the party.  But you will probably  ‘What the heck “Soju” is?’. That means you don’t have idea about “Soju prototype”. So, you called to your friend and asked “What is Soju?” and Your friend said “Oh, it’s alcohol beverage. ” and you said “Oh, got it.” and the call is ended.  Now, you got what is mean by “Soju”. If you got the function prototype, then you will know what this function is. (what is returning, what are the inputs for this function etc..) .You will probably think “Shoot, It would’ve been much better, if he let me know what the Soju is, at the very beginning!”. Then you’re thinking of “forward declaration”. forward declaration is the action of putting the function prototypes in the very beginning of the program file. You can write all the function prototypes at the beginning of you program file or you can just list them in the “header file” and just simply write the name of your header file at the beginning of your file. Then “#include” command will copy the list and paste them into the very beginning of your program file.

You can find nice example and quiz about it at here, http://www.learncpp.com/cpp-tutorial/17-forward-declarations/

So stdio.h is a some kind of hear file. Simply it “stands for standard input and output”. It’s standard library header file for C. What is meaning by that? Well, if you know that programming is all about input and output and the meaning of standard, then I think it’s easy to understand it. It’s just a header file that contains all the function prototypes that need to at run all C program. That’s it. At least, You must have #include <stdio.h> to run your C program.

So now what? Let’s break ‘int man(){}’!

Oh, my. We just finished the first line. Darn it. I know, we need to be tedious if we want to learn Computer. Personally, I think it’s because Computer is super stupid. We need to instruct it what to do, one by one. But the thing is Computer is powerful! If you manipulate them well, you can be a BILLIONARE. Yup. That’s it. I must learn of it. Anyway,  let’s get back to our “main” point. what is int main(){}? First of all, You need to know about what is function. If you found a things that in form of “something1 something2 (blah1){blah2}”, then “good job!”, you found a function! Have you ever learned any kind of function in math? It’s same as it. If you don’t know what is function in mathematics, then I’ll tell you. Function is a thing that get input then expected output.  It’s like a vending machine. You insert coin and you get your beverage. Well, So “blah 1” is your coin(i.e., something that you’re putting in), and “something 1” is the type of thing that you will get. (i.e., pepsi or coke whatever you want, they are all beverage.) “something2” is the name of function(Vending machine). At last, “blah2” is instruction of how the vending machine should work in order to get your drink. So int main(){} is a function! and the type of its return value is integer and the name of the function is main. You MUST have main to run your program ,because it’s the start point of the execution of your program. So between “{ }” there are instruction for your vending machine(aka function).

printf()

printf is a function call. What does that mean? Except for main you need to call your function if you want to use it. So that means you want to use the function. That’s it. How do you use the vending machine? You need put the coin! Our coin is “the name of someone that we like for my case it will be printf(“?”), then your function will do something. printf will return you something or do something. Have you ever used some game machine that you need to put coin? Not all function gives something like vending machine to you. Some function will let you do something if they got coin like game machine. printf is one of the game machines. They let you print your input to the standard output(it’s just a default place that printing things are written on).

return 0

0 has some secrete code in it. Conventionally, if a program returns 0, then the program was successfully executed! Don’t confused with the  0 as Boolean value. As a Boolean value, 0 means false. Sometimes you might want to know if your program has finished successfully or not. So It’s convention to return 0 at the end of main. That’s why we had “int main(){}”, since we’re returning 0.

Compilation/Execution 

Yup, that was the function. After writing function, if you want to run your code. Just save your file and open up your console and type gcc  -Wall  -o <name of your object file(what about “Myfirstofile”?)><your c file name> then you just completed compilation. Magically, you will have object file. Execute your program is same as executing your object file. Don’t double click it! Elegantly, you can just type Myfirstofile.exe(for window users), or ./Myfirstofile(for Unix/Linux users) on your console. That’s it. You will see the printed name on your console.

Data type/Declaring

Writing code is a giving instructions to the computer. Most of instruction in the world has similar form.

“Do something with something”

We use functions, operators to order computer “Do something”

But with What?

That’s data! So Computer programming is “Do the list of job with data”

That’s it.

But ,But what’s data? 

Data is “Distinct Information, usually formatted in a special way” from ‘webopedia’. It’s saying that data is information ,but not any information! It should be distinct and mostly formatted uniquely. So let’s think about “2”, now tell me, 2 is integer or float?

Yes, 2 is integer. You can categorize 2 as an integer. So it’s distinct and it has it’s unique format that is distinguishable from float. Integer is one of data type of C. Data type is just categorization of data. e.g., 2 is integer , “2.34” is float.

Let me list some data type of C and introduce some interesting fact of them.

“int, char, long, float, double”

They’re all primary data type.  Primary type is like an ‘atom’ in chemist they are some kind of unit.

You can use those primary data type to make up the secondary data type which are

“Array, Structure, Unions, Pointers”

Let’s break the primary data type in half first!

If you take any math course above the primary school level, you will know what does mean by Integer. Yup. That’s it. That’s the integer we’re using in C. But, there is additional information that you might be interested in. The actual size for the integer type. Integer got 2 bytes usually. What is byte? well to know what is mean by “byte”, you should know “bit” first.

As you probably know, computers is stupid. Because they only know either 0 or 1. Darn stupid so they need to store all the information as a long, long sequence of 0 and 1. If computer can store a lot of 0 and 1, then it has good storage capacity. We need to tell the amount of its capacity and we use the number of 0 and 1 that it can store for that. Bit is just unit for that. If the computer only saving one either 0 or 1 ,then it has one bit. So the byte is composed of 8 bit. So that means if we have one byte, we can store 10000000 or 11000000 or any kind of 8- length of string that is composed of 0 and 1.

Hence, now you will understand what is mean by integer got 2 bytes(16 bits). By using Integer type, you can manipulate number form -32768 to 32768. Isn’t that enough? well, there are always some people want more. So now we have a “long”. That is also integer-like type but have more range that means they take up more space also. They take usually 4 bytes. So if you want to manipulate number above the integer, you better use the long type.

float and double are for decimal numbers. Their relationship is very similar with the one between int and long. float only takes 4 bytes but if you need more then you use double(double takes 8 bytes).

Lastly, for primary type there is a ‘char’.

‘char’ stores one character like ‘A’, ‘B’ or ‘c’ etc. ‘char’ is  1 byte data type. Hence it can store 8 bits. Its range is from -128 to 127, so we have a choice of 256 = 2^8.  You can declare your char type of variable with either number or character. (variable is just space place holder for data types. Like you store 1(integer) to variable ‘a’. Then when you print a, the computer will print ‘1’) E.g., char mychar = ‘a’  or char mychar = 65. (When you declare you variable, you need to put what type is it before you write the name of the variable.) You might say, “come on! 65 is not a character. If I say 65pple, can be it’s apple? Hell No. You mad computer science student!” But actually,  A will be interpreted as 65 by computer. Because, again computers are stupid, so they even don’t know what is mean by “A”. So they look up some dictionary that is called ASCII code. By ASCII code A is 65. Then they change it to some kind of a sequence of 0 and 1 in order to make computer understand.

So now we’re done with the primary data type.

Now, are you satisfied with this? Then let’s go to secondary data type. As I stated before,

“Array, Structure, Unions, Pointers” are the secondary data type.

Let’s look at the most important one first!

It’s called “POINTER!”.

Many people seems to have trouble to understand this notion. But well, come with me. Let’s break this bad boy in a half.

//writing is in process (I’ll write about pointer based on pointer introduction pdf from MIT)

But But, How do we use those data types?  I don’t want to put something in my brain that I don’t need to use!!

Okay, then let’s put aside the secondary data type for now and talk about how to use it first.

You can manipulate those datatype using variable. You store data in variable. So variable is some kind of pocket to put the things that you need. But only one for each pocket. You will probably have “A LOT” of pocket while you’re programming. Hence you need to at least write down on the pockets about what type of things that pockets have inside. That’s it. We call this (decide what type of object, your pocket will contain)as  declaring variable(i.e., make a pocket to store things) by typing like below.

Data_type variable_name ;

e.g., int MyInt;

When you make a pocket, you don’t need to put something at the same time. You can make a pocket first and you can put it later on. e.g., char mychar; and later on mychar = 2; is possible.

* bit interesting subject (1): const

“Const” stands for “constant” and this same as any int/float variable but it’s unchangeable.

If you set it, you can’t change it but that means you CAN prevent other people from changing it.

* bit interesting subject (2): unsigned/ signed variable

Since for integer, you got 2 bytes, so it’s 2×8 = 16 bits. (Actually it depends on the compiler and OS, you use. That’s what I heard in my lecture, so people sometimes use size of(), we well talk about this later in detail) You need to give a bit for sign and 15 bits to represent the number behind the sign.

The picture above is the example of the binary representation of -1.

So the range of the integer is from -2^15 to 2^15, but if you know that you’re only will assign positive number to a variable and you’re planning increasing the variable a lot over the range of integer. Then you can change some implementation of how integer is stored. Instead of using the first bit for a sign, you can use it to represent a number. So now you have a integer up to 2^16. you can do this by declaring your variable as “unsigned int”

e.g., unsigned int Myint;

Operator with variable

Let’s say if you want add two variables, then how would you do it? We do this by “Operator”. If you ever done any programming in Python, you might recognize that the operators used in C are pretty similar with the things in Python.

There is a chart that you can check those operator.

E.g., int var3 = var2 + var1,

float var4 = var2 / var1

Easy? But,usually we sue program to interact with the real world.

So we expect to get the values for variables from users. So how we will get those input?

How to read standard user input?

We can read directly from the key board.

In this case, we need to use “scanf” to get the value and will store it to a variable.

E.g., scanf()// stop for here for this moment.

Some website to check to write

http://www.codingunit.com/c-tutorial-how-to-use-pointers

http://www.codingunit.com/c-tutorial-more-on-pointers

http://beej.us/guide/bgc/output/html/multipage/scanf.html

http://pdos.csail.mit.edu/6.828/2012/readings/pointers.pdf

** Note: There might be some misleading information since I’m not an expert, so don’t believe it all and please get me correct if you find some flaws. Also I got ‘a lot’ of  information from ‘everywhere’. What I did is just writing and summing that up in a nice and understandable way(hopefully).  I feel super lazy to write all the source, BUT for you, I’ll list some of nice website that I thought useful, while I was searching the information to write this blog.

– http://www.codingunit.com/(Go to the c-tutorial section, very nice and I got a lot of information from here.)

Advertisements