Skip to content

Instantly share code, notes, and snippets.

@chokkan
Last active December 17, 2015 23:59
Show Gist options
  • Save chokkan/5693789 to your computer and use it in GitHub Desktop.
Save chokkan/5693789 to your computer and use it in GitHub Desktop.
A 'buggy' code for calculating the sum of values in an input file.
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp = NULL;
double value, sum = 0;
fp = fopen(argv[1], "r");
if (fp = NULL) {
fprintf(stderr, "failed to open %s\n", argv[1]);
return 1;
}
while (fscanf(fp, "%f", &value) == 1) {
sum += value;
}
printf("%f\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment