Least Significant Bit

All in all we’re just another bit in the word

How to teach C to a violinist

Posted by jayzeegp en julio 12, 2008

I know lots of computer geeks who learn music but I just know one musician who would like to learn how to make programs just to “have fun” with it (yeah, crazy).

This entry (and I hope that some more in future) is to try to explain C in a very easy way, so if you are studying computer science, just read this in case you want to laugh a little😛.

Well, I assume you know how to use a text editor and the compiler. So we will just talk about C.

Main function

We won’t explain now what functions are, just imagine they are places to put the code inside and the main function will be the first one that will be executed.

The synthax is the following:

int main ( ) {

/*Code of the program*/

}

Comments

You may like to put comments on your program to explain things to other readers of the code or just to make you not forget why did you do one certain thing on a certain moment. To insert the comments you just have to put the text inside /* and */ as you see in the example above. It won’t affect the program execution.

Variables

Imagine that you want to do a program that makes the addition of 2 numbers. You must store those numbers somewhere. Thats the reason to use variables. I think you can imagine them as a box where you can put numbers, characters and some other things (that you don’t need to worry about for now :P).

How to declare them. I will show you with some examples.

int a;

This line declares a variable which can “hold” an integer number, the name of the variable is a. And int is the type of the variable. For now we will just use int variables. You must put this declaration at the beggining of the main function. And, at the end of this statement you must type ;

Which value is stored on that variable? We didn’t put any values so you can’t know which value is stored there.

Declare a variable with a value:

int b=4;

This would declare a int variable called b which stores the value 4.

Can I change the value of a variable after declaring it? Yes. You have to put the variable name, then the symbol “=” and then the value (or operations which results in that value)s.

a=34;

b=a+2;

Can you give me an example of all that? My mind is going crazy!

Yes, I can:

int main ( ) {

int a; /*This declares variable a*/

int b=4; /*This declares variable b with value 4*/

a=3; /*This stores 3 on variable a*/

b=a+2; /*This will change the value of b from 4 to 5*/

}

3 comentarios to “How to teach C to a violinist”

  1. […] Julio 14, 2008 If you did not see part I, please click here. […]

  2. […] 15, 2008 Now that you know how to declare variables and how to print information on screen, you will learn how to make your program use data asked to […]

  3. […] you didn’t see Parts I, II and III you may like to read […]

Responder

Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: