Results 1 to 2 of 2

Thread: Programming in C (Part 2)

  1. #1
    Junior Member
    Join Date
    Nov 2003
    Posts
    5

    Programming in C (Part 2)

    OK, here’s the second part of my C tutorial. If you want to see the first part, it should be at

    http://www.antionline.com/showthread...106#post691106



    4 – Working with integer variables

    Here I will show you how to add and increment integer variables. Incrementation is shorthand code to increase or decrease (decrement) an integer variable by a certain amount.



    #include <stdio.h>

    void main()
    {
    int x, y;

    x=5;

    printf("The value of x is %i.\n",x);

    x++;

    printf("The value of x is now %i.",x);

    y = x * 2 / 3;

    printf("The value of y is %i.",y);

    }



    New code breakdown:

    int x, y; - this tells the compiler to declare two integers at once. This saves you having to repeat the int declaration thing over and over again.

    \n – When in a string, this tells the printf function to start a new line. This tidies up the output so it isn’t one big chunk of text.

    x++; - this is a shorthand way of writing x = x + 1. It increases x by 1. It also has it’s opposite – x--. This (obviously) decreases x by 1.

    y = x * 2 / 3; - the * bit here is the compilers way of writing a multiplication x. The forward slash is a division sign. Simple really. The compiler uses x to represent the value of x, obviously – in this case 6.

    The output of this program should be:

    The value of x is 5.
    The value of x is now 6.
    The value of y is 4.

    Excellent.


    5 – Characters and the getch() function

    OK – so far we have only worked with numbers, but letters are also important. This program waits for a character to be pressed on the keyboard and prints it out.



    #include stdio.h

    void main()
    {

    char c;

    c = getch();

    printf(“You pressed the %c key”,c);

    }




    New code breakdown:

    char c; - this declares a character variable. It uses an ASCII code to hold any of 256 characters.

    c = getch(); - here, the getch() function is called, which waits for a keypress. When a key is pressed, the character that the key represents is ‘returned’ from the function and ‘passed’ by the = sign to the c variable. Got that?

    %c – this tells the printf function to insert a character variable – similar to %i earlier.


    6 – String variables

    A string is a group of character variables – an ‘array’. This basically means that the characters are grouped in the computers memory and can be accessed by a single variable. It’s a little more complicated than that, but more will be explained in later lessons.



    #include stdio.h

    void main()
    {

    char string[] = “Wow! It’s a string!”;

    printf(“%s, string);

    }



    New code breakdown:

    char string[] – as I said earlier, a string is a group of characters. The [] brackets show that it is a group and not just a single variable.

    string[] = “Wow! It’s a string!” – This is an example of initialising and declaring a variable on the same line. Again, it’s just a space saver.

    %s – another symbol for the printf() function, this time specifying a string.

    And that’s that. Stay tuned for more on arrays.

  2. #2
    Senior Member
    Join Date
    Sep 2003
    Posts
    161
    Great tutorial, i cannt wait for the next one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •