Create a gist now

Instantly share code, notes, and snippets.

@chokkan /sum.c
Last active Dec 17, 2015

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