Programming C: Posts 4 – Arithmetic, operator in C

Today's lesson will introduce you how to perform calculations in C, operators to handle more data.

1. Mathematical operators

The C language provides 5 basic arithmetic operations

STT Operator Name Example Result
1 + Community 3 + 4 7
2 Minus 10.5 – 3 7.5
3 * Staff 3 * 2 6
4 / Share 6 / 3 2
5 % Divide the balance 8 % 3 2

Ok. Now try doing examples:

#include <stdio.h>

int main()
{
    int a = 5, b = 7;
    double c = 4.5, d = 6;

    printf("%d + %f = %f \n", a, c, a + c);
    printf("%d - %d = %d \n", a, b, a - b);
    printf("%d * %f = %f \n", b, d, b * d);

    /* Luu y phep chia nhe*/

    printf("%d / %d = %d \n", b, a, b / a);
    printf("%f / %d = %f \n", c, a, c / a);
    printf("%f / %f = %f \n", c, d, c / d);

    printf("%d %% %d = %d \n", b, a, b % a);

    return 0;
}

Result:

5 + 4.500000 = 9.500000
5 - 7 = -2
7 * 6.000000 = 42.000000
7 / 5 = 1
4.500000 / 5 = 0.900000
4.500000 / 6.000000 = 0.750000
7 % 5 = 2

The remainder operator (%) require both operands are integers. It returns the remainder of the division. Example 7 % 5 calculated by dividing integers 7 give 5 to be 1 and the remainder is 2; so the result is 2.

Common, if both operands are integers then the result will be an integer. However, one or both of the operands is true then the result will be a real number.

When both operands are integer division operator then division is performed as an integer division and the division is not normal that we use. Integer division always is the natural result of injuries. Example: 7 / 5 = 1 but not 7 / 5 = 1.4. To fix this, we may transfer some or all 2 to perform some type then performs division. How to Convert (or cast) It is as follows:

(type to be transferred) variable.
CEO: (float) the;

Noted While this is a cast of the original variable type does not change, only the instantaneous value (at that time to change to a new type). To save this immediate value you need to declare a new variable of type to transfer and assign that value to. For example, for easy.

#include <stdio.h>

int main()
{
    int a = 5, b = 7;
    double c;

    printf("%d / %d = %d \n", b, a, b / a);

    /* Chuyen gia tri tuc thoi cua b sang kieu so thuc*/
    printf("%d / %d = %f \n", b, a, (double)b / a);

    /* Chuyen gia tri tuc thoi cua a sang kieu so thuc*/
    printf("%d / %d = %f \n", b, a, b / (double)a);

    /* Neu lam the nay thi van khong dung, vi b/a duoc so nguyen
     * sau do chung ta moi ep kieu so nguyen do sang so thuc
     */
    printf("%d / %d = %f \n", b, a, (double)(b / a));

    return 0;
}

Result:

7 / 5 = 1
7 / 5 = 1.400000
7 / 5 = 1.400000
7 / 5 = 1.000000

2. Relational operators

The C language provides 6 relational operators to compare the number of. The relational operators valuable 1 (the right result) or 0 (when the wrong result).

STT Operator Name Example Result
1 == Compared with 5 == 5 1
2 != Compare other 5 != 5 0
3 > Compare greater 5 > 4 1
4 < Compare smaller 5 < 4 0
5 &gt;= Comparison greater than or equal 5 &gt;= 4 1
6 <= Comparison less than or equal 5 <= 5 1

We use these operators to compare values, characters,… However not compare strings together because this will lead to the address of the string to be compared rather than strings of content. We have the function to compare strings in the string library and will learn later. When compared with other characters, then we can understand the nature of the computer compares the ASCII code of the character together. CEO. 'A' & gt; 'B' will return the value 0 for 'A' is the ASCII code 65 while 'B' is 66.

3. Logical operators

C offers 3 Logical operators for connecting the logical expression. Like the relational operators, the logical operators has value 1 or 0.

STT Operator Name Example Result
1 ! Negative ! (5 > 4) 0
2 && And 5 > 4 && 5 > 6 0
3 || Or 5 > 4 || 5 > 6 1

4. Operator to increase or decrease

The operator increased (++) and reduced (- -) provide respectively for convenience plus 1 on a variable or minus 1 from a variable.

#include <stdio.h>

int main()
{
    int i, k;

    i = 5; k = i++;
    printf("i = %d, k = %d\n", i, k);

    i = 5; k = ++i;
    printf("i = %d, k = %d\n", i, k);

    i = 5; k = i--;
    printf("i = %d, k = %d\n", i, k);

    i = 5; k = --i;
    printf("i = %d, k = %d\n", i, k);

    return 0;
}

Result:

i = 6, k = 5
i = 6, k = 6
i = 4, k = 5
i = 4, k = 4

That is, we have:

++i and –i then i is calculated before then will take the results to implement expression
i ++ and i– then i was put into practice new expressions before then calculate i

5. Assignment operator

The assignment operator is used to store the values ​​for 1 certain variables.

Operator Example Equivalent
= x = 5 Assign 5 for x
+= x = 5 x = x + 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5

Run some tests nhé.

#include <stdio.h>

int main()
{
    int x;

    x = 5;
    printf("x = 5 => x = %d\n", x);

    x += 5;
    printf("x += 5 => x = %d\n", x);

    x -= 5;
    printf("x -= 5 => x = %d\n", x);

    x *= 5;
    printf("x *= 5 => x = %d\n", x);

    x /= 5;
    printf("x /= 5 => x = %d\n", x);

    return 0;
}

Result:

x = 5 => x = 5
x = 5 => x = 10
x -= 5 => x = 5
x *= 5 => x = 25
x /= 5 => x = 5

6. Some other operators

6.1 Comma operator

Multiple expressions can be connected to the same expression using the comma operator. Comma operator requirements 2 operand. First, it estimates the left operand then right operand, and returns the value of the right operand as the final result.
Example:

#include <stdio.h>

int main()
{
    int m, t;

    m = (t =2, t*5 + 10);
    printf("t = %d, m = %d\n", t, m);

    return 0;
}

Result:

t = 2, m = 20

6.2 Operators take size

C provides useful operator, sizeof, to calculate the size of any data item or data type. It requires a single operand can be a model name (example, int) or an expression (example, 100) and returns the size of the entity specified in bytes. Prototyping example nhá. This operator we have become familiar in the data type and then.

Running examples tested:

#include <stdio.h>

int main()
{
    printf("char size = %d byte\n", sizeof(char));
    printf("short size = %d byte\n", sizeof(short));
    printf("int size = %d byte\n", sizeof(int));
    printf("long size = %d byte\n", sizeof(long));
    printf("float size = %d byte\n", sizeof(float));
    printf("double size = %d byte\n", sizeof(double));
    printf("1.55 size = %d byte\n", sizeof(1.55));
    printf("\"Hello\" size = %d byte\n", sizeof("Hello"));
    return 0;
}

Result:

char size = 1 byte
short size = 2 byte
int size = 4 byte
long size = 8 byte
float size = 4 byte
double size = 8 byte
1.55 size = 8 byte
“Hello” size = 6 byte

7. Priority of operators

Priority of operators is done from top to bottom in the following table. In each row has priority as in the first column 3.

Species Operator Priority
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
multiply * / % Left to right
Add up + – Left to right
Move << >> Left to right
relationship < <= > >= Left to right
Balance == != Left to right
Allows DNA bits & Left to right
XOR bit ^ Left to right
OR allow bits | Left to right
AND logic allowed && Left to right
Allows the OR logic || Left to right
Condition ?: Right to left
Assign = += -= *= /= %=>>= <<= &= ^= |= Right to left
comma , Left to right

Exercise

  1. Write a program in seconds, Change the number of seconds entered into a "gio:phut:Shine ", each component is an integer 2 number. VD input 7826 then print out 02:10:26

  2. The variable x = 3; Do not run the program, Guess the value of x after execution:
    x = ++x – 3 + x;
    After guessing finished, try to write a program to check the results.