Least Significant Bit

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

How to teach C to a violinist (Part III)

Posted by jayzeegp en julio 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 the user via keyboard.

In order to do this we will use scanf function.

We will use it to receive an integer via keyboard and “write” it into a variable that we created previously.

Syntax to ask for an integer and store it in the variable called “raf”:

scanf(“%d”,&raf);

I think it is easy to see that %d is the same that we did on printf, it just says that our program is waiting for an integer number. The & symbol is a thing a little bit complex to explain right now, so we will think it just says “save the number here”, and then, you put the name of the variable.

Example:

int main(){

int a;

printf(“Insert a number\n”);

scanf(“%d”,&a);

}

This program will just wait for the user to insert a number via keyboard and will save it on variable a.

I think I understood! Give me more exercises!

1.-Modify the example above to make it say “The number you entered was X”, where X is the number stored in a.

2.-Make a program which ask the user for two numbers and prints the result of multiplying them on screen.

4 comentarios to “How to teach C to a violinist (Part III)”

  1. rafinhaviolino said

    Ok… after a help of my teacher and personal helper in his “free time”, I did the 1st exercise. =D~

    int
    main(){
    int a;
    printf(“Enter a Number:”);
    scanf(“%d”,&a);
    printf(“The number you entered was %d”,a);
    getchar();
    getchar();
    }

    And now I want to know more! =P~

  2. rafinhaviolino said

    The 2nd one I did without to much problems…
    cof… cof… cof… x]~
    Thanks the other class that I had a lil time ago.^^’

    int
    main () {
    int a;
    int b;
    int c;
    printf(“Enter a: “);
    scanf(“%d”,&a);
    printf(“Enter b: “);
    scanf(“%d”,&b);
    c=a*b;
    printf(” %d*%d=%d”,a,b,c);
    getchar();
    getchar();
    }

    Is it right? :P~
    At least it works! x]~

  3. jayzeegp said

    Both will work, but I will teach you a little thing that you could do with printf on the second exercise:
    int
    main () {
    int a;
    int b;

    printf(”Enter a: “);
    scanf(”%d”,&a);
    printf(”Enter b: “);
    scanf(”%d”,&b);

    printf(” %d*%d=%d”,a,b,a*b);
    getchar();
    getchar();
    }

    I hope you will know how it works without problems😛

  4. […] 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: