Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 4
  • chroniccommand
    Posts: 1,389
    In my last guide, I talked about basic datatypes in C such as int, float, double and char. Now I will go over using math in C with these datatypes(Please don't run and scream and close your browser when you see this, its not all that bad... for now).


    Math in C
    So, you should remember the basic datatypes by now. Int(Integer), float, double and char(You may wonder what unsigned and signed means, but that's for part 5). Now I will go over basic math. In case you don't remember from part 1, a computer is stupid. It's just fast and obeys commands. So we will be inputting what the computer needs to do. And at the end we may create our first useful program, a very basic calculator. Now take a look at this chart of basic operators in C

    + Plus
    - Minus
    * Times
    / Divided by

    Now the plus and the minus probably seem normal to you (If you payed attention in kindergarten). The times and divided by symbols may be a little strange. You should be used to seeing an "X" like figure for multiply but in C we use the asterisk. The divided by symbol should be strange too as you should be used to seeing that strange symbol with 2 dots and a - like figure in between it. Well in C, we use the forward slash to represent division. Now we should know what 2 plus 2 equals. Well take a look at this simple C program that will calculate 2 + 2 and print it. I will explain the code with comments, so there is no need for an explanation.

    #include <stdio.h>
    //Don't forget the standard header file
    int main()
    {
    int answer = 2 + 2; /*Declaring integer answer as 2 plus 2*/
    printf(\"Two plus two equals %i\n\", answer);
    return(0);
    }

    So if you read the comments, we are setting the integer of answer to 2 plus 2, and then printing the sum of 2 plus 2 in that printf statement. So you should get something like this:

    Two plus two equals 4


    We can also do the same thing with division, multiplication and subtraction. Now remember "My Dear Aunt Sally"? That stands for Multiplication, Division, Addition, Subtraction. The way the C compiler works is it looks for multiplication first, then division then addition then subtraction, in that order. If you wanted to make something like addition go before multiplication you would add parenthesis around the addition, and the addition is calculated first. Now are you ready to create your first calculator program on C? Finally something useful! What this will do is take two numbers and multiply, divide, add and subtract them. Let's get coding shall we?

    #include <stdio.h>

    int main()
    {
    int one = 14; //first number
    int two = 30; //second number
    printf(\"The two numbers added are %i\n\", one + two);
    printf(\"The two numbers subtracted are %i\n\", one - two);
    printf(\"The two numbers multiplied are %i\n\", one * two);
    printf(\"The two numbers divided are %i\n\", one / two);
    return(0);
    }

    As you can see, this preforms simple math on the values of one and two. Compile and run this and see what happens.

    This was a simple tutorial of using math in C. There are more advanced functions I will eventually be going over, but that's it for this guide. In the next portion I'll do if statements and that good shit. Hope you learned something,
    --Chroniccommand
  • Xin
    Posts: 3,251
    Once again an amazing contribution to the community!
    Xin
  • Bursihido
    Posts: 406
    very helpful tutorial thanks :)