How to write clean code
struct thingy {
int x;
double y;
};
int calcualte_result(struct thingy x, struct thingy y) {
if((x.x - y.y) > (y.x - x.y)) {
return 0;
} else if ((y.x - x.y) > (x.x - y.y)) {
return 1
} else {
return -1;
}
}
int main(void) {
struct thingy x;
x.x = 50;
x.y = 5.0;
y.x = 45;
y.x = 2.5;
calculate_result(x, y);
}
Follow the style guide (will be marked)
There is no right style guide, but you should follow it
struct thingy {
int x;
double y;
};
int calcualte_result(struct thingy x, struct thingy y) {
if((x.x - y.y) > (y.x - x.y)) {
return 0;
} else if ((y.x - x.y) > (x.x - y.y)) {
return 1
} else {
return -1;
}
}
int main(void) {
struct thingy x;
x.x = 50;
x.y = 5.0;
y.x = 45;
y.x = 2.5;
calculate_result(x, y);
}
We can pass input into functions:
int cool_calculation(int x, int y)
int x
, int y
are the input, or arguments into the functionWe can use the input to determine how the function runs
int cool_calculation(int x, int y) {
if (x > 0) {
// do something when x is positive
} else {
// do something if x is negative
}
}
scanf
)main
to allow for CLIint main(void) {
}
int main(int argc, char *argv[]) {
//...
}
6
is really "6"
atoi() function
to convert strings to integersStands for ASCII to Integer
Included instdlib.h
atoi(const char *str)
atol
, atof
and atoll
all exist (long, float, long long)int i = 0
while (i < SOME_NUM) { i++; }
int i = 0
while (i < SOME_NUM) {
...
i++;
}
for (int i = 0; i < SOME_NUM; i++) {
...
}