When I run the following code, I get an error.
void printData(int total_trees,int burned){
printf("Before printing data\n");
float percentBurned = (float)burned / (total_trees+burned)*100;
printf("total burned: %d (%.1f\%)\n",burned,percentBurned);
printf("trees left: %d\n", total_trees);
// printf("trees left: %d\n", total_trees);
printf("After printing data");
}
The error says *** stack smashing detected ***: terminated
Before outputting the error, it outputs the first three printf statements.
Link to image because this is my first stackoverflow post
I've tried rearranging everything so many times and nothing has worked so far. I'm guessing the printf statements themselves aren't triggering an overflow, but I could be wrong.
I'm grateful for any help you can give :)
edit: printf("trees left: %d\n", total_trees); is the last line that runs before throwing the error
\%with%%?"trees left:"is printed, but"After printing data"is not printed? Or did you run the program line by line in a debugger? If it is the former, then your conclusion could be false, becausestdoutis normally buffered and therefore not necessarily printed immediately. In that case, you may want to consider printing tostderrinstead, which is normally unbuffered.float percentBurnedand notdouble percentBurned?