Gotchas
Watch out for some common unintentional mistakes that can be made in C.
We'll cover the following...
There are a number of classic gotchas in C to watch out for when trying to figure out why our program is not running as expected (or not running at all). We should check for these first when debugging, they are common.
Integer math
Like Python, in C when we write mathematical expressions, we should be sure to include decimal points for all numbers (that is, unless we actually want to do integer math). This is best illustrated by the following example. What do you think the following code will print to the screen?
Press + to interact
#include <stdio.h>int main(void){double mass = 2.5;double velocity = 4.5;double kinetic_energy = (1/2) * mass * velocity * velocity;printf("Kinetic energy = %.3f\n", kinetic_energy);return 0;}
If you guessed this, you are correct!
Why did this happen? It’s because the C expression ...