I'm easing back into C after several months of using Python and Matlab. Can anyone give me some pointers on how to improve my code's efficiency and readability?
#include <stdio.h>
#define MAX_ARG 2
int count( char FNAME[] );
int main( int argc, char *argv[]) {
if( argc > MAX_ARG ) {
printf("Too many arguments ( %d ) supplied.\n", argc);
return 0;
}
int FILE_LENGTH = 0;
FILE_LENGTH = count( argv[1] );
printf("The length of %s is: %d lines.\n", argv[1], FILE_LENGTH);
return 0;
}
int count( char FNAME[] ) {
FILE *fp;
int CHR;
int LINE_COUNT = 0;
fp = fopen( FNAME,"r" );
while( ( CHR = fgetc( fp ) ) != EOF ) {
if( CHR == '\n' ) {
LINE_COUNT++;
}
}
fclose( fp );
return LINE_COUNT;
}