forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.c
64 lines (51 loc) · 1.65 KB
/
arithmetic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************
*
* Program: Arithmetic operations
*
* Description: Examples of arithmetic operations in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=v7r9LD6oIYA
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <math.h>
int main(void)
{
int a = 5;
int b = 2;
// standard operations include addition, subtraction, multiplication,
// division and modulus
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
printf("%d / %d = %d\n", a, b, a / b);
printf("%d %% %d = %d\n", a, b, a % b);
// increment and decrement examples
a++;
printf("a: %d\n", a);
a--;
printf("a: %d\n", a);
// increment and decrement also come in prefix variants
++a;
printf("a: %d\n", a);
// postfix increment will first use the value of the variable in the
// expression and then increment the number
printf("a++: %d\n", a++ );
printf("a: %d\n", a );
// prefix increment will FIRST increment a and then use its value in
// the expression
printf("++a: %d\n", ++a );
double x = 5.0;
double y = 2.0;
// operations in C behave differently depending on the type... notice how
// division behaves in this case
printf("%f + %f = %f\n", x, y, x / y);
// many operators we might expect to have are not included in the language
// itself, such as power, but we can include math.h or other libraries to
// use them
double z = pow(x, y);
printf("%f^%f = %f\n", x, y, z);
return 0;
}